// ScreenShotsDlg.cpp : implementation file // #include "stdafx.h" #include "ScreenShots.h" #include "ScreenShotsDlg.h" #include "catchdlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include "catchkey.h" ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About #define WM_SHOTS (WM_USER + 101) #define WM_MY_SHORTS (WM_USER + 105) CScreenShotsDlg::CScreenShotsDlg(CWnd* pParent /*=NULL*/) : CDialog(CScreenShotsDlg::IDD, pParent) { //{{AFX_DATA_INIT(CScreenShotsDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_bLButnDown = false; m_mPoint.x = 0; m_mPoint.y = 0; m_mnFlags = 0; m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CScreenShotsDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CScreenShotsDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CScreenShotsDlg, CDialog) //{{AFX_MSG_MAP(CScreenShotsDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_LBUTTONDOWN() ON_WM_CANCELMODE() ON_WM_MOUSEMOVE() ON_WM_CTLCOLOR() ON_BN_CLICKED(IDC_START, OnStart) ON_BN_CLICKED(IDC_HIDE, OnHide) ON_WM_LBUTTONUP() //}}AFX_MSG_MAP ON_MESSAGE(WM_SHOTS, OnShowShots) ON_MESSAGE(WM_MY_SHORTS, OnCatchShots) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CScreenShotsDlg message handlers BOOL CScreenShotsDlg::OnInitDialog() { DWORD dwVK[] = {'P'}; int nLength = sizeof(dwVK) / sizeof(DWORD); StartCatch(dwVK, nLength, this->m_hWnd); 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 m_bmBack.DeleteObject(); m_brBack.DeleteObject(); m_bmBack.LoadBitmap(IDB_BK); m_brBack.CreatePatternBrush(&m_bmBack); // TODO: Add extra initialization here SetWindowText("截屏工具"); CRect rt; SystemParametersInfo(SPI_GETWORKAREA, 0, &rt, 0) ; int tastHeight = GetSystemMetrics(SM_CYSCREEN) - rt.Height(); m_swRect.left = GetSystemMetrics(SM_CXSCREEN) - 250; m_swRect.top = GetSystemMetrics(SM_CYSCREEN) - 35 - tastHeight -5 ; m_swRect.right = GetSystemMetrics(SM_CXSCREEN); m_swRect.bottom = GetSystemMetrics(SM_CYSCREEN) - tastHeight -5 ; SetWindowPos(&wndTopMost,m_swRect.left , m_swRect.top ,m_swRect.Width() , m_swRect.Height() ,SWP_SHOWWINDOW); return TRUE; // return TRUE unless you set the focus to a control } void CScreenShotsDlg::OnSysCommand(UINT nID, LPARAM lParam) { 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 CScreenShotsDlg::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 CScreenShotsDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } BOOL CScreenShotsDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class return CDialog::PreTranslateMessage(pMsg); } void CScreenShotsDlg::OnLButtonDown(UINT nFlags, CPoint point) { if (point.x < 40) { PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y)); SetCursor(AfxGetApp()->LoadCursor(IDC_RIGHT)); } m_bLButnDown = true; m_mPoint.x = point.x; m_mPoint.y = point.y; m_mnFlags = nFlags; CDialog::OnLButtonDown(nFlags, point); } void CScreenShotsDlg::OnCancelMode() { CDialog::OnCancelMode(); // TODO: Add your message handler code here } void CScreenShotsDlg::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if (point.x < 40) SetCursor(AfxGetApp()->LoadCursor(IDC_NORMAL)); m_mPoint.x = point.x; m_mPoint.y = point.y; m_mnFlags = nFlags; CDialog::OnMouseMove(nFlags, point); } HBRUSH CScreenShotsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Change any attributes of the DC here return m_brBack; // TODO: Return a different brush if the default is not desired return hbr; } void CScreenShotsDlg::OnStart() { // TODO: Add your control notification handler code here OnOK(); CCatchDlg dlg; ((CScreenShotsApp *)AfxGetApp())->m_pMainWnd = &dlg; dlg.DoModal(); } void CScreenShotsDlg::OnHide() { // TODO: Add your control notification handler code here NOTIFYICONDATA nid; nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA); nid.hWnd=this->m_hWnd; nid.uID=IDR_MAINFRAME; nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ; nid.uCallbackMessage=WM_SHOTS;//自定义的消息名称 nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME)); strcpy(nid.szTip,"ScreenShots");//信息提示条为“ScreenShots” Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标 ShowWindow(SW_HIDE);//隐藏主窗口 } void CScreenShotsDlg::OnOK() { // TODO: Add extra validation here NOTIFYICONDATA nid; nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA); nid.hWnd=this->m_hWnd; nid.uID=IDR_MAINFRAME; nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME)); Shell_NotifyIcon(NIM_DELETE,&nid); //在托盘区删除图标 CDialog::OnOK(); } LRESULT CScreenShotsDlg::OnShowShots(WPARAM wParam, LPARAM lParam) { if(wParam!=IDR_MAINFRAME) { return 1; } switch(lParam) { case WM_RBUTTONUP: //右键起来时弹出快捷菜单 { LPPOINT lpoint=new tagPOINT; ::GetCursorPos(lpoint); //得到鼠标位置 CMenu menu; menu.CreatePopupMenu(); //声明一个弹出式菜单 menu.AppendMenu(MF_STRING,IDC_START,"截图"); menu.AppendMenu(MF_STRING,IDOK,"退出"); //确定弹出式菜单的位置 menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this); //资源回收 HMENU hmenu=menu.Detach(); menu.DestroyMenu(); delete lpoint; } break; case WM_LBUTTONDBLCLK: //双击左键的处理 { this->ShowWindow(SW_SHOW); //简单的显示主窗口 } break; } return 0; } LRESULT CScreenShotsDlg::OnCatchShots(WPARAM wParam, LPARAM lParam) { if(m_bLButnDown) { ::SendMessage(this->m_hWnd,WM_LBUTTONUP,m_mPoint.x,m_mPoint.y); } StopCatch(); OnStart(); return 0; } void CScreenShotsDlg::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default m_bLButnDown = false; CDialog::OnLButtonUp(nFlags, point); }