抓屏并保存为BMP文件Dlg.cpp
上传用户:shibojindu
上传日期:2013-06-09
资源大小:37k
文件大小:3k
- // 抓屏并保存为BMP文件Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "抓屏并保存为BMP文件.h"
- #include "抓屏并保存为BMP文件Dlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CBMPDlg dialog
- CBMPDlg::CBMPDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CBMPDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CBMPDlg)
- m_tip = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CBMPDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CBMPDlg)
- DDX_Text(pDX, IDC_EDIT1, m_tip);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CBMPDlg, CDialog)
- //{{AFX_MSG_MAP(CBMPDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON1, OnCaptureSave)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CBMPDlg message handlers
- BOOL CBMPDlg::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
-
- 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 CBMPDlg::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 CBMPDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CBMPDlg::OnCaptureSave()
- {
- // TODO: Add your control notification handler code here
- //获得输入的保存路径
- CFileDialog savePath(FALSE ,NULL,"save.bmp",OFN_HIDEREADONLY,"Bitmap Files(*.bmp)|*.bmp||",NULL);
- savePath.m_ofn.lpstrTitle="请选择保存路径并输入文件名";
- if(savePath.DoModal()==IDOK)
- {
- CString fullName;
- fullName=savePath.GetPathName();
-
- m_tip="屏幕成功拷贝";
- UpdateData(FALSE);
- //获得屏幕窗口的对象句柄
- CWnd *pWnd=GetDesktopWindow();
- //对话框窗口最小化,使截到的屏幕不包含对话框窗口
- this->ShowWindow(SW_SHOWMINIMIZED);
- //将屏幕窗口拷贝到DIB位图中
- HANDLE hDib=DIBFromWindow(pWnd,NULL);
- CFile file;
- file.Open(fullName,CFile::modeCreate|CFile::modeWrite);
- SaveToFile(hDib,file);//将位图存为指定名字的文件
- file.Close();
- GlobalFree(hDib);
- }
- }