实现屏幕拷贝功能:
1、 定义设备绘图句柄,内存绘图句柄,并使用函数初始化
HDC hscrdc,hmemdc;
hscrdc=CreateDCW(L"DISPLAY",NULL,NULL,NULL);
if(hscrdc==NULL)
{
MessageBox(L"create dc error");
return hbitmap;
}
hmemdc=CreateCompatibleDC(hscrdc);
if(hmemdc==NULL)
{
MessageBox(L"create dc error");
return hbitmap;
}
2、
定义位图句柄,并用函数初始化
HBITMAP hbitmap,holdbitmap;
//为了保证所截取的屏幕可见,应该获得屏幕的分辨率
xscrn=GetDeviceCaps(hscrdc,HORZRES);
yscrn=GetDeviceCaps(hscrdc,VERTRES);
//然后要通过分辨率来设置nheight,nwidth的值
int nx=0,ny=0,nx2=0,ny2=0;
if
(nx<0)
nx=0;
if(ny<0)
ny=0;
if(nx2>xscrn)
nx2=xscrn;
if(ny2>yscrn)
ny2=yscrn;
nwidth=nx2-nx;
nheight=ny2-ny;
hbitmap=CreateCompatibleBitmap(hscrdc,nwidth,nheight);
holdbitmap=(HBITMAP)SelectObject(hmemdc,hbitmap);//完成了
3、
完成图像从屏幕句柄到内存句柄的拷贝
BitBlt(hmemdc,0,0,nwidth,nheight,hscrdc,nx,ny,SRCCOPY);
下面是完整代码
// zhuapingDlg.cpp : 实现文件
//
#include
"stdafx.h"
#include
"zhuaping.h"
#include
"zhuapingDlg.h"
#ifdef
_DEBUG
#define
new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的CAboutDlg 对话框
class
CAboutDlg : public CDialog
{
public
:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected
:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void
CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CzhuapingDlg 对话框
CzhuapingDlg::CzhuapingDlg(CWnd* pParent /*=NULL*/)
: CDialog(CzhuapingDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void
CzhuapingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_STATIC1, m_Pic);
}
BEGIN_MESSAGE_MAP(CzhuapingDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON2, &CzhuapingDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON1, &CzhuapingDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
// CzhuapingDlg 消息处理程序
BOOL CzhuapingDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回TRUE
}
void
CzhuapingDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的MFC 应用程序,
// 这将由框架自动完成。
void
CzhuapingDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR CzhuapingDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void
CzhuapingDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
OnOK();
}
void
CzhuapingDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
LPRECT lprect;
HBITMAP tempmap;
lprect=new RECT();
lprect->top=0;
lprect->left=0;
lprect->right=1000;
lprect->bottom=700;
tempmap=Copybitmap(lprect);
m_Pic.SetBitmap(tempmap);
delete lprect;
}
// //把选定的屏幕区域拷贝到位图
HBITMAP CzhuapingDlg::Copybitmap(LPRECT lprect)
{
HBITMAP hbitmap,holdbitmap;
HDC hscrdc,hmemdc;
int nwidth,nheight;
int nx,ny,nx2,ny2;
int xscrn,yscrn;
if(IsRectEmpty(lprect))
{
return NULL;
}
hscrdc=CreateDCW(L"DISPLAY",NULL,NULL,NULL);
if(hscrdc==NULL)
{
MessageBox(L"create dc error");
return hbitmap;
}
hmemdc=CreateCompatibleDC(hscrdc);
if(hmemdc==NULL)
{
MessageBox(L"create dc error");
return hbitmap;
}
nx=lprect->left;
ny=lprect->top;
nx2=lprect->right;
ny2=lprect->bottom;
xscrn=GetDeviceCaps(hscrdc,HORZRES);
yscrn=GetDeviceCaps(hscrdc,VERTRES);
if(nx<0)
nx=0;
if(ny<0)
ny=0;
if(nx2>xscrn)
nx2=xscrn;
if(ny2>yscrn)
ny2=yscrn;
nwidth=nx2-nx;
nheight=ny2-ny;
hbitmap=CreateCompatibleBitmap(hscrdc,nwidth,nheight);
holdbitmap=(HBITMAP)SelectObject(hmemdc,hbitmap);
BitBlt(hmemdc,0,0,nwidth,nheight,hscrdc,nx,ny,SRCCOPY);
hbitmap=(HBITMAP)SelectObject(hmemdc,holdbitmap);
DeleteDC(hscrdc);
DeleteDC(hmemdc);
return hbitmap;
}