通過鼠標(biāo)動(dòng)作來發(fā)出命令的程序
在一些比較不錯(cuò)的瀏覽器中,出現(xiàn)了一些新的功能,通過鼠標(biāo)動(dòng)作(也稱鼠標(biāo)手勢(shì)Mouse Gestures)來發(fā)出一些命令,比如opera,myie2.一般是這樣,先按住鼠標(biāo)右鍵,不要松,然后畫直線或者其他設(shè)定的路徑,就可以完成指定的命令。下面我們就來實(shí)現(xiàn)這個(gè)功能,具體的核心代碼來自共享軟件聯(lián)盟小樹沖浪瀏覽器中,整理改編了其中的一些地方。
本文引用地址:http://www.ex-cimer.com/article/151544.htm1.建立一個(gè)對(duì)話框程序,聲明以下變量和函數(shù)
BOOL m_bIsCapture;//一個(gè)標(biāo)志變量
char m_MouseGestures[4], m_SeqMG[4];//用來保存鼠標(biāo)動(dòng)作的代碼U(上) D(下)等..
int m_iMGLen;
int m_iMouseGS, m_iMouseGE;
POINT m_StartPoint; //鼠標(biāo)的坐標(biāo)點(diǎn)
BOOL MoveDirection(CPoint point, char* Direction); //判斷鼠標(biāo)的簡(jiǎn)單動(dòng)作,四個(gè),上下左右
void PushMouseGesture(char gesture);//把鼠標(biāo)動(dòng)作的代碼保存起來
2.在對(duì)話框中加一個(gè)文本框,增加它的CString變量,m_mouse,用來顯示鼠標(biāo)的動(dòng)作
3.重載OnMouseMove的函數(shù),如下
if( nFlags == MK_RBUTTON) //判斷時(shí)候鼠標(biāo)右鍵按下
{
if (m_bIsCapture) //初始的值的TRUE, 只有當(dāng)?shù)谝稽c(diǎn)的時(shí)候發(fā)生里面的動(dòng)作
{
m_bIsCapture=FALSE;
SetCapture(); //捕獲鼠標(biāo)
m_StartPoint = point; //記錄初始坐標(biāo)點(diǎn)
}
char dir;
if(MoveDirection(point, dir)) //調(diào)用函數(shù)
{
PushMouseGesture(dir);
m_StartPoint = point;
}
}
CDialog::OnMouseMove(nFlags, point);
}
4.判斷鼠標(biāo)動(dòng)作的函數(shù) (核心),個(gè)人認(rèn)為是很巧妙而且簡(jiǎn)單的算法:
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) x0)
*Direction = RBUT_LEFT;
else if(y>abs(x) y>0)
*Direction = RBUT_DOWN;
else if(abs(y)>abs(x) y0)
*Direction = RBUT_UP;
else
return FALSE;
return TRUE;
}
else
return FALSE;
}
5.PushMouseGesture函數(shù)
這個(gè)函數(shù)主要是將鼠標(biāo)的動(dòng)作保存到m_MouseGestures中,等以后調(diào)用
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;
評(píng)論