弹出对话框源代码中抓取活动窗口图片

来源:互联网 发布:淘宝限制发布的 编辑:程序博客网 时间:2024/05/09 04:55
 

void CAppsnapApp::OnHelpTest()
{
 // TODO: Add your command handler code here
 CBmpSnapTest testDlg;
 testDlg.DoModal();
}

//////////////////////////////////////////////////////////////////////////////////////////////

BmpSnapTest.cpp

//////////////////////////////////////////////////////////////////////////////////////////////

// BmpSnapTest.cpp : implementation file
//

#include "stdafx.h"
#include "appsnap.h"
#include "BmpSnapTest.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBmpSnapTest dialog


CBmpSnapTest::CBmpSnapTest(CWnd* pParent )
 : CDialog(CBmpSnapTest::IDD, pParent)
{
 //{{AFX_DATA_INIT(CBmpSnapTest)
  // NOTE: the ClassWizard will add member initialization here
 //}}AFX_DATA_INIT
 m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}


void CBmpSnapTest::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CBmpSnapTest)
 DDX_Control(pDX, IDC_STATIC_PICTURE, m_bmpShow);
 //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBmpSnapTest, CDialog)
 //{{AFX_MSG_MAP(CBmpSnapTest)
 ON_WM_PAINT()
 ON_BN_CLICKED(IDC_BUTTON_SNAP, OnButtonSnap)
 ON_WM_QUERYDRAGICON()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////////////////////
#define OVEREDGE 5
BOOL CBmpSnapTest::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
 //SetIcon(::LoadIcon(NULL,(LPCTSTR)IDI_ICON1),FALSE);
 //SetIcon(::LoadIcon(NULL,(LPCTSTR)IDI_ICON1),TRUE);
 // TODO: Add extra initialization here
 // TODO: Add extra initialization here
 // 定义热键
 RegisterHotKey(m_hWnd,1688,MOD_CONTROL | MOD_SHIFT,'A' );
    RegisterHotKey(m_hWnd,2688,MOD_CONTROL | MOD_SHIFT,'a' );  

 // 初始化内存DC
 CDC * pDC = m_bmpShow.GetDC();
 m_memDC.CreateCompatibleDC(pDC);

 // 初始化位图对象
 m_bmpShow.GetWindowRect(&m_rcShow);
 m_memBMP.CreateCompatibleBitmap(pDC, m_rcShow.Width() - OVEREDGE,
  m_rcShow.Height() - OVEREDGE);
 m_memDC.SelectObject(&m_memBMP);
 m_bmpShow.ReleaseDC(pDC);

 // 初始化对话框中用于显示的图像控件区域
 m_memDC.BitBlt(0,0,m_rcShow.Width() - OVEREDGE,
  m_rcShow.Height() - OVEREDGE,NULL,0,0,WHITENESS);
 return TRUE;  // return TRUE  unless you set the focus to a control
}

//////////////////////////////////////////////////////////////////////////
void CBmpSnapTest::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();
  m_bmpShow.GetWindowRect(&m_rcShow);
  CDC *pShowDC = m_bmpShow.GetDC();
  pShowDC->BitBlt(0, 0, m_rcShow.Width(), m_rcShow.Height(), &m_memDC, 0, 0, SRCCOPY);
  m_bmpShow.ReleaseDC(pShowDC);
 }
}

/////////////////////////////////////////////////////////////////////////////
// CBmpSnapTest message handlers

void CBmpSnapTest::OnButtonSnap()
{
 // TODO: Add your control notification handler code here
 //AfxMessageBox(_T("SNAP OK!"));
 RECT rect;
 m_bmpShow.GetWindowRect(&rect);

 // 获得活动窗体指针
 //CWnd *pTarWnd = this->GetActiveWindow();

 // 获取整个屏幕窗口指针
 CWnd *pTarWnd = this->GetDesktopWindow();

 // 获取WINDOWS窗口处于活动的窗口指针
 //CWnd *pTarWnd = this->GetForegroundWindow();

 // 获得活动窗体的设备上下文指针
 CDC  *pTarDC  = pTarWnd->GetWindowDC();

 // 获得活动窗体矩形
 CRect rcTar;
 pTarWnd->GetWindowRect(&rcTar);
 m_bmpShow.GetWindowRect(&m_rcShow);

 // 将活动窗体的图像复制到图像控件中
 m_memDC.StretchBlt(0,0,m_rcShow.Width(),m_rcShow.Height(),pTarDC,
    0,0,rcTar.Width(),rcTar.Height(),SRCCOPY);

 // 释放设备上下文指针
 pTarWnd->ReleaseDC(pTarDC);
 
 // 重绘图像控件区域
 this->InvalidateRect(&m_rcShow);
 
}
/////////////////////////////////////////////////////////////////
LRESULT CBmpSnapTest::OnHotKey(WPARAM wParam, LPARAM lParam)
{
    if (wParam == 1688 || wParam == 2688)
 {
  OnButtonSnap();
 }

 return 0;
}

void CBmpSnapTest::OnDestroy()
{
 CDialog::OnDestroy();
 
 // TODO: Add your message handler code here
 UnregisterHotKey(m_hWnd,1688);
    UnregisterHotKey(m_hWnd,2688);
}
/////////////////////////////////////////////////////////////////
void CBmpSnapTest::OnOK()
{
 // TODO: Add extra validation here
 AfxMessageBox(_T("SNAP OK!"));
 //CDialog::OnOK();
}

/////////////////////////////////////////////////////////////////////////////////////////////

BmpSnapTest.h

/////////////////////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_BMPSNAPTEST_H__53C7BAA1_357C_44E7_BC10_85D23212600B__INCLUDED_)
#define AFX_BMPSNAPTEST_H__53C7BAA1_357C_44E7_BC10_85D23212600B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// BmpSnapTest.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CBmpSnapTest dialog

class CBmpSnapTest : public CDialog
{
// Construction
public:
 CBmpSnapTest(CWnd* pParent = NULL);   // standard constructor

// Dialog Data
 //{{AFX_DATA(CBmpSnapTest)
 enum { IDD = IDD_DIALOG1 };
 CStatic m_bmpShow;
 //}}AFX_DATA


// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CBmpSnapTest)
 public:
 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation
protected:
 HICON m_hIcon;

 // Generated message map functions
 //{{AFX_MSG(CBmpSnapTest)
 virtual BOOL OnInitDialog();
 afx_msg void OnPaint();
 afx_msg void OnButtonSnap();
 virtual void OnOK();
 afx_msg void OnDestroy();
 afx_msg LRESULT OnHotKey(WPARAM wParam,LPARAM lParam);
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
private:
 CRect m_rcShow;
 CDC m_memDC;
 CBitmap m_memBMP;
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_BMPSNAPTEST_H__53C7BAA1_357C_44E7_BC10_85D23212600B__INCLUDED_)

原创粉丝点击