#include "pch.h"
#include "MFC截图工具.h"
#include "afxdialogex.h"
#include "CutPictureDlg.h"
// CutPictureDlg 对话框
IMPLEMENT_DYNAMIC(CutPictureDlg, CDialogEx)
CutPictureDlg::CutPictureDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_DIALOG_CUTPIC, pParent)
{
m_traker.m_nStyle = CRectTracker::dottedLine | CRectTracker::resizeInside;
m_traker.m_nHandleSize = 5;
m_traker.m_rect.SetRect(0,0,0,0);
width = GetSystemMetrics(SM_CXSCREEN);
hight = GetSystemMetrics(SM_CYSCREEN);
CDC *pDc=GetDesktopWindow()->GetDC();
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDc, width,hight);
m_memDc.CreateCompatibleDC(pDc);
m_memDc.SelectObject(&bmp);
m_memDc.BitBlt(0,0, width, hight,pDc,0,0,SRCCOPY);
pDc->DeleteDC();
bmp.DeleteObject();
}
CutPictureDlg::~CutPictureDlg()
{
}
void CutPictureDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CutPictureDlg, CDialogEx)
ON_WM_LBUTTONDOWN()
ON_WM_PAINT()
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()
// CutPictureDlg 消息处理程序
void CutPictureDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_traker.HitTest(point)<0) {
m_traker.TrackRubberBand(this,point,TRUE);
m_traker.m_rect.NormalizeRect();
}
else {
m_traker.Track(this,point,TRUE);
}
Invalidate(FALSE);
CDialogEx::OnLButtonDown(nFlags, point);
}
void CutPictureDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.BitBlt(0,0,width,hight,&m_memDc,0,0,SRCCOPY);
m_traker.Draw(&dc);
}
void CutPictureDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
int wid = m_traker.m_rect.Width();
int hei = m_traker.m_rect.Height();
CBitmap bmp;
bmp.CreateCompatibleBitmap(&m_memDc,wid,hei);
CDC memDc;
memDc.CreateCompatibleDC(NULL);
memDc.SelectObject(&bmp);
memDc.BitBlt(0,0,wid,hei,&m_memDc, m_traker.m_rect.left, m_traker.m_rect.top,SRCCOPY);
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP,bmp);
CloseClipboard();
CTime t = CTime::GetCurrentTime();
srand(time(NULL));
CString strPicName;
strPicName.Format(L"C:\\Users\\GreatWall\\Desktop\\截图%s.png", t.Format("%y%m%d%H%M%S"));
CImage img;
img.Attach(bmp);
img.Save(strPicName);
img.Detach();
this->ShowWindow(SW_HIDE);
CDialogEx::OnLButtonDblClk(nFlags, point);
}
MFC截图工具
最新推荐文章于 2024-08-16 16:02:02 发布