1、 屏幕抓图程序代码说明:1.建立基于 MFC 的单文档应用程序2增加菜单项抓图3向视图类添加变量: HDC m_hdcCompatible;/与屏幕兼容设备HBITMAP m_hbmpScreen;/屏幕抓图的位图句柄/ Capture123View.h : CCapture123View 类的接口/#pragma onceclass CCapture123View : public CScrollViewprotected: / 仅从序列化创建CCapture123View();DECLARE_DYNCREATE(CCapture123View)/ 属性public:CCapture123D
2、oc* GetDocument() const;/ 操作public:/ 重写public:virtual void OnDraw(CDC* pDC); / 重写以绘制该视图virtual BOOL PreCreateWindow(CREATESTRUCTprotected:virtual void OnInitialUpdate(); / 构造后第一次调用virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void
3、 OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);/ 实现public:virtual CCapture123View();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext#endifprotected:/ 生成的消息映射函数protected:DECLARE_MESSAGE_MAP()public:HDC m_hdcCompatible;/与屏幕兼容设备HBITMAP m_hbmpScreen;/屏幕抓图的位图句柄/菜单项抓图处理函数afx_msg v
4、oid OnGrabscreen();#ifndef _DEBUG / Capture123View.cpp 中的调试版本inline CCapture123Doc* CCapture123View:GetDocument() const return reinterpret_cast(m_pDocument); #endif/ Capture123View.cpp : CCapture123View 类的实现/#include “stdafx.h“#include “Capture123.h“#include “Capture123Doc.h“#include “Capture123View
5、.h“#ifdef _DEBUG#define new DEBUG_NEW#endif/ CCapture123ViewIMPLEMENT_DYNCREATE(CCapture123View, CScrollView)BEGIN_MESSAGE_MAP(CCapture123View, CScrollView)/ 标准打印命令ON_COMMAND(ID_FILE_PRINT, / CCapture123View 绘制void CCapture123View:OnDraw(CDC* pDC)CCapture123Doc* pDoc = GetDocument();ASSERT_VALID(pDo
6、c);if (!pDoc)return;/关键代码SelectObject(pDC-m_hDC,m_hbmpScreen);BitBlt(pDC-m_hDC,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),m_hdcCompatible,0,0,SRCCOPY);/ TODO: 在此处为本机数据添加绘制代码void CCapture123View:OnInitialUpdate()CScrollView:OnInitialUpdate();CSize sizeTotal;/ TODO: 计算此视图的合计大小size
7、Total.cx=GetSystemMetrics(SM_CXSCREEN);sizeTotal.cy=GetSystemMetrics(SM_CYSCREEN);/*sizeTotal.cx = sizeTotal.cy = 100;*/SetScrollSizes(MM_TEXT, sizeTotal);/ CCapture123View 打印BOOL CCapture123View:OnPreparePrinting(CPrintInfo* pInfo)/ 默认准备return DoPreparePrinting(pInfo);void CCapture123View:OnBeginPr
8、inting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 添加额外的打印前进行的初始化过程void CCapture123View:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 添加打印后进行的清理过程/ CCapture123View 诊断#ifdef _DEBUGvoid CCapture123View:AssertValid() constCScrollView:AssertValid();void CCapture123View:Dump(CDumpContextCCapt
9、ure123Doc* CCapture123View:GetDocument() const / 非调试版本是内联的ASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CCapture123Doc);return (CCapture123Doc*)m_pDocument;#endif /_DEBUG/ CCapture123View 消息处理程序void CCapture123View:OnGrabscreen()HDC hdcScreen=:GetDC(NULL);m_hdcCompatible=CreateCompatibleDC(hdcScreen);m_
10、hbmpScreen=CreateCompatibleBitmap(hdcScreen,GetDeviceCaps(hdcScreen,HORZRES),GetDeviceCaps(hdcScreen,VERTRES);SelectObject(m_hdcCompatible,m_hbmpScreen);BitBlt(m_hdcCompatible,0,0,GetDeviceCaps(hdcScreen,HORZRES),GetDeviceCaps(hdcScreen,VERTRES),hdcScreen,0,0,SRCCOPY);HCURSOR hCursor=GetCursor();POI
11、NT ptCursor;GetCursorPos(ICONINFO IconInfo;if (GetIconInfo(hCursor,ptCursor.y-=(int)IconInfo.yHotspot);if (IconInfo.hbmMask!=NULL)DeleteObject(IconInfo.hbmMask);if (IconInfo.hbmColor!=NULL)DeleteObject(IconInfo.hbmColor);DrawIconEx(m_hdcCompatible,ptCursor.x,ptCursor.y,hCursor,0,0,0,NULL,DI_NORMAL|DI_COMPAT);Invalidate();/ TODO: 在此添加命令处理程序代码