MFC截图程序的实现(四)

时间:2022-02-04 17:02:08

  前面几篇文章介绍了如何进行窗口截图并保存至剪切板,现在将完整的代码贴出来:

主要是两个文件:( 完整源代码:http://download.csdn.net/detail/wwkaven/7487915 )

// 123Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "123.h"
#include "123Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


HWND My123Hwnd;
HWND cutWnd;


/////////////////////////////////////////////////////////////////////////////
// CMy123Dlg dialog

CMy123Dlg::CMy123Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy123Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy123Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy123Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy123Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy123Dlg, CDialog)
//{{AFX_MSG_MAP(CMy123Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy123Dlg message handlers

BOOL CMy123Dlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

m_pic.SubclassDlgItem(IDC_PIC,this); // 关联控件
My123Hwnd = m_hWnd; // 赋值

return TRUE; // return TRUE unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CMy123Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
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;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMy123Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}


void CMy123Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default

if (1 == nIDEvent) {
POINT pnt;
RECT rc;
HWND DeskHwnd = ::GetDesktopWindow(); //取得桌面句柄
HDC DeskDC = ::GetWindowDC(DeskHwnd); //取得桌面设备场景
int oldRop2 = SetROP2(DeskDC, R2_NOTXORPEN);
::GetCursorPos(&pnt); //取得鼠标坐标
HWND UnHwnd = ::WindowFromPoint(pnt) ; //取得鼠标指针处窗口句柄


::GetWindowRect(UnHwnd, &rc); //获得窗口矩形
cutWnd = UnHwnd;

if( rc.left < 0 ) rc.left = 0;
if (rc.top < 0 ) rc.top = 0;
HPEN newPen = ::CreatePen(PS_SOLID, 3, RGB(255, 0, 0)); //建立新画笔,载入DeskDC
HGDIOBJ oldPen = ::SelectObject(DeskDC, newPen);
::Rectangle(DeskDC, rc.left, rc.top, rc.right, rc.bottom); //在窗口周围显示闪烁矩形
Sleep(400); //设置闪烁时间间隔
::Rectangle( DeskDC, rc.left, rc.top, rc.right, rc.bottom);
::SetROP2(DeskDC, oldRop2);
::SelectObject( DeskDC, oldPen);
::DeleteObject(newPen);
::ReleaseDC( DeskHwnd, DeskDC);
DeskDC = NULL;
}

if (2 == nIDEvent)
{
if (m_pic.GetIsFinshed()) {
CopyBitmapToClipboard(FromHandle(cutWnd), TRUE);
}
}

CDialog::OnTimer(nIDEvent);
}


void CMy123Dlg::CopyBitmapToClipboard(CWnd *wnd, BOOL FullWnd)
{
CDC *dc;
if(FullWnd)
{
/* 抓取整个窗口*/
dc = new CWindowDC(wnd);
}
else
{
/* 仅抓取客户区时*/
dc = new CClientDC(wnd);
}

CDC memDC;
memDC.CreateCompatibleDC(dc);

CBitmap bm;
CRect r;
if(FullWnd)
wnd->GetWindowRect(&r);
else
wnd->GetClientRect(&r);

CString s;
wnd->GetWindowText(s);
CSize sz(r.Width(), r.Height());
bm.CreateCompatibleBitmap(dc, sz.cx, sz.cy);

CBitmap * oldbm = memDC.SelectObject(&bm);
memDC.BitBlt(0, 0, sz.cx, sz.cy, dc, 0, 0, SRCCOPY);
//直接调用OpenClipboard(),而不用wnd->GetParent()->OpenClipboard();
wnd->OpenClipboard();

::EmptyClipboard();
::SetClipboardData(CF_BITMAP, bm.m_hObject);
CloseClipboard();
//恢复原始环境
memDC.SelectObject(oldbm);
bm.Detach();
delete dc;

KillTimer(2);

// 加一句提示
MessageBox(_T("复制完成"));

}

// MyPic.cpp : implementation file
//

#include "stdafx.h"
#include "123.h"
#include "MyPic.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CMyPic

CMyPic::CMyPic()
{
}

CMyPic::~CMyPic()
{
}


BEGIN_MESSAGE_MAP(CMyPic, CStatic)
//{{AFX_MSG_MAP(CMyPic)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyPic message handlers

void CMyPic::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

SetCapture(); // 鼠标捕获
HCURSOR hc = LoadCursor(AfxGetApp()->m_hInstance, MAKEINTRESOURCE (IDC_CURSOR1));
//IDC_CURSOR1是靶形光标资源号
::SetCursor(hc);
HICON hicon2 = LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE (IDI_ICON2));
//IDI_ICON2为无靶图标资源号
this->SetIcon(hicon2);

FromHandle(My123Hwnd)->SetTimer(1,600,NULL);
IsFinshed = false;

CStatic::OnLButtonDown(nFlags, point);
}

void CMyPic::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

ReleaseCapture(); //释放鼠标捕获
HICON hicon1 = LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE (IDI_ICON1));
//IDI_ICON1是有靶图标资源号
this->SetIcon(hicon1);

IsFinshed = true;

FromHandle(My123Hwnd)->KillTimer(1);

FromHandle(My123Hwnd)->SetTimer(2, 600, NULL);

CStatic::OnLButtonUp(nFlags, point);
}

bool CMyPic::GetIsFinshed()
{
return IsFinshed;
}

但是,完成后效果并不是很好,因为win7Aero特效的原因,有些窗口截图失败,Ctrl + V后是一块黑,或是背景为一块黑,见图:

MFC截图程序的实现(四)

正常应该是这样的:

MFC截图程序的实现(四)

当然,大部分窗口是正常的:


MFC截图程序的实现(四)

MFC截图程序的实现(四)MFC截图程序的实现(四)MFC截图程序的实现(四)MFC截图程序的实现(四)