11.6.2 截图模块主窗体的设计与实现(1)
截图模块的主界面以截屏开始前的屏幕为背景并载有信息提示框,截图的主要功能便是依靠此界面而完成。本节将主要介绍主窗体的实现方法。
(1)在对话框的构造函数中,初始化截图模块窗体类变量。最主要的是完成屏幕图像数据的复制工作。实现如下:
- 01 CCatchDlg::CCatchDlg(CWnd* pParent /*=NULL*/)
- 02 : CDialog(CCatchDlg::IDD, pParent)
- 03 {
- 04 m_scRect.left = 0; //屏幕左侧坐标
- 05 m_scRect.top = 0; //屏幕上侧坐标
- 06 m_startPt = 0; //鼠标开始位置坐标
- 07 m_bDraw = FALSE; //是否开始橡皮筋
- 08 m_bFirstDraw = FALSE; //是否第一次开始橡皮筋
- 09 m_bShowMsg = FALSE; //是否显示信息框
- 10 m_strOld = ""; //上次RGB值
- 11 m_scRect.right = GetSystemMetrics(SM_CXSCREEN); //获取屏幕宽度
- 12 m_scRect.bottom = GetSystemMetrics(SM_CYSCREEN); //获取屏幕高度
- 13 //获取屏幕图像
- 14 m_pBitmap=CBitmap::FromHandle(CopyScreenToBitmap(m_scRect));
- 15 //设置橡皮筋框的样式
- 16 m_rectTracker.m_nStyle=CRectTracker::resizeInside |CRectTracker::
- solidLine;
- 17 m_rectTracker.m_rect.SetRect(-1,-1,-1,-1); //设置橡皮筋框的大小
- 18 }
(2)CopyScreenToBitmap()函数实现复制屏幕图像到一个位图变量的功能,如下:
- 01 HBITMAP CCatchDlg::CopyScreenToBitmap(CRect rect, bool bSave)
- 02 {
- 03 GetSystemCursor();
- 04 HDC hScrDC, hMemDC; //屏幕和内存设备描述表
- 05 HBITMAP hBitmap = NULL;
- 06 HBITMAP hOldBitmap = NULL;
- 07 int nX, nY, nX2, nY2; //选定区域坐标
- 08 Int nWidth, nHeight; //断定区域的宽度及高度
- 09 //为屏幕创建设备描述表
- 10 hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
- 11 //为屏幕设备描述表创建兼容的内存设备描述表
- 12 hMemDC = CreateCompatibleDC(hScrDC);
- 13 if (IsRectEmpty(rect)) //确保选定区域不为空矩形
- 14 return NULL;
- 15 //选定区域坐标
- 16 nX = rect.left;
- 17 nY = rect.top;
- 18 nX2 = rect.right;
- 19 nY2 = rect.bottom;
- 20 //确保选定区域是可见的
- 21 if (nX < 0)
- 22 nX = 0;
- 23 if (nY < 0)
- 24 nY = 0;
- 25 if (nX2 > m_scRect.Width())
- 26 nX2 = m_scRect.Width();
- 27 if (nY2 > m_scRect.Height())
- 28 nY2 = m_scRect.Height();
- 29 nWidth =abs( nX2 - nX);
- 30 nHeight =abs( nY2 - nY);
- 31 //创建一个与屏幕设备描述表兼容的位图
- 32 hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
- 33 //把新位图选到内存设备描述表中
- 34 hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
- 35 //把屏幕设备描述表复制到内存设备描述表中
- 36 if(bSave)
- 37 {
- 38 CDC dcCompatible;
- 39 dcCompatible.CreateCompatibleDC(CDC::FromHandle(hMemDC));
- 40 dcCompatible.SelectObject(m_pBitmap);
- 41 //将位图绘制到内存设备
- 42 BitBlt(hMemDC, 0, 0, nWidth, nHeight,
- 43 dcCompatible, nX, nY, SRCCOPY);
- 44 }
- 45 else
- 46 {
- 47 BitBlt(hMemDC, 0, 0, nWidth, nHeight,
- 48 hScrDC, nX, nY, SRCCOPY);
- 49 }
- 50 POINT ptCursor;
- 51 ::GetCursorPos(&ptCursor); //获取光标
- 52 ICONINFO IconInfo;
- 53 if (GetIconInfo(m_capCursor, &IconInfo))
- 54 {
- 55 ptCursor.x -= ((int) IconInfo.xHotspot);
- 56 ptCursor.y -= ((int) IconInfo.yHotspot);
- 57 if (IconInfo.hbmMask != NULL)
- 58 DeleteObject(IconInfo.hbmMask);
- 59 if (IconInfo.hbmColor != NULL)
- 60 DeleteObject(IconInfo.hbmColor);
- 61 }
- 62 //将光标绘制到相应的区域
- 63 DrawIconEx( hMemDC, ptCursor.x, ptCursor.y, m_capCursor, 0,0, 0,
- 64 NULL, DI_NORMAL | DI_COMPAT );
- 65 hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
- 66 DeleteDC(hScrDC); //释放DC
- 67 DeleteDC(hMemDC);
- 68 if(bSave)
- 69 {
- 70 if (OpenClipboard()) //保存到剪切板
- 71 {
- 72 EmptyClipboard(); //清空剪切板
- 73 //把屏幕内容粘贴到剪切板上, hBitmap 为刚才的屏幕位图句柄
- 74 SetClipboardData(CF_BITMAP, hBitmap);
- 75 CloseClipboard(); //关闭剪切板
- 76 }
- 77 }
- 78 return hBitmap;
- 79 }