如果你想避免Win32 native interop,Windows Input Simulator是一個看起來很好看的管理包裝器,圍繞SendInput。但最新版本不支持鼠標模擬。但是,活動主幹具有用於支持鼠標模擬的補丁(請參閱this討論)。該項目使用VS2010 C#,您可以從Subversion版本庫here導出和構建自己。
如果您對Win32 native interop感興趣,我將演示如何將SendInput API與F#結合使用。這個應用程序模擬一個右鍵單擊。
open System
open System.Runtime.InteropServices
type MOUSEINPUT = struct
val dx:int
val dy:int
val mouseData:int
val dwFlags:int
val time:int
val dwExtraInfo:int
new(_dx, _dy, _mouseData, _dwFlags, _time, _dwExtraInfo) = {dx=_dx; dy=_dy; mouseData=_mouseData; dwFlags=_dwFlags; time=_time; dwExtraInfo=_dwExtraInfo}
end
type INPUT = struct
//need to escape traditional INPUT identifier here since "type" is a reserved word
//though could use any other identifier name if so desired
val ``type``:int //0 is mouse
val mi:MOUSEINPUT
new(_type, _mi) = {``type``=_type; mi=_mi}
end
let MOUSEEVENTF_RIGHTDOWN = 0x0008
let MOUSEEVENTF_RIGHTUP = 0x0010
[<DllImport("user32.dll", SetLastError=true)>]
extern uint32 SendInput(uint32 nInputs, INPUT* pInputs, int cbSize)
let mutable inputRightDown = INPUT(0, MOUSEINPUT(0, 0, 0, MOUSEEVENTF_RIGHTDOWN, 0, 0))
let mutable inputRightUp = INPUT(0, MOUSEINPUT(0, 0, 0, MOUSEEVENTF_RIGHTUP, 0, 0))
SendInput(uint32 1, &&inputRightDown, Marshal.SizeOf(inputRightDown))
SendInput(uint32 1, &&inputRightUp, Marshal.SizeOf(inputRightUp))
對您的問題進行二讀,我看到最終要模擬鼠標和鍵盤事件發送到在後臺運行的應用程序,在這種情況下SendMessage/PostMessage可能是更合適的API。這仍然需要本地互操作,並希望我的示例使用SendInput將有所幫助。 Here是一個相關的問題。
優秀尖端InputSimulator !! +1 – 2011-10-19 14:39:08