在視圖類開頭定義
class CMouseView : public CVIEw
{
protected: // create from serialization only
CMouseVIEw();
DECLARE_DYNCREATE(CMouseVIEw)
// my add
CString m_ClassName;
int m_Dragging; //鼠標狀態
HCURSOR m_HCross; //鼠標形狀
CPoint m_PointOld; //鼠標的初始地點
CPoint m_pointOrigin; //鼠標原點
在構造函數中對
CMouseView::CMouseVIEw()
{
// TODO: add construction code here
m_Dragging=0; //鼠標的狀態為0,沒有按下,1為按下
m_HCross = AfxGetApp()->LoadStandardCursor(IDC_CROSS); //獲得鼠標的形狀賦值
m_HCross 存放程序的光標句柄,AfxGetApp()獲得應用程序類對象的指針,這個指針用於調用應用程序類對象的成員函數LoadStandardCursor()傳送光標的句柄。光標句柄參數參考LoadStandardCursor()函數
添加一個鼠標的lbuttondown 消息,然後添加代碼,(暈,書是用vc++7.0,看來很多東西以後要自己想了)
(cheat---遇到困難了嗎,祝賀你,你獲得了進步的機會~)
添加
void CMouseVIEw::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_pointOrigin = point; //用鼠標的坐標的賦值
m_PointOld = point; /用鼠標的坐標的賦值
SetCapture(); //捕獲鼠標
m_Dragging = 1; //設置鼠標狀態
RECT Rect; //定義窗口范圍
GetClIEntRect(&Rect); //獲得客戶區的范圍
ClIEntToScreen(&Rect); //轉換為屏幕坐標
::ClipCursor(&Rect); // 限定鼠標范圍
添加鼠標移動,消息
void CMouseVIEw::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
::SetCursor(m_HCross); //顯示鼠標形狀
if(m_Dragging)
{
CClientDC ClIEntDC(this); //獲得視窗設備表述表
ClIEntDC.SetROP2(R2_NOT);//生成逆轉當前屏幕顏色來繪圖方式,
ClIEntDC.MoveTo(m_pointOrigin);//畫
ClIEntDC.LineTo(m_PointOld);//畫起點--終點線
ClIEntDC.MoveTo(m_pointOrigin);
ClIEntDC.LineTo(m_PointOld);
m_pointOrigin = point;
}
CVIEw::OnMouseMove(nFlags, point);
}
添加鼠標up消息
void CMouseVIEw::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_Dragging)
{
m_Dragging = 0;
::ReleaseCapture();
::ClipCursor(NULL);
CClientDC ClIEntDC(this);
ClIEntDC.SetROP2(R2_NOT);
ClIEntDC.MoveTo(m_pointOrigin);
ClIEntDC.LineTo(m_PointOld);
ClIEntDC.SetROP2(R2_COPYPEN);
ClIEntDC.MoveTo(m_pointOrigin);
ClIEntDC.LineTo(m_PointOld);
}
CVIEw::OnLButtonUp(nFlags, point);
}