⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copywnddlg.cpp

📁 vc程序设计技巧与实例中关于多媒体技术的vc++原代码
💻 CPP
字号:
// CopyWndDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CopyWnd.h"
#include "CopyWndDlg.h"

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

void toClipboard(CWnd * pWnd, BOOL bFullWnd);

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

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

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCopyWndDlg dialog

CCopyWndDlg::CCopyWndDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCopyWndDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCopyWndDlg)
		// 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 CCopyWndDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCopyWndDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCopyWndDlg, CDialog)
	//{{AFX_MSG_MAP(CCopyWndDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_FULLWND, OnFullWnd)
	ON_BN_CLICKED(IDC_CLIENTONLY, OnClientonly)
	ON_BN_CLICKED(IDC_CAPTURE, OnCapture)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCopyWndDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	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);
		}
	}

	// 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_bFullWnd = TRUE;

	//选中【捕捉整个窗体】单选按钮
	CButton* pBtn = (CButton*)GetDlgItem(IDC_FULLWND);
	pBtn->SetCheck(1);

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

void CCopyWndDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CCopyWndDlg::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 CCopyWndDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/**********************************************************************
函数名:toClipboard
用途:  拷贝当前窗体或者客户区的内容到剪切板
参数:  pWnd[in],指向一个CWnd对象指针,这个窗口的内容要被发送到剪切板;
		bFullWnd[in],如果该参数为TRUE,则整个窗口的内容都会被送到剪切板
		如果该参数为FALSE,则只有客户区的内容被送到剪切板
返回值:
**********************************************************************/
void toClipboard(CWnd * pWnd, BOOL bFullWnd)
{
	CDC dc;
	if(bFullWnd)      //捕捉整个窗体内容
	{
		//获得窗体的DC
		HDC hdc = ::GetWindowDC(pWnd ->m_hWnd);
		//绑定到一个CDC对象
		dc.Attach(hdc);
	} 
	else             //只捕捉客户区内容
	{ 
		//获得客户区DC
		HDC hdc = ::GetDC(pWnd->m_hWnd);
		//绑定到一个CDC对象
		dc.Attach(hdc);
	} 
	
	CDC memDC;
	memDC.CreateCompatibleDC(&dc);
	
	CBitmap bm;
	CRect rect;
	if(bFullWnd)
        pWnd->GetWindowRect(&rect);       //获得窗口矩形区域
	else 
		pWnd->GetClientRect(&rect);       //获得客户区矩形区域
	
	CSize sz(rect.Width(), rect.Height());
	//根据dc创建CBitmap对象
	bm.CreateCompatibleBitmap(&dc, sz.cx, sz.cy);

	CBitmap * oldbm = memDC.SelectObject(&bm);
	memDC.BitBlt(0, 0, sz.cx, sz.cy, &dc, 0, 0, SRCCOPY);
	
	//打开剪切板
	pWnd->OpenClipboard();
	//清空剪切板数据
	::EmptyClipboard();
	//把位图的内容拷贝到剪切板
	::SetClipboardData(CF_BITMAP, bm.m_hObject);
	//关闭剪切板
	CloseClipboard();
	memDC.SelectObject(oldbm);
	bm.Detach();  
}

void CCopyWndDlg::OnFullWnd() 
{
	m_bFullWnd = TRUE;
}

void CCopyWndDlg::OnClientonly() 
{
	m_bFullWnd = FALSE;	
}

void CCopyWndDlg::OnCapture() 
{
	toClipboard(this,m_bFullWnd);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -