www.gusucode.com > 浅谈在应用CRectTracker类的程序中响应WM_LBUTTONUP消息 > 浅谈在应用CRectTracker类的程序中响应WM_LBUTTONUP消息/crecttracker_demo/CRectTracker_Demo/CRectTracker_DemoDlg.cpp
// CRectTracker_DemoDlg.cpp : implementation file // #include "stdafx.h" #include "CRectTracker_Demo.h" #include "CRectTracker_DemoDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif /////////////////////// CRect rect; BOOL IsLButtonDown=FALSE;//鼠标左键是否按下 CPoint pt_start;//橡皮筋区域的启始点(鼠标左键按下时) CPoint pt_end;//橡皮筋区域的终止点(鼠标左键松开时) /////////////////////// ///////////////////////////////////////////////////////////////////////////// // 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() ///////////////////////////////////////////////////////////////////////////// // CCRectTracker_DemoDlg dialog CCRectTracker_DemoDlg::CCRectTracker_DemoDlg(CWnd* pParent /*=NULL*/) : CDialog(CCRectTracker_DemoDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCRectTracker_DemoDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CCRectTracker_DemoDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCRectTracker_DemoDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCRectTracker_DemoDlg, CDialog) //{{AFX_MSG_MAP(CCRectTracker_DemoDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_SETCURSOR() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_LBUTTONDBLCLK() ON_BN_CLICKED(IDC_ABOUT, OnAbout) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCRectTracker_DemoDlg message handlers BOOL CCRectTracker_DemoDlg::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 ////////////////////////////////////// //初始化 m_rectTracker.m_nStyle=CRectTracker::resizeOutside|CRectTracker::dottedLine; m_rectTracker.m_rect.SetRect(0,0,0,0); ////////////////////////////////////// return TRUE; // return TRUE unless you set the focus to a control } void CCRectTracker_DemoDlg::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 CCRectTracker_DemoDlg::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 { ////////画出橡皮筋///////// CDC* pDC; pDC=GetDC(); m_rectTracker.Draw(pDC); pDC->DeleteDC(); /////////////////////// CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CCRectTracker_DemoDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } BOOL CCRectTracker_DemoDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { // TODO: Add your message handler code here and/or call default ////////////////////// //改变光标的形状 if (pWnd == this && m_rectTracker.SetCursor(this, nHitTest)) return TRUE; ////////////////////// else return CDialog::OnSetCursor(pWnd, nHitTest, message); } void CCRectTracker_DemoDlg::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default /////////////////////////////////////// //如鼠标点击位置不在选框区域内,则构造一个新选框 Invalidate(TRUE); if(m_rectTracker.HitTest(point)<0) { IsLButtonDown=TRUE; CRectTracker temp; temp.TrackRubberBand(this,point,TRUE); temp.m_rect.NormalizeRect(); //鼠标(矩形选框)起始位置 pt_start=point; //鼠标(矩形选框)结束位置 GetCursorPos(&pt_end); this->SendMessage(WM_LBUTTONUP,NULL,NULL); } //否则重置选框大小或位置 else { m_rectTracker.Track(this,point,TRUE); m_rectTracker.m_rect.NormalizeRect(); this->OnPaint(); } /////////////////////////////////////// CDialog::OnLButtonDown(nFlags, point); } void CCRectTracker_DemoDlg::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default ///////////////////////////////// //确保在构造一个新选框时才响应OnLButtonUp,否则在响应OnLButtonDblClk时会出现错误结果 if(IsLButtonDown==TRUE) { //MessageBox("鼠标左键松开","松开",NULL); //由于GetCursorPos获得的pt_end是Screen坐标,故用ScreenToClient()进行转换 rect.right=pt_end.x; rect.bottom=pt_end.y; ScreenToClient(&rect); pt_end.x=rect.right; pt_end.y=rect.bottom; //left,top,right,bottom m_rectTracker.m_rect.SetRect(pt_start.x,pt_start.y,pt_end.x,pt_end.y); m_rectTracker.m_rect.NormalizeRect(); this->OnPaint(); IsLButtonDown=FALSE; } ///////////////////////////////// CDialog::OnLButtonUp(nFlags, point); } void CCRectTracker_DemoDlg::OnLButtonDblClk(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default //////////////////////////// MessageBox("鼠标左键双击","双击",NULL); //////////////////////////// CDialog::OnLButtonDblClk(nFlags, point); } void CCRectTracker_DemoDlg::OnAbout() { // TODO: Add your control notification handler code here ////////////////////////////////////// CAboutDlg dlgAbout; dlgAbout.DoModal(); ////////////////////////////////////// }