//将屏幕信息转换为位图结构 CDC *pDeskDC=GetDesktopWindow()->GetDC();//获取桌面的额画图对象 int width=300; int height=300; CDC memDC;//定义一个内存画布 memDC.CreateCompatibleDC(pDeskDC);//创建一个兼容的画布 CBitmap bmp; bmp.CreateCompatibleBitmap(pDeskDC,width,height);//创建兼容位图 memDC.SelectObject(&bmp);//选中位图对象 memDC.BitBlt(0,0,width,height,pDeskDC,0,0,SRCCOPY);//将屏幕信息绘制到位图中 。。。。 char* pData=new char[bitmap,bmWidthBytes* bitmap,bmHeight];//分配内存,存储位图的实际数据 //获取位图的实际数据 ::GetDIBits(memDC.m_hDC,bmp,0,bitmap.bmHeight,pData,pBInfo,DIB_RGB_COLORS); // 客户端SOCKET通信 #include "Winsock2.h" #pragma comment(lib,"Ws2_32.lib"); // 连接库文件 WSDATA data; WSAStartup(2,&data);//初始化套接字 hostent* phost=gethostbyname("");//获取本机信息 char* localIP=inet_ntoa(*(struct in_addr*)*phost->h_addr_list);//获取IP地址 sockaddr_in addr; //定义一个套接字地址 addr.sin_addr.S_un.S_addr=inet_addr(localIP);//设置IP地址 addr.sin_port=htons(5001);//设置端口号 m_Socket=socket(AF_INET,SOCK_DGRAM,0);//创建UDP套接字 if(m_Socket==INVALID_SOCKET) { MessageBox("套接字创建失败"); //如果套接字创建失败,弹出对话框 } char*len="512"; if(setsockopt(m_Socket,SQL_SOCKET,SO_SNDBUF,len,4)!=0) { MessageBox("设置失败"); } if(bind(m_Socket,(sockeaddr*)&addr,sizeof(addr))==SOCKET_ERROR)//绑定套接字 { MessageBox("套接字绑定失败"); } char* len="512"; if(setsockopt(m_Socket,SOL_SOCKET,SO_SNDBUF,len,4)!=0) {//设置套接字发送缓冲区大小 MessageBox("设置失败"); } if(bind(m_Socket,(sockaddr*)&addr,sizeof(addr)==SOCKET_ERROR)//绑定套接字 {MessageBox("套接字绑定失败"); } //在创建成功后可以调用sendto方法发送数据。 sendto(m_Socket,pPackage,packsize,0,(sockaddr*)&addr,sizeof(addr)); //////////////////////////////////////////////////////////////////////////////////////////// //主窗体设计 hostent* phost=gethostbyname(""); char* localIP=inet_ntoa(*(struct in_addr*)*phost->h_addr_list);//获取IP地址 sockaddr_in addr; addr.sin_family=AF_INET; addr.sin_addr.S_addr=inet_addr(localIP); addr.sin_port=htons(5002); m_Socket=socket(AF_INET,SOCK_DGRAM,0); if(bind(m_Socket,(sockaddr*)&addr,sizeof(addr)==SOCKET_ERROR)//绑定套接字 { MessageBox("套接字绑定失败"); } ///////////////////////////////////////////// sockaddr_in addr;//定义网络地址 addr.sin_family=AF_INET;//设置网络地址类型 addr.sin_port=htons(m_ClientPort);//设置网络端口号 addr.sin_addr.S_un.S_addr=inet_addr(m_ClientIP.GetBuffer(0));//设置IP int size=sizeof(addr);//获取地址大小 char* pData="Confirm";//定义发送的信息 sendto(m_Socket,pData,8,0,(socketaddr*)&addr,size);//发送数据 atoi()//将编辑文本转换为整数。 CFont newFont,*oldFont; //声明新字体对象,和旧字体对象。 gameFrame::gameFrame() { Rect rect; Create(NULL,"绘图窗口"); //建立窗口 CClientDC dc(this);//将DC的数据映射到窗口 int width=dc.GetDeviceCaps(HORZRES);//取得屏幕区的宽度 int height=dc.GetDeviceCaps(VERTRES);//取得屏幕区的高度 GetWindowRect(&rect);//取得窗口区矩形的大小 width=(width-(rect.right-rect.left))/2; height=(height-(rect.bottom-rect.top))/2; MoveWindow(width,//移动窗口位置 height, (rect.right-rect.left), (rect.bottom-rect.top), true); //在这里建立Cfont类对象newFont newFont.CreateFont(100,50,50,0,100, TRUE,TRUE,FALSE, DEFAULT_CHARSET, OUT_CHARCTER_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH|FF_MODERN, "楷体"); } ////////////////////////////////////// LoadImage()//加载位图 CPaintDC dc(this);//建立响应WM_PAINT消息的DC CDC *mdc=new CDC; //声明内存dc CBitmap * bitmap=new CBitmap;//声明CBitmap位图对象 mdc->CreateCompatibleDC(&dc);//建立于dc兼容的DC bitmap->m_hObject=(HBITMAP)::LoadImage(NULL,"/bground.bmp", IMAGE_BITMAP,500,400,LR_LOADFROMFILE);//加载位图 mdc->SelectObject(bitmap);//将位图对象指定到mdc中 //镂空原理的和运算与或运算。 OnPaint() { CPaintDC dc(this); mdc->SelectObject(bgbmp);//取得背景图 //贴上背景图 dc.BitBlt(0,0,rect,right,rect.bottom,mdc,0,0,SRCCOPY); mdc->SelectObject(bitmap);//取得原位图 //将蒙版与背景图做AND运算 dc.BitBlt(250,150,104,154,mdc,104,0,SRCAND); //将要镂空的图与背景图做OR运算 dc.BitBlt(250,150,104,154,mdc,0,0,SRCPAINT); } //服务器端SOCKET BOOL CServerDlg::OnInitDialog() { //获取本机IP hostent* phost=gethostbyname(""); char* localIP=inet_ntoa(*(struct in_addr *)*phost->h_addr_list); sockaddr_in addr; addr.sin_family=AF_INET; addr.sin_addr.S_un.S_addr=inet_addr(localIP); addr.sin_port=htons(5002); //创建套接字 m_Socket=socket(AF_INET,SOCK_DGRAM,0); if(m_Socket==INVALID_SOCKET); { MessageBox("套接字创建失败"); } //绑定套接字 if(bind(m_Socket,(sockaddr*)&addr,sizeof(addr))==SOCKET_ERROR) { MessageBox("套接字绑定失败"); } m_BmpData=new char[1024*1024*2]; memset(m_BmpData,0,1024*1024*2); m_TempData=new char[1024*1024*2]; memset(m_TempData,0,1024*1024*2); m_Header=m_BmpData; WSAAsyncSelect(m_Socket,m_hWnd,CM_RECEIVEDM,FD_READ); m_ShowBmp=FALSE; m_BmpSize=0; m_RecSize=0; m_ClientPort=0; CServerApp* pApp=(CServerApp*)AfxGetApp(); CString sql="select * from tb_ClientInfo"; pAp->m_DataManage.m_pRecord->CusorLocation=adUseServer; pApp->m_DataManage.m_pRecord->Open((_bstr_t)sql,pApp->m_DataManage.m_pConnect.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText); m_ClientIP=""; if(pApp->m_DataManage.m_pRecord->RecordCount>0) { m_ClientIP=(char*)(_bstr_t)pApp->m_DataManage.m_pRecord->GetFields()->GetItem(0L)->Value; } pApp->m_DtaManage.m_pRecord->Close(); m_LeftTopX=0; m_LeftTopY=0; m_BK.SetWindowPos(NULL,0,0,305,305,SWP_NOMOVE); return TRUE; } void CServerDlg::OnReceived() { //接收数据 char* buffer=new char[MAX_BUFF]; sockaddr_in addr; int factsize=sizeof(sockaddr); int ret=recvfrom(m_Socket,buffer,MAX_BUFF,0,(sockaddr*)&addr,&factsize); if(ret!=-1) {CString recvIP=inet_ntoa(addr.sin_addr); if(recvIP!=m_ClinetIP) { delete []buffer; return; } m_ClientPort=ntohs(addr.sin_port); //记录接收的数据报大小 m_RecvSize+=ret; //读取序号 WORD orderID=*(WORD)&buffer[0]; WORD endID=*(WORD)&buffer[2]; //读取位图的大小 int bmpsize=*(int*)&buffer[ret-12]; //获取屏幕的X轴坐标 m_LeftTopX=*(WORD*)&buffer[ret-8]; //获取屏幕的Y轴坐标 m_LeftTopY=*(WORD*)&buffer[ret-6]; //读取结束标记 sockaddr_in addr; addr.sin_family=AF_INET; addr.sin_port=htons(m_ClientPort); addr.sin_addr.S_un.S_addr=inet_addr(m_ClientIP.GetBuffer(0)); int size=sizeof(addr); //发送确认信息 char* pData="Confirm"; sendto(m_Socket,pData,8,0,(sockaddr*)&addr,size); if(orderID==0) {m_BmpSize=bmpsize; m_BmpData=m_Header; }else m_BmpSize+=bmpsize; memcpy(m_BmpData,&buffer[4],bmpsize); m_BmpData=m_Header; }else m_BmpSize+=bmpsize; memcpy(m_BmpData,&buffer[4],bmpsize); m_BmpData+=bmpsize; m_ShowBmp=FALSE; if(endID==1) { m_ShowBmp=TRUE; m_BmpData=m_Header; memcpy(m_TempData,m_Header,1024*1024); ShowImage(); m_RecSize=0; } } delete [] buffer; } void CServerDlg::OnButtonDown(UINT nID) { if(!m_ClinetIP.IsEmpty()) { WORD x=m_LeftTopX; WORD y=m_LeftTopY; char buffer[4]; sockaddr_in addr; addr.sin_family=AF_INET; addr.,sin_port=htons(m_ClinetPort); addr.sin_addr.S_un.S_addr=inet_addr(m_ClientIP.GetBuffer(0)); int size=sizeof(addr); switch(nID) { case IDC_BTNDOWN: { y=m_LeftTopY+100; *(WORD*)&buffer[0]=x; *(WORD*)&buffer[2]=y; int ret=sendto(m_Socket,&buffer[0],4,0,(sockaddr*)&addr,size); break; } case IDC_BTUP: {y=m_LeftTopY-100; *(WORD*)&buffer[0]=x; *(WORD*)&buffer[2]=y; int ret=sendto(m_Socket,&buffer[0],4,0,(sockaddr*)&addr,size); break; } case IDC_BTLEFT: { x=m_LeftTopX-100; *(WORD*)&buffer[0]=x; *(WORD*)&buffer[2]=y; int ret=sendto(m_Socket,&buffer[0],4,0,(sockaddr*)&addr,size); break; } case IDC_BTRIGHT: { x=m_LeftTopX+100; *(WORD*)&buffer[0]=x; *(WORD*)&buffer[2]=y; int ret=sendto(m_SOcket,&buffer[0],4,0,(sockaddr*)&addr,size); break; } case IDC_BTRESTORE: { *(WORD*)&buffer[0]=0; *(WORD*)&buffer[2]=0; int ret=sendto(m_Socket,&buffer[0],4,0,(sockaddr*)&addr,size); break; } } } }
---------------------------------
http://www.educity.cn/wenda/285572.html