获取屏幕显示
获取屏幕显示
CDC* pDeskDC = GetDesktopWindow()->GetDC();
//获取桌面画布对象CRect rc;GetDesktopWindow()->GetClientRect(rc);
//获取屏幕的客户区域int width = 567;
//获取屏幕的宽度int height = 452;
//获取屏幕的高度CDC memDC; //定义一个内存画布memDC.CreateCompatibleDC(pDeskDC);
//创建一个兼容的画布CBitmap bmp;bmp.CreateCompatibleBitmap(pDeskDC,width,height);
//创建兼容位图memDC.SelectObject(&bmp);
//选中位图对象BITMAP bitmap;bmp.GetBitmap(&bitmap);int panelsize = 0;
//记录调色板大小if (bitmap.bmBitsPixel<16)
//判断是否为真彩色位图panelsize = (int)(pow(2,bitmap.bmBitsPixel*sizeof(RGBQUAD)));//2的N次方BITMAPINFO *pBInfo = (BITMAPINFO*)LocalAlloc(LPTR,sizeof(BITMAPINFO)+panelsize);pBInfo->bmiHeader.biBitCount = bitmap.bmBitsPixel;pBInfo->bmiHeader.biClrImportant = 0;pBInfo->bmiHeader.biCompression = 0;pBInfo->bmiHeader.biWidth = width;pBInfo->bmiHeader.biHeight = height;pBInfo->bmiHeader.biPlanes = bitmap.bmPlanes;pBInfo->bmiHeader.biSize = sizeof(BITMAPINFO);pBInfo->bmiHeader.biSizeImage = bitmap.bmWidthBytes*bitmap.bmHeight;pBInfo->bmiHeader.biXPelsPerMeter = 0;pBInfo->bmiHeader.biYPelsPerMeter = 0;m_X = m_RecvX;m_Y = m_RecvY;memDC.BitBlt(0,0,width,height,pDeskDC,m_X,m_Y,SRCCOPY);char* pData = new char[bitmap.bmWidthBytes* bitmap.bmHeight];::GetDIBits(memDC.m_hDC,bmp,0,bitmap.bmHeight,pData,pBInfo,DIB_RGB_COLORS);int BufSize = panelsize+ sizeof(BITMAPINFO)+bitmap.bmWidthBytes* bitmap.bmHeight;memcpy(pSendBuf,pBInfo,sizeof(BITMAPINFO));char* pHead = pSendBuf;pSendBuf += sizeof(BITMAPINFO);memcpy(pSendBuf,pData,bitmap.bmWidthBytes* bitmap.bmHeight);pSendBuf = pHead;addr1.sin_family = AF_INET;addr1.sin_port = htons(5002);addr1.sin_addr.S_un.S_addr = inet_addr(m_ServerIP);//定义数据报的格式/*序号2位||结束标记2位||位图数据||位图数据大小4位||屏幕X坐标2位||屏幕Y坐标2位||数据报大小4位*///定义每次发送位图数据的大小bmpsize = GraphSize;//计算每个位图发送的次数count = BufSize / GraphSize;mod = BufSize % GraphSize;if (mod != 0)count+=1;m_FrameIndex = 0;int ret = SendData(m_FrameIndex,mod,bmpsize,count,pSendBuf,addr1);pSendBuf = pHead;delete []pData;LocalFree(pBInfo);pDeskDC->DeleteDC();bmp.DeleteObject();memDC.DeleteDC();