在windows CE/Windows Mobile上用程序截屏
// totally from web
HBITMAP CopyScreenToBitmap(int &nWidth,int &nHeight)
{
// 屏幕和内存设备描述表
HDC hScrDC, hMemDC;
// 位图句柄
HBITMAP hBitmap, hOldBitmap;
// 屏幕分辨率
int xScrn, yScrn;
//为屏幕创建设备描述表
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
//为屏幕设备描述表创建兼容的内存设备描述表
hMemDC = CreateCompatibleDC(hScrDC);
// 获得屏幕分辨率
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//存储屏幕的长宽
nWidth = xScrn;
nHeight = yScrn;
// 创建一个与屏幕设备描述表兼容的位图
hBitmap = CreateCompatibleBitmap(hScrDC, xScrn, yScrn);
// 把新位图选到内存设备描述表中
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
// 把屏幕设备描述表拷贝到内存设备描述表中
BitBlt(hMemDC, 0, 0, xScrn,yScrn,hScrDC, 0, 0, SRCCOPY);
//得到屏幕位图的句柄
hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
//清除
DeleteDC(hScrDC);
DeleteDC(hMemDC);
// 返回位图句柄
return hBitmap;
}
void CCopyScreenDlg::OnBtnsnap()
{
// TODO: Add your control notification handler code here
HBITMAP bmp;
int nWidth,nHeight;
//抓取屏幕
bmp = CopyScreenToBitmap(nWidth,nHeight);
//将抓取的屏幕压缩显示到窗体上
CDC *pDC = new CClientDC(this);
CDC srcDC;
CRect rect;
this->GetClientRect(&rect);
rect.DeflateRect(0,20);
CPen pen;
pen.CreatePen(PS_SOLID,2,RGB(0,255,255));
pDC->SelectObject(&pen);
pDC->Rectangle(rect);
srcDC.CreateCompatibleDC(pDC);
srcDC.SelectObject(bmp);
pDC->StretchBlt(rect.left+2,rect.top+2,rect.right - rect.left - 4 ,rect.bottom - rect.top - 4,&srcDC,0,0,nWidth,nHeight,SRCCOPY);
DeleteObject(bmp);
}