// CutScreenDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "CutScreen.h"
#include "CutScreenDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CCutScreenDlg 对话框
CCutScreenDlg::CCutScreenDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CCutScreenDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
begin.x = -1;
begin.y = -1;
end.x = -1;
end.y = -1;
TagBegin.x = -1;
TagBegin.y = -1;
TagEnd.x = -1;
TagEnd.y = -1;
Action = TAG_NOTHING;
UserMark = -1;
for(int i = 0; i < 4; i++)
{
UserBmp[i] = NULL;
}
UserColor = RGB(255, 0, 0);
}
void CCutScreenDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CCutScreenDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_HOTKEY, OnHotKey)
ON_MESSAGE(WM_TIMER, OnTimer)
ON_MESSAGE(WM_LBUTTONDOWN, OnLButtonDown)
ON_BN_CLICKED(IDC_BUTTON_SAVE, &CCutScreenDlg::OnBnClickedButtonSave)
ON_BN_CLICKED(IDC_BUTTON_RECT, &CCutScreenDlg::OnBnClickedButtonRect)
ON_BN_CLICKED(IDC_BUTTON_RAY, &CCutScreenDlg::OnBnClickedButtonRay)
ON_BN_CLICKED(IDC_BUTTON_WORD, &CCutScreenDlg::OnBnClickedButtonWord)
ON_BN_CLICKED(IDC_BUTTON_MARK, &CCutScreenDlg::OnBnClickedButtonMark)
ON_BN_CLICKED(IDC_BUTTON_OVER, &CCutScreenDlg::OnBnClickedButtonOver)
ON_BN_CLICKED(IDC_BUTTON_BACK, &CCutScreenDlg::OnBnClickedButtonBack)
ON_BN_CLICKED(IDC_BUTTON_CIRCLE, &CCutScreenDlg::OnBnClickedButtonCircle)
END_MESSAGE_MAP()
// CCutScreenDlg 消息处理程序
BOOL CCutScreenDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
ModifyStyle(WS_CAPTION , WS_MINIMIZE , SWP_DRAWFRAME);
SetFocus();
ShowWindow(SW_HIDE);
SetTimer(1, 2000, NULL);
RegisterHotKey(m_hWnd, 200, MOD_CONTROL, 'A');
RegisterHotKey(m_hWnd, 201, MOD_ALT, 'A');
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CCutScreenDlg::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.FillSolidRect(rect, RGB(255, 255, 255));
CClientDC pdc(this);
CDC bufferdc;
CDC transferdc;
HBITMAP bufferbmp = CreateCompatibleBitmap(pdc, scrlen, scrhei);
bufferdc.CreateCompatibleDC(&pdc);
transferdc.CreateCompatibleDC(&pdc);
transferdc.SelectObject(bmp);
bufferdc.SelectObject(bufferbmp);
bufferdc.BitBlt(0, 0, scrlen, scrhei, &transferdc, 0, 0, SRCCOPY);
pdc.BitBlt(0, 0, scrlen, scrhei, &bufferdc, 0, 0, SRCCOPY);
DeleteObject(bufferbmp);
DeleteDC(pdc);
DeleteDC(bufferdc);
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CClientDC pdc(this);
CDC bufferdc;
CDC transferdc;
HBITMAP bufferbmp = CreateCompatibleBitmap(pdc, scrlen, scrhei);
bufferdc.CreateCompatibleDC(&pdc);
transferdc.CreateCompatibleDC(&pdc);
transferdc.SelectObject(bmp);
bufferdc.SelectObject(bufferbmp);
bufferdc.BitBlt(0, 0, scrlen, scrhei, &transferdc, 0, 0, SRCCOPY);
if(begin.x != -1 && begin.y != -1 && end.x != -1 && end.y != -1)
{
CPoint pt;
GetCursorPos(&pt);
CRect rectchoose, rectappear;
rectchoose.bottom = (begin.y > end.y) ? begin.y : end.y;
rectchoose.top = (begin.y < end.y) ? begin.y : end.y ;
rectchoose.left = (begin.x < end.x) ? begin.x : end.x;
rectchoose.right = (begin.x > end.x) ? begin.x : end.x;
bufferdc.DrawEdge(rectchoose, EDGE_BUMP, BF_RECT);
rectappear.left = menu.x;
rectappear.top = menu.y;
rectappear.right = menu.x + 7*ROOM + 6*SIDE;
rectappear.bottom = menu.y + 2*ROOM + SIDE;
bufferdc.DrawEdge(rectappear, EDGE_BUMP, BF_RECT);
}
pdc.BitBlt(0, 0, scrlen, scrhei, &bufferdc, 0, 0, SRCCOPY);
DeleteObject(bufferbmp);
DeleteDC(pdc);
DeleteDC(bufferdc);
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CCutScreenDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CCutScreenDlg::PasteScreen()
{
CClientDC pdc(this);
scrlen = GetSystemMetrics(SM_CXSCREEN);
scrhei = GetSystemMetrics(SM_CYSCREEN);
CRect rect;
rect.left = 0;
rect.top = 0;
rect.bottom = scrhei;
rect.right = scrlen;
HDC hdc = CreateDCW(_T("DISPLAY"), NULL, NULL, NULL);
CDC* dc = CDC::FromHandle(hdc);
HDC clihdc = pdc.GetSafeHdc();
bmp = CreateCompatibleBitmap(clihdc, scrlen, scrhei);
CDC bufferdc;
bufferdc.CreateCompatibleDC(&pdc);
bufferdc.SelectObject(bmp);
bufferdc.BitBlt(0, 0, scrlen, scrhei, dc, 0, 0, SRCCOPY);
//::SetWindowPos(GetSafeHwnd(),HWND_TOPMOST, -3, -3, scrlen + 10, scrhei + 10, SWP_NOZORDER| SWP_NOACTIVATE|SWP_SHOWWINDOW);
::SetWindowPos(GetSafeHwnd(),HWND_TOP, -3, -3, scrlen + 10, scrhei + 10, SWP_NOZORDER| SWP_NOACTIVATE|SWP_SHOWWINDOW);
pdc.BitBlt(0, 0, scrlen, scrhei, &bufferdc, 0, 0, SRCCOPY);
DeleteDC(pdc);
DeleteDC(bufferdc);
}
long CCutScreenDlg::OnHotKey(WPARAM wp, LPARAM lp)
{
switch (wp)
{
case 200:
OnOK();
break;
case 201:
Init();
break;
default:
break;
}
return MAKELONG(0, 0);
}
BOOL CCutScreenDlg::PreTranslateMessage(MSG* pmsg)
{
return FALSE;
if(pmsg->message == WM_HOTKEY || pmsg->message == WM_TIMER || pmsg->message == WM_LBUTTONDOWN)
{
return FALSE;
}
if(pmsg->message == WM_LBUTTONDOWN)
{
return FALSE;
}
return TRUE;
}
long CCutScreenDlg::OnTimer(WPARAM wp, LPARAM lp)
{
if(wp == 1)
{
PasteScreen();
KillTimer(1);
}
else if(wp == 3)
{
Draw();
}
else if(wp == 4)
{
DoAction();
}
return MAKELONG(0, 0);
}
long CCutScreenDlg::OnLButtonDown(WPARAM wp, LPARAM lp)
{
if(begin.x == -1 && begin.y == -1)
{
GetCursorPos(&begin);
if(NULL == SetTimer(3, 50, NULL))
{
AfxMessageBox(_T("settimer fail"));
}
}
else if(end.x == -1 && end.y == -1)
{
GetCursorPos(&end);
KillTimer(3);
Draw();
}
else if(end.x != -1 && end.y != -1 && begin.x != -1 && begin.y != -1)
{
//进入用户标记的处理,根据用户操作启用定时器,定时器操作中根据用户action类型决定画图
if(Action == TAG_NOTHING)
return MAKELONG(0, 0);
if(TagBegin.x == -1 && TagBegin.y == -1)
{
//修正边缘,如果点选的区域超出范围则做无效点击处理
CRect rect;
rect.bottom = (begin.y > end.y) ? begin.y : end.y;
rect.top = (begin.y < end.y) ? begin.y : end.y ;
rect.left = (begin.x < end.x) ? begin.x : end.x;
rect.right = (begin.x > end.x) ? begin.x : end.x;
GetCursorPos(&TagBegin);
if(TagBegin.x > rect.right - EDGE_ALLOW)
{
TagBegin.x = -1;
TagBegin.y = -1;
return MAKELONG(0, 0);
}
else if(TagBegin.x < rect.left + EDGE_ALLOW)
{
TagBegin.x = -1;
TagBegin.y = -1;
return MAKELONG(0, 0);
}
if(TagBegin.y > rect.bottom - EDGE_ALLOW)
{
TagBegin.x = -1;
TagBegin.y = -1;
return MAKELONG(0, 0);
}
else if(TagBegin.y < rect.top + EDGE_ALLOW)
{
TagBegin.x = -1;
TagBegin.y = -1;
return MAKELONG(0, 0);
}
SetTimer(4, 50, NULL);
}
else if(TagEnd.x == -1 && TagEnd.y == -1)
{
GetCursorPos(&TagEnd);
KillTimer(4);
DoAction();
}
}
return MAKELONG(0, 0);
}
//void CCutScreenDlg::OnBnClickedButton1()
//{
// // TODO: 在此添加控件通知处理程序代码
//}
void CCutScreenDlg::Draw()
{
CClient
评论17
最新资源