分类: C/C++
2008-08-01 16:58:21
BOOL m_bIsCapture;//一个标志变量 char m_MouseGestures[4], m_SeqMG[4];//用来保存鼠标动作的代码U(上) D(下)等.. int m_iMGLen; int m_iMouseGS, m_iMouseGE; POINT m_StartPoint; //鼠标的坐标点 BOOL MoveDirection(CPoint& point, char* Direction); //判断鼠标的简单动作,四个,上下左右 void PushMouseGesture(char gesture);//把鼠标动作的代码保存起来2.在对话框中加一个文本框,增加它的CString变量,m_mouse,用来显示鼠标的动作
if( nFlags == MK_RBUTTON) //判断时候鼠标右键按下 { if (m_bIsCapture) //初始的值的TRUE, 只有当第一点的时候发生里面的动作 { m_bIsCapture=FALSE; SetCapture(); //捕获鼠标 m_StartPoint = point; //记录初始坐标点 } char dir; if(MoveDirection(point, &dir)) //调用函数 { PushMouseGesture(dir); m_StartPoint = point; } } CDialog::OnMouseMove(nFlags, point); }4.判断鼠标动作的函数 (核心),个人认为是很巧妙而且简单的算法:
BOOL Cmouse2Dlg::MoveDirection(CPoint &point, char *Direction) { int x = point.x - m_StartPoint.x; int y = point.y - m_StartPoint.y; int dist = x*x y*y; if(dist>64) { if(x>abs(y) && x>0) *Direction = RBUT_RIGHT; else if(abs(x)>abs(y) && x<0) *Direction = RBUT_LEFT; else if(y>abs(x) && y>0) *Direction = RBUT_DOWN; else if(abs(y)>abs(x) && y<0) *Direction = RBUT_UP; else return FALSE; return TRUE; } else return FALSE; }5.PushMouseGesture函数
if(m_iMouseGE!=0 || m_iMouseGS !=0) //m_iMouseGS和m_iMouseGE初始为0 { int pre = (m_iMouseGE -1 m_iMGLen)m_iMGLen; if(m_MouseGestures[pre] == gesture) return; } m_MouseGestures[m_iMouseGE] = gesture; m_iMouseGE = (m_iMouseGE 1)m_iMGLen; if(m_iMouseGS == m_iMouseGE) m_iMouseGS = (m_iMouseGS 1)m_iMGLen;6.重载OnRButtonUp函数,这是最后触发的动作,命令处理都在这里
if(!m_bIsCapture) //看标志变量,是否触发了鼠标动作 { m_bIsCapture=TRUE; ReleaseCapture(); int i =0; m_SeqMG[0]= ''\0''; while(m_iMouseGE != m_iMouseGS) //将鼠标动作保存到m_SeqMG中,并在最后加上\0 { m_SeqMG[i] = m_MouseGestures[m_iMouseGS]; i ; m_SeqMG[i] = ''\0''; m_iMouseGS = (m_iMouseGS 1)m_iMGLen; } if(i>0) //开始比较鼠标动作,在这里可以加上自定义的动作和命令! { if(strcmp(m_SeqMG, "D")==0) m_mouse="Down"; else if(strcmp(m_SeqMG, "L")==0) m_mouse="Left"; else if(strcmp(m_SeqMG, "U")==0) m_mouse="Up"; else if(strcmp(m_SeqMG, "R")==0) m_mouse="Right"; else if(strcmp(m_SeqMG, "UD")==0) m_mouse="Up Down"; else if(strcmp(m_SeqMG, "RLR")==0) m_mouse="Right Left Right"; else if(strcmp(m_SeqMG, "UL")==0) m_mouse="Up Left"; else if(strcmp(m_SeqMG, "UR")==0) m_mouse="Up Right"; else { m_mouse=""; i = 0; } } UpdateData(FALSE); }7.在文档试图程序中是差不多的过程,可以自己试试.