2009-09-08 170 views
4

如何將鼠標光標位置設置爲C#屏幕上的指定點?如何將鼠標光標位置設置爲C#屏幕上的指定點?

我是否必須開發接收鼠標和鍵盤座標並按下主板緩衝區?

是否有另一個做點擊或我想象?

+2

立即跳入腦海的問題是'爲什麼'? – 2009-09-08 05:47:45

+0

在我腦海中做點事情。爲什麼? – Sherif 2009-09-08 05:49:09

+0

@MatthewScharley我來這裏是爲了「自動化」.. – 2013-06-08 13:20:38

回答

6

下面我們來設置鼠標的位置並進行點擊:

public static void ClickSomePoint() { 
    // Set the cursor position 
    System.Windows.Forms.Cursor.Position = new Point(20, 35); 

    DoClickMouse(0x2); // Left mouse button down 
    DoClickMouse(0x4); // Left mouse button up 
} 

static void DoClickMouse(int mouseButton) { 
    var input = new INPUT() { 
     dwType = 0, // Mouse input 
     mi = new MOUSEINPUT() { dwFlags = mouseButton } 
    }; 

    if (SendInput(1, input, Marshal.SizeOf(input)) == 0) { 
     throw new Exception(); 
    } 
} 
[StructLayout(LayoutKind.Sequential)] 
struct MOUSEINPUT { 
    int dx; 
    int dy; 
    int mouseData; 
    public int dwFlags; 
    int time; 
    IntPtr dwExtraInfo; 
} 
struct INPUT { 
    public uint dwType; 
    public MOUSEINPUT mi; 
} 
[DllImport("user32.dll", SetLastError=true)] 
static extern uint SendInput(uint cInputs, INPUT input, int size); 

只要記住,這可能是非常惱人的用戶。

:)


如果你想單擊窗體上的按鈕,你可以使用'PerformClick()'方法。

+0

有沒有另一個做點擊或我想象? – Sherif 2009-09-08 06:09:16

+0

或者可能不是。這是有什麼事嗎? – Sherif 2009-09-08 06:10:58

+0

但如果我想點擊屏幕上的任何東西,是否有東西或我的想象力會在這裏結束? – Sherif 2009-09-08 06:16:21