鼠标在Windows服务中移动自动化

时间:2014-04-21 14:31:55

标签: c# windows service mouse

我创建了一个Windows服务, 在On start方法中,我正在为输入设备调用Listener

public class LeapMouseServiceService : ServiceBase
{
    private Controller cnt;
    private LeapListener ls;
    public LeapMouseServiceService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {            
        this.cnt.AddListener(ls);
    }

MouseOpera有来自USer32.dll的mouse_event

    class MouseOpera
{
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern bool SetCursorPos(int x, int y);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    private const int MOUSEEVENTF_MOVE = 0x01;
    private const int MOUSEEVENTF_LEFTDOWN = 0x02;
    private const int MOUSEEVENTF_LEFTUP = 0x04;
    private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const int MOUSEEVENTF_RIGHTUP = 0x10;

    public static void DeltaMove(int x, int y)
    {
        mouse_event(MOUSEEVENTF_MOVE, x, y, 0, 0);
    }

LeapListener类具有Onframe方法

class LeapListener : Listener
{
    private Object thisLock = new Object();
    System.IO.StreamWriter sw = new System.IO.StreamWriter(@"K:\logs\log.txt",true);
    private void SafeWriteLine(String line)
    {
        lock (thisLock)
        {                
            sw.WriteLine(line);
        }
    }
    public override void OnInit(Controller controller)
    {
        SafeWriteLine(DateTime.Now.ToShortDateString());
        SafeWriteLine("Initialized");
    }

    public override void OnConnect(Controller controller)
    {
        SafeWriteLine("Connected");
    }

    public override void OnDisconnect(Controller controller)
    {
        //Note: not dispatched when running in a debugger.
        SafeWriteLine("Disconnected");
    }

    public override void OnExit(Controller controller)
    {
        SafeWriteLine("Exited");
    }

    public override void OnFrame(Controller controller)
    {
        // Get the most recent frame and report some basic information
        Frame frame = controller.Frame();
        MouseOpera.DeltaMove((int)(hand.Direction.Yaw*10),-(int)(hand.Direction.Pitch*10));

当服务运行时,log.txt完美捕获所有Leap活动。但是,鼠标移动并没有发生。如何使用C#代码使鼠标从Windows服务自动移动?

0 个答案:

没有答案