// ScreenCaptureDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "ScreenCapture.h"
#include "ScreenCaptureDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CScreenCaptureDlg 对话框
CScreenCaptureDlg::CScreenCaptureDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_SCREENCAPTURE_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CScreenCaptureDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CScreenCaptureDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CScreenCaptureDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CScreenCaptureDlg::OnBnClickedButton2)
END_MESSAGE_MAP()
// CScreenCaptureDlg 消息处理程序
BOOL CScreenCaptureDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CScreenCaptureDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CScreenCaptureDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CScreenCaptureDlg::OnBnClickedButton1()
{
// 全屏截图
CTime tm = CTime::GetCurrentTime();
CString str;
str.Format("%u", tm.GetTime());
str += ".bmp";
CFileDialog fDlg(FALSE, NULL, str, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT);
if (fDlg.DoModal() != IDOK)
{
return;
}
CString pathname = fDlg.GetPathName();
this->ShowWindow(SW_HIDE);//隐藏主界面
Sleep(250);
SaveScreenToFile(pathname);
this->ShowWindow(SW_SHOW);//显示主界面
}
void CScreenCaptureDlg::OnBnClickedButton2()
{
// 自定义截图
//// 截图
this->ShowWindow(SW_HIDE);//隐藏主界面
CoverDlg dlg;
if (IDCANCEL == dlg.DoModal())
{
if (!dlg.GetWhetherCancel())
{
POINT startPoint = dlg.GetStartPoint();
POINT endPoint = dlg.GetEndPoint();
CRect rect;
if (startPoint.x < endPoint.x)
{
rect.left = startPoint.x;
rect.bottom = endPoint.x;
}
else
{
rect.left = endPoint.x;
rect.bottom = startPoint.x;
}
if (startPoint.y < endPoint.y)
{
rect.top = endPoint.y;
rect.right = startPoint.y;
}
else
{
rect.top = startPoint.y;
rect.right = endPoint.y;
}
if (startPoint.x == endPoint.x)
{
MessageBox("请选择图片范围");
this->ShowWindow(SW_SHOW);//显示主界面
return;
}
if (startPoint.y == endPoint.y)
{
MessageBox("请选择图片范围");
this->ShowWindow(SW_SHOW);//显示主界面
return;
}
Sleep(250);
CTime tm = CTime::GetCurrentTime();
CString str;
str.Format("%u", tm.GetTime());
str += ".bmp";
CFileDialog fDlg(FALSE, NULL, str, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT);
if (fDlg.DoModal() != IDOK)
{
this->ShowWindow(SW_SHOW);//显示主界面
return;
}
CString pathname = fDlg.GetPathName();
CopyScreenToBitmap(rect, pathname);
}
}
this->ShowWindow(SW_SHOW);//显示主界面
}
/************************************************************************/
/* 将屏幕指定区域存成图片 */
/************************************************************************/
void CScreenCaptureDlg::CopyScreenToBitmap(CRect lpRect, CString pathname)
{
//获得整个屏幕的DC,并创建一个与之兼容的DC
HDC hScrDC = ::GetDC(NULL);//获取桌面DC
HDC hMemDC = NULL;
//the pointer will save all pixel point's color value
BYTE *lpBitmapBits = NULL;
int nWidth = lpRect.bottom - lpRect.left;//横
int nHeight = lpRect.top- lpRect.right;//竖
Sleep(300);
hMemDC = ::CreateCompatibleDC(hScrDC);
//initialise the struct BITMAPINFO for the bimap infomation,
//in order to use the function CreateDIBSection
// on wince os, each pixel stored by 24 bits(biBitCount=24)
//and no compressing(biCompression=0)
BITMAPINFO bi;
ZeroMemory(&bi, sizeof(BITMAPINFO));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = nWidth;
bi.bmiHeader.biHeight = nHeight;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = GetDeviceCaps(hMemDC, BITSPIXEL);//16;//24;
//use the function CreateDIBSection and SelectObject
//in order to get the bimap pointer : lpBitmapBits
HBITMAP bitmap = ::CreateDIBSection(hMemDC, &bi, DIB_RGB_COLORS, (LPVOID*)&lpBitmapBits, NULL, 0);
HGDIOBJ oldbmp = ::SelectObject(hMemDC, bitmap);
// copy the screen dc to the memory dc
BitBlt(hMemDC, 0, 0, nWidth, nHeight/*横*/, hScrDC, lpRect.left, lpRect.right, SRCCOPY);//选择左上角的点和长宽
//if you only want to get the every pixel color value,
//you can begin here and the following part of this function will be unuseful;
//the following part is in order to write file;
////////////////////////////////////////////////////////////////////
//bimap file header in order to write bmp file //bimap file header in order to write bmp file
BITMAPFILEHEADER bh;
ZeroMemory(&bh, sizeof(BITMAPFILEHEADER));
bh.bfType = 0x4d42; //bitmap
bh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bh.bfSize = bh.bfOffBits + ((nWidth*nHeight) * bi.bmiHeader.biBitCount / 8);//2==bi.bmiHeader.biBitCount/8
//write into file
CFile file;
if (file.Open(pathname, CFile::modeCreate | CFile::modeWrite))
{
file.Write(&bh, sizeof(BITMAPFILEHEADER));
file.Write(&(bi.bmiHeader), sizeof(BITMAPINFOHEADER));
file.Write(lpBitmapBits, bi.bmiHeader.biBitCount / 8 * nWidth * nHeight);//2==bi.bmiHeader.biBitCount/8
file.Close();
}
::SelectObject(hMemDC, oldbmp);
::DeleteObject(bitmap);
::DeleteObject(hMemDC);
::ReleaseDC(NULL, hScrDC);
}
vs使用mfc实现全屏截屏和自定义区域截屏(修改)
5星 · 超过95%的资源 需积分: 50 122 浏览量
2017-05-11
13:12:34
上传
评论 2
收藏 235KB ZIP 举报
songqiu65
- 粉丝: 16
- 资源: 5
最新资源
- mosquitto-2.018-install-windows-x64
- FTPServer FTP 服务器,绿色免安装,单文件
- 梦畅语音点名软件,上课点名
- 利用ADNI数据集和标签,在tensorflow框架上使用tensorlayer接口,通过架构u-net实现海马体的分割
- Kutools for Word v9.0 office word 插件
- 修复Windows 10 LTSC 2021资源占用率高
- Hash工具,小巧绿色hash校验工具,免费hash工具
- 重启进行BIOS快捷方式,不需要开机按BIOS键
- 鸭子开车记(儿童绘本)
- 威纶通触摸屏编程软件Easy builder pro V6.09.01.556安装包(2024.04).txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