MFC截图程序的实现(七)

时间:2021-11-23 17:02:13

窗口截图程序已经基本实现了,功能也比较完整。下面是界面图:

MFC截图程序的实现(七)

说明:

1、默认的保存位置在C盘根目录,点击按钮可以弹出文件夹选择对话框修改保存截图的位置。

2、Edit控件是禁用的,相当于只读,功能是显示当前的保存路径。

3、用户拖动小窗口的靶子,至截图窗口释放,自动保存截图至设置的文件夹。文件名为当前的时间,例如当前时间为2014年6月12日22点2分14秒,则保存的文件名为“2014061220214.bmp”。

4、贴出主要的源代码(完整源代码:http://download.csdn.net/detail/wwkaven/7490769):

// 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;
CRect cutRc;


/////////////////////////////////////////////////////////////////////////////
// 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()
ON_BN_CLICKED(IDC_BTN_SAVEPATH, OnBtnSavepath)
//}}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; // 赋值

savePath = _T("C:");
GetDlgItem(IDC_EDIT_SAVEPATH)->SetWindowText(savePath);

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;
/*
int offset = 6;
rc.left -= offset;
rc.top -= offset;
rc.right += offset;
rc.bottom += offset;
*/
cutRc = rc;

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);
//CopyBitmapToClipboard(CopyScreenToBitmap((LPRECT)&cutRc));
CString savaFullPath = savePath + _T("\\") + GetNowTime(_T("%Y%m%d%H%M%S")) + _T(".bmp");
SaveBitmapToFile(CopyScreenToBitmap((LPRECT)&cutRc), savaFullPath);
}
}

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("复制完成"));

}

/////////////////////////////////////////////////////////////
// 功能:将HBITMAP对象复制到剪切板
// 参数:要复制的HBITMAP对象
////////////////////////////////////////////////////////////
void CMy123Dlg::CopyBitmapToClipboard(HBITMAP hBitmap)
{
KillTimer(2);

OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();

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

}

///////////////////////////////////////////////////////////////////////
// 功能:将屏幕的某个矩形区域的图像复制到一个位图对象中
// 参数:输入一个确定矩形范围的LPRECT型数据
// 返回:一个存储了截图的bitmap句柄
///////////////////////////////////////////////////////////////////////
HBITMAP CMy123Dlg::CopyScreenToBitmap(LPRECT lpRect)
{
HDC hScrDC, hMemDC;
// 屏幕和内存设备描述表
HBITMAP hBitmap, hOldBitmap;
// 位图句柄
int nX, nY, nX2, nY2;
// 选定区域坐标
int nWidth, nHeight;
// 位图宽度和高度
int xScrn, yScrn;
// 屏幕分辨率

// 确保选定区域不为空矩形
if (IsRectEmpty(lpRect))
return NULL;

//为屏幕创建设备描述表
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
//为屏幕设备描述表创建兼容的内存设备描述表
hMemDC = CreateCompatibleDC(hScrDC);
// 获得选定区域坐标
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;
}


////////////////////////////////////////////////////////////////////////////////////////////////
// 功能:将截取的图像存储为文件
// 参数1:HBITMAP对象
// 参数2:文件名(路径+xx.bmp)
////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CMy123Dlg::SaveBitmapToFile(HBITMAP hBitmap, LPCSTR lpFileName)
{
HDC hDC; //设备描述表
int iBits; //当前显示分辨率下每个像素所占字节数
WORD wBitCount; //位图中每个像素所占字节数
DWORD dwPaletteSize=0, //定义调色板大小, 位图中像素字节大小 ,位图文件大小 , 写入文件字节数
dwBmBitsSize,
dwDIBSize, dwWritten;
BITMAP Bitmap; //位图属性结构
BITMAPFILEHEADER bmfHdr; //位图文件头结构
BITMAPINFOHEADER bi; //位图信息头结构
LPBITMAPINFOHEADER lpbi; //指向位图信息头结构

HANDLE fh, hDib, hPal,hOldPal=NULL; //定义文件,分配内存句柄,调色板句柄

//计算位图文件每个像素所占字节数
HDC hWndDC = CreateDC(_T("DISPLAY"),NULL,NULL,NULL);
hDC = ::CreateCompatibleDC( hWndDC ) ;
iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);

