首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 控制滑鼠操作 (一) 限制移動範圍與基礎設定

     編輯:關於C語言

功能概覽

控制滑鼠操作包含限制移動範圍、設定左右鍵、顯示/隱藏游標、設定雙擊時間間隔等。本文透過兩個具體範例介紹相關知識。

1. 限制滑鼠移動範圍

使用 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() 釋放限制。

2. 滑鼠基礎設定 API

包含 SwapMouseButton、ShowCursor、SetDoubleClickTime、GetDoubleClickTime,詳細宣告見上文(二)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved