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

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

>_<:MouseCap.h

 #include<afxwin.h>
class CMyApp :public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMainWindow:public CFrameWnd
{
protected:
BOOL m_bTracking; //标志:鼠标按下为真,否则为假
BOOL m_bCaptureEndabled;//初始化为真,由OnNcLButtonDown()切换,以便用来打开或关闭鼠标捕获对程序影响
CPoint m_ptFrom;
CPoint m_ptTo; void InverLine(CDC* pDC,CPoint ptFrom,CPoint ptTo);
public:
CMainWindow();
protected:
afx_msg void OnLButtonDown(UINT nFlags,CPoint point);
afx_msg void OnLButtonUp(UINT nFlags,CPoint point);
afx_msg void OnMouseMove(UINT nFlags,CPoint point);
afx_msg void OnNcLButtonDown(UINT nHitTest,CPoint point);
DECLARE_MESSAGE_MAP();
};

>_<:MouceCap.cpp

 #include <afxwin.h>
#include "MouseCap.h" CMyApp myApp; BOOL CMyApp::InitInstance(){ m_pMainWnd=new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
} BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_NCLBUTTONDOWN()
END_MESSAGE_MAP() CMainWindow::CMainWindow(){
m_bTracking = FALSE;
m_bCaptureEndabled=TRUE; CString strWndClass = AfxRegisterWndClass(
,
AfxGetApp()->LoadStandardCursor(IDC_CROSS),
(HBRUSH)(COLOR_WINDOW+),
AfxGetApp()->LoadStandardIcon(IDI_WINLOGO)
); Create(strWndClass,_T("捕获鼠标_橡皮筋操作"));
} void CMainWindow::OnLButtonDown(UINT nFlags,CPoint point){
m_ptFrom=point;
m_ptTo=point;
m_bTracking=TRUE; if(m_bCaptureEndabled)
SetCapture();
}
void CMainWindow::OnMouseMove(UINT nFlags,CPoint point){
if(m_bTracking){
CClientDC dc(this);
InverLine(&dc,m_ptFrom,m_ptTo);//清除原来的
InverLine(&dc,m_ptFrom,point);//画一条新的
m_ptTo=point;
}
}
void CMainWindow::OnLButtonUp(UINT nFlags,CPoint point){
if(m_bTracking){
m_bTracking=FALSE;
if(GetCapture()==this)
::ReleaseCapture(); CClientDC dc(this);
InverLine(&dc,m_ptFrom,m_ptTo);//清除原来的 CPen pen(PS_DASHDOTDOT,,RGB(,,));//画一条红的
dc.SelectObject(&pen); dc.MoveTo(m_ptFrom);
dc.LineTo(point);
}
}
//单击标题栏激活CFrameWnd::OnNcLButtonDown(nHitTest,point)处理程序
void CMainWindow::OnNcLButtonDown(UINT nHitTest,CPoint point){
if(nHitTest==HTCAPTION){
m_bCaptureEndabled=m_bCaptureEndabled ? FALSE:TRUE;
SetWindowText(m_bCaptureEndabled ?
_T("能够"):_T("不能"));
}
CFrameWnd::OnNcLButtonDown(nHitTest,point);
}
void CMainWindow::InverLine(CDC* pDC,CPoint ptFrom,CPoint ptTo){
int nOldMode=pDC->SetROP2(R2_NOT);//这种绘图模式保证一条清除一条新的
pDC->MoveTo(ptFrom);
pDC->LineTo(ptTo); pDC->SetROP2(nOldMode);
}
04-25 18:53