CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx =GetSystemMetrics(SM_CXSCREEN);
sizeTotal.cy =GetSystemMetrics(SM_CYSCREEN);
SetScrollSizes(MM_TEXT, sizeTotal);
}
第三步:在主菜单条的编辑菜单下添加一子菜单项,ID为IDM_EDIT_SCREEN,标题为“抓取全屏”。通过ClassWizard在CGrabScreenView中添加该菜单项的命令响应函数,采用ClassWizard建议的函数名称OnEditScreen。
第四步:在OnEditScreen函数中实现抓取整个屏幕图像及光标区域。
首先在CgrabScreenView类定义中增加成员变量HDC m_hdcCompatible和HBITMAP m_hbmScreen,分别对应整个屏幕图像及光标区域的兼容设备描述表和兼容位图。然后用下面给出的程序代码获取所需数据。
void CGrabScreenView::OnEditScreen()
{
// TODO: Add your command handler code here
//获取全屏幕窗口的设备描述表
HDC hdcScreen=::GetDC(NULL);
//产生全屏幕窗口设备描述表的兼容设备描述表
m_hdcCompatible=CreateCompatibleDC(hdcScreen);
//产生全屏幕窗口设备描述表的兼容位图
HBITMAP m_hbmScreen=CreateCompatibleBitmap(hdcScreen,
GetDeviceCaps(hdcScreen,HORZRES),GetDeviceCaps(hdcScreen,VERTRES));
//将兼容位图选入兼容设备描述表
SelectObject(m_hdcCompatible,m_hbmScreen);
//将全屏幕窗口位图的象素数据拷贝到兼容设备描述表
BitBlt(m_hdcCompatible,0,0,GetDeviceCaps(hdcScreen,HORZRES),
GetDeviceCaps(hdcScreen,VERTRES),hdcScreen,0,0,SRCCOPY);
//获取当前光标及其位置
HCURSOR hCursor=GetCursor();
POINT ptCursor;
GetCursorPos(&ptCursor);
//获取光标的图标数据
ICONINFO IconInfo;
if (GetIconInfo(hCursor, &IconInfo))
{
ptCursor.x -= ((int) IconInfo.xHotspot);
ptCursor.y -= ((int) IconInfo.yHotspot);
if (IconInfo.hbmMask != NULL)
DeleteObject(IconInfo.hbmMask);
if (IconInfo.hbmColor != NULL)
DeleteObject(IconInfo.hbmColor);
}
//在兼容设备描述表上画出该光标
DrawIconEx(
m_hdcCompatible, // handle to device context
ptCursor.x, ptCursor.y,
hCursor, // handle to icon to draw
0,0, // width of the icon
0, // index of frame in animated cursor
NULL, // handle to background brush
DI_NORMAL | DI_COMPAT // icon-drawing flags
);
//使窗口无效,调用OnDraw重画窗口
Inv