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

📄 getpicdlg.cpp

📁 这是书上的代码
💻 CPP
字号:
// GetPicDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GetPic.h"
#include "GetPicDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CGetPicDlg dialog

CGetPicDlg::CGetPicDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGetPicDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGetPicDlg)
	m_left = 0;
	m_right = 300;
	m_top = 0;
	m_bottom = 300;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGetPicDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGetPicDlg)
	DDX_Control(pDX, IDC_PIC, m_pic);
	DDX_Text(pDX, IDC_LEFT, m_left);
	DDV_MinMaxInt(pDX, m_left, 0, 400);
	DDX_Text(pDX, IDC_RIGHT, m_right);
	DDV_MinMaxInt(pDX, m_right, 0, 400);
	DDX_Text(pDX, IDC_TOP, m_top);
	DDV_MinMaxInt(pDX, m_top, 0, 400);
	DDX_Text(pDX, IDC_BOTTOM, m_bottom);
	DDV_MinMaxInt(pDX, m_bottom, 0, 400);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGetPicDlg, CDialog)
	//{{AFX_MSG_MAP(CGetPicDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_KILLFOCUS(IDC_BOTTOM, OnKillfocusBottom)
	ON_EN_KILLFOCUS(IDC_LEFT, OnKillfocusLeft)
	ON_EN_KILLFOCUS(IDC_RIGHT, OnKillfocusRight)
	ON_EN_KILLFOCUS(IDC_TOP, OnKillfocusTop)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGetPicDlg message handlers

BOOL CGetPicDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CGetPicDlg::OnOK() 
{
	// TODO: Add extra validation here
	LPRECT temprect;
	HBITMAP tempmap;

	//假设要截图的区域为(0,0)至(300,400),具体实践中可由用户选择
	temprect = new RECT();
	temprect->bottom = m_bottom;
	temprect->left = m_left;
	temprect->right = m_right;
	temprect->top = m_top;

	tempmap = Copybitmap(temprect);
	//拷贝到剪贴板
	Toclip(tempmap);
	//显示获取的屏幕
	m_pic.SetBitmap(tempmap);
	delete temprect;

}
//把选定的屏幕区域拷贝到位图中的函数
//lprect 代表选定区域
HBITMAP CGetPicDlg::Copybitmap(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("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;
}	
//把屏幕内容粘贴到剪贴板上
void CGetPicDlg::Toclip(HBITMAP hbitmap)
{
	
	if (::OpenClipboard(m_hWnd)) //hwnd为程序窗口句柄
	{
		//清空剪贴板
		EmptyClipboard();
		//把屏幕内容粘贴到剪贴板上,hbitmap 为刚才的屏幕位图句柄
		SetClipboardData(CF_BITMAP, hbitmap);
		//关闭剪贴板
		CloseClipboard();
	}
}



void CGetPicDlg::OnKillfocusBottom() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
}

void CGetPicDlg::OnKillfocusLeft() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
}

void CGetPicDlg::OnKillfocusRight() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
}

void CGetPicDlg::OnKillfocusTop() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
}

⌨️ 快捷键说明

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