Visual C++实现对计算机远程监控
2008-11-14 19:33:26 来源:WEB开发网其中,SendDesktop()函数负责将屏幕保存成位图,然后再通过send()函数将其以数据的形式发送出去,这一部分牵扯较多的位图操作,比较繁琐,由于本文重点并不在此,仅作为一个功能函数将其关键性代码摘选如下:
void SendDesktop()
{
……
//创建桌面设备环境句柄
hdcmy = CreateDC("DISPLAY",NULL,NULL,NULL);
hbufferdc = CreateCompatibleDC(hdcmy);
//创建位图
hBit = CreateCompatibleBitmap(hdcmy, BitWidth, BitHeight);
hOldBitmap = (HBITMAP)SelectObject(hbufferdc, hBit);
StretchBlt(hbufferdc, 0, 0, BitWidth, BitHeight,
hdcmy, 0, 0,SysWidth,SysHeight, SRCCOPY);
hBit = (HBITMAP)SelectObject(hbufferdc, hOldBitmap);
……
//DDBtoDIB
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE );
// 获取位图信息
GetObject(bitmap,sizeof(bm),(LPSTR)&bm);
//初始化位图信息头
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
//bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biBitCount = 4;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
……
lpbi = (LPBITMAPINFOHEADER)hDib;
*lpbi = bi;
GetDIBits(hdc, bitmap, 0L, (DWORD)bi.biHeight,(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS );
bi = *lpbi;
if (bi.biSizeImage == 0)
{
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight;
}
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDib, dwLen, GMEM_MOVEABLE))
hDib = handle;
……
lpbi = (LPBITMAPINFOHEADER)hDib;
BOOL bgotbits = GetDIBits( hdc, bitmap0L, (DWORD)bi.biHeight,(LPBYTE)lpbi+ (bi.biSize + ncolors * sizeof(RGBQUAD)),(LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
SelectPalette(hdc,hPal,FALSE);
……
send(NewSock,(char*)&bitSize,sizeof(bitSize)+1,MSG_OOB);
recv(NewSock,(char*)&BitMsg,sizeof(BitMsg)+1,0);
plmagePoint = (LPBYTE)hDib;
for(WORD i=0;i {
send(NewSock,(char*)plmagePoint,sizeof(BYTE)*US_MAXSIZE,MSG_OOB);
plmagePoint = plmagePoint + US_MAXSIZE;
recv(NewSock,(char*)&BitMsg,sizeof(BitMsg)+1,0);
}
if (bitSize%US_MAXSIZE)
{
send(NewSock,(char*)plmagePoint,sizeof(BYTE)*GlobalSize(hDib)%US_MAXSIZE,MSG_OOB);
recv(NewSock,(char*)&BitMsg,sizeof(BitMsg)+1,0);
}
……
}
客户机端程序设计实现
更多精彩
赞助商链接