[游戏学习25] MFC 橡皮筋画线效果

 

>_<:这是给出窗口内外不同情况的处理展示的例子。

>_<:MouseCap.h

 1 #include<afxwin.h>
 2 class CMyApp :public CWinApp
 3 {
 4 public:
 5     virtual BOOL InitInstance();
 6 };
 7 class CMainWindow:public CFrameWnd
 8 {
 9 protected:
10     BOOL m_bTracking; //标志:鼠标按下为真,否则为假
11     BOOL m_bCaptureEndabled;//初始化为真,由OnNcLButtonDown()切换,以便用来打开或关闭鼠标捕获对程序影响
12     CPoint m_ptFrom;
13     CPoint m_ptTo;
14 
15     void InverLine(CDC* pDC,CPoint ptFrom,CPoint ptTo);
16 public:
17     CMainWindow();
18 protected:
19     afx_msg void OnLButtonDown(UINT nFlags,CPoint point);
20     afx_msg void OnLButtonUp(UINT nFlags,CPoint point);
21     afx_msg void OnMouseMove(UINT nFlags,CPoint point);
22     afx_msg void OnNcLButtonDown(UINT nHitTest,CPoint point);
23     DECLARE_MESSAGE_MAP();
24 };

>_<:MouceCap.cpp

 1 #include <afxwin.h>
 2 #include "MouseCap.h"
 3 
 4 CMyApp myApp;
 5 
 6 BOOL CMyApp::InitInstance(){
 7 
 8     m_pMainWnd=new CMainWindow;
 9     m_pMainWnd->ShowWindow(m_nCmdShow);
10     m_pMainWnd->UpdateWindow();
11     return TRUE;
12 }
13 
14 BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
15     ON_WM_LBUTTONDOWN()
16     ON_WM_LBUTTONUP()
17     ON_WM_MOUSEMOVE()
18     ON_WM_NCLBUTTONDOWN()
19 END_MESSAGE_MAP()
20 
21 
22 CMainWindow::CMainWindow(){
23     m_bTracking = FALSE;
24     m_bCaptureEndabled=TRUE;
25 
26     CString strWndClass = AfxRegisterWndClass(
27         0,
28         AfxGetApp()->LoadStandardCursor(IDC_CROSS),
29         (HBRUSH)(COLOR_WINDOW+1),
30         AfxGetApp()->LoadStandardIcon(IDI_WINLOGO)
31         );
32 
33     Create(strWndClass,_T("捕获鼠标_橡皮筋操作"));
34 }
35 
36 void CMainWindow::OnLButtonDown(UINT nFlags,CPoint point){
37     m_ptFrom=point;
38     m_ptTo=point;
39     m_bTracking=TRUE;
40 
41     if(m_bCaptureEndabled)
42         SetCapture();
43 }
44 void CMainWindow::OnMouseMove(UINT nFlags,CPoint point){
45     if(m_bTracking){
46         CClientDC dc(this);
47         InverLine(&dc,m_ptFrom,m_ptTo);//清除原来的
48         InverLine(&dc,m_ptFrom,point);//画一条新的
49         m_ptTo=point;
50     }
51 }
52 void CMainWindow::OnLButtonUp(UINT nFlags,CPoint point){
53     if(m_bTracking){
54         m_bTracking=FALSE;
55         if(GetCapture()==this)
56             ::ReleaseCapture();
57 
58         CClientDC dc(this);
59         InverLine(&dc,m_ptFrom,m_ptTo);//清除原来的
60         
61         CPen pen(PS_DASHDOTDOT,8,RGB(0,255,0));//画一条红的
62         dc.SelectObject(&pen);
63 
64         dc.MoveTo(m_ptFrom);
65         dc.LineTo(point);
66     }
67 }
68 //单击标题栏激活CFrameWnd::OnNcLButtonDown(nHitTest,point)处理程序
69 void CMainWindow::OnNcLButtonDown(UINT nHitTest,CPoint point){
70     if(nHitTest==HTCAPTION){
71         m_bCaptureEndabled=m_bCaptureEndabled ? FALSE:TRUE;
72         SetWindowText(m_bCaptureEndabled ?
73             _T("能够"):_T("不能"));
74     }
75     CFrameWnd::OnNcLButtonDown(nHitTest,point);
76 }
77 void CMainWindow::InverLine(CDC* pDC,CPoint ptFrom,CPoint ptTo){
78     int nOldMode=pDC->SetROP2(R2_NOT);//这种绘图模式保证一条清除一条新的
79     pDC->MoveTo(ptFrom);
80     pDC->LineTo(ptTo);
81 
82     pDC->SetROP2(nOldMode);
83 }

 

posted @ 2014-05-18 17:21  beautifulzzzz  阅读(2306)  评论(0编辑  收藏  举报