如果你想避免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