if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else if (iBits <= 24)
wBitCount = 24;
else
wBitCount = 24 ;

//计算调色板大小
if (wBitCount <= 8)
dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD);

//设置位图信息头结构
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;

dwBmBitsSize = ((Bitmap.bmWidth * wBitCount+31)/32) * 4 * Bitmap.bmHeight ;

//为位图内容分配内存
hDib = GlobalAlloc(GHND,dwBmBitsSize+dwPaletteSize+sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;

// 处理调色板
hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal)
{
hDC = ::GetDC(NULL);
hOldPal = ::SelectPalette(hDC, (HPALETTE)hPal, FALSE);
RealizePalette(hDC);
}

// 获取该调色板下新的像素值
GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight,
(LPSTR)lpbi + sizeof(BITMAPINFOHEADER)
+dwPaletteSize,
(LPBITMAPINFO )
lpbi, DIB_RGB_COLORS);

//恢复调色板
if (hOldPal)
{
SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}

//创建位图文件
fh = CreateFile(lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);

if (fh == INVALID_HANDLE_VALUE)
return FALSE;

// 设置位图文件头
bmfHdr.bfType = 0x4D42; // "BM"
dwDIBSize = sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER)
+ dwPaletteSize + dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)
+ (DWORD)sizeof(BITMAPINFOHEADER)
+ dwPaletteSize;

// 写入位图文件头
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);

// 写入位图文件其余内容
WriteFile(fh, (LPSTR)lpbi, dwDIBSize,
&dwWritten, NULL);

//清除
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);

KillTimer(2);
return TRUE;
}



void CMy123Dlg::OnBtnSavepath()
{
// TODO: Add your control notification handler code here
savePath = GetSavePath();
GetDlgItem(IDC_EDIT_SAVEPATH)->SetWindowText(savePath);
//GetNowTime(_T("%Y年%m月%d日%H时%M分%S秒"));
}


///////////////////////////////////////////////////////////////////////////
// 功能:弹出一个文件夹选择对话框
// 返回:用户选择的文件夹路径
//////////////////////////////////////////////////////////////////////////
LPCSTR CMy123Dlg::GetSavePath()
{
#define BIF_NEWDIALOGSTYLE 0x00000040 // 因为vc6.0不支持BIF_NEWDIALOGSTYLE,vs2003以上无此问题(此句可删掉)

TCHAR Buffer[MAX_PATH];
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
bi.hwndOwner = NULL;
bi.ulFlags = BIF_RETURNONLYFSDIRS; //要求返回文件系统的目

bi.ulFlags=BIF_NEWDIALOGSTYLE; //窗口可以调整大小,有新建文件夹按钮

bi.pszDisplayName = Buffer; //此参数如为NULL则不能显示对话框
bi.lpszTitle = _T("请选择文件夹");
bi.lpfn = NULL;
bi.iImage=IDR_MAINFRAME;

LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
if(pIDList)
{
SHGetPathFromIDList(pIDList, Buffer);
//取得文件夹路径到Buffer里

LPCSTR temp = Buffer;
return temp;
}
else
{
return _T(""); // 用户点了取消
}

LPMALLOC lpMalloc;
if(FAILED(SHGetMalloc(&lpMalloc)))
return _T("");

//释放内存
lpMalloc->Free(pIDList);
lpMalloc->Release();
}

///////////////////////////////////////////////////////////////
// 功能: 通过CTime类获取当前时间
// 参数:格式化样式参数
// 返回:相应的格式化后的日期
///////////////////////////////////////////////////////////////
CString CMy123Dlg::GetNowTime(CString formatStr)
{
CTime m_time;

m_time=CTime::GetCurrentTime(); //获取当前时间日期

return m_time.Format(formatStr);
}

// MyPic.cpp : implementation file//#include "stdafx.h"#include "123.h"#include "MyPic.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CMyPicCMyPic::CMyPic(){}CMyPic::~CMyPic(){}BEGIN_MESSAGE_MAP(CMyPic, CStatic)	//{{AFX_MSG_MAP(CMyPic)	ON_WM_LBUTTONDOWN()	ON_WM_LBUTTONUP()	//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMyPic message handlersvoid 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;}