Use mouse gestures to send commands

Source: Internet
Author: User
Tags abs bool

In some of the more good browsers, there are some new features, through the mouse action (also known as mouse gestures mouse gestures) to send out some commands, such as opera,myie2. Generally, hold down the right mouse button, do not loose, and then draw a line or other set path, You can complete the specified command. Here's what we're going to do, and the core code comes from the Shareware Alliance small-tree surfing browser, which reorganizes some of these areas.

1. Create a dialog box program that declares the following variables and functionsBOOL 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. Add a text box in the dialog box to increase its CString variable, m_mouse, to display the mouse action

3. Overloaded onmousemove functions, as follows

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. To judge the function of the mouse action (core), personal thought is very clever and simple algorithm:

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 function

This function is mainly to save the mouse action to the m_mousegestures, and so on later call

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.