限制滑鼠移動範圍在圓形區域
主要是使用了ClipCursor,
void CMy001View::OnLButtonDown(UINT nFlags, CPoint point) //********限制滑鼠移動範圍
{
CRect rect; //新建一個矩形框
GetClientRect(&rect); //把客戶區賦值給矩形框
ClientToScreen(&rect); //將客戶區視窗轉換為螢幕視窗座標
ClipCursor(&rect); //限制滑鼠移動範圍在矩形框中
CView::OnLButtonDown(nFlags, point);
}
void CMy001View::OnRButtonUp(UINT nFlags, CPoint point) //*********解除滑鼠移動範圍
{
ClipCursor(NULL);//釋放滑鼠
CView::OnRButtonUp(nFlags, point);
}
但是這個函式設定滑鼠範圍必須是一個矩形框,對於圓形就不行了,我在網上找啊找,終於發現一個帖子裡,(http://topic.csdn.net/u/20091121/15/83e6e972-8f94-4a17-b7ec-1fbc8c073b13.html?16940)有位大神wartim提到動態地求出當前點所構成的四邊形是否是圓內切四邊形,然後把四邊形賦給ClipCursor。這個想法實在是太好了,於是嘗試了一下,在OnMouseMove函式中寫下:
////////////////////限制滑鼠活動範圍///////////////////////////////////////////
CRect rect;
GetClientRect(&rect);
if((point.x-radius)*(point.x-radius)+(point.y-radius)*(point.y-radius) > (radius*radius+5000))
{//如果滑鼠接近圓的邊緣
rect.bottom = point.y>radius?point.y:radius;//設定根據滑鼠位置和圓心位置設定矩形大小
rect.top = point.y>radius?radius:point.y;
rect.left = point.x>radius?radius:point.x;
rect.right = point.x>radius?point.x:radius;
ClientToScreen(&rect);//將客戶端轉為螢幕座標
ClipCursor(&rect); //限制滑鼠運動區域
}
else
{//直到滑鼠回到圓中,釋放滑鼠限制區域
ClipCursor(NULL);//釋放滑鼠
}
可以實現功能,但是,總還是有些邊邊角角有些奇怪的現象,滑鼠會亂動,出乎意料。如果滑鼠拉得過快,也會在邊緣上衝出去,就無法響應滑鼠移動事件了。