控制滑鼠操作包含限制移動範圍、設定左右鍵、顯示/隱藏游標、設定雙擊時間間隔等。本文透過兩個具體範例介紹相關知識。
使用 ClipCursor 與 GetWindowRect API 函式:
[DllImport("user32", EntryPoint = "ClipCursor")]
public extern static int ClipCursor(ref RECT lpRect);
[DllImport("user32.dll", EntryPoint = "GetWindowRect")]
public extern static int GetWindowRect(int hwnd, ref RECT lpRect);
public struct RECT
{
public int left, top, right, bottom;
}
public void Lock(Form form)
{
RECT rect = new RECT();
GetWindowRect(form.Handle.ToInt32(), ref rect);
ClipCursor(ref rect);
}
public void UnLock()
{
RECT screenRect = new RECT();
screenRect.top = 0;
screenRect.left = 0;
screenRect.bottom = Screen.PrimaryScreen.WorkingArea.Bottom;
screenRect.right = Screen.PrimaryScreen.WorkingArea.Right;
ClipCursor(ref screenRect);
}
點擊「控制滑鼠移動」按鈕呼叫 Lock(this),滑鼠即被限制在視窗範圍內;點擊「恢復移動」呼叫 UnLock() 釋放限制。
包含 SwapMouseButton、ShowCursor、SetDoubleClickTime、GetDoubleClickTime,詳細宣告見上文(二)。