C#WinForm - 在Winform

时间:2015-06-03 22:25:44

标签: c# winforms events keyboard mouse

目前我正在尝试创建一个简化我必须执行的常用操作的应用程序。我想将鼠标移动到屏幕上的特定位置(手动),然后按1,2,3次X次。键1,2,3应执行以下操作:

1(D1)=单击鼠标右键 2(D2)=将鼠标当前Y轴向下移动37 px 3(D3)=鼠标左键单击。

使用下面的代码我会得到这种行为,但它在WinForm之外不起作用。

    [DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    public const int MOUSEEVENTF_LEFTDOWN = 0x02;
    public const int MOUSEEVENTF_LEFTUP = 0x04;
    public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    public const int MOUSEEVENTF_RIGHTUP = 0x10;

    private void Main_KeyDown(object sender, KeyEventArgs e)
    {
        this._x = Cursor.Position.X;
        this._y = Cursor.Position.Y;

        if (e.KeyCode == Keys.D1)
            mouse_event(MOUSEEVENTF_RIGHTDOWN, this._x, this._y, 0, 0);
        if (e.KeyCode == Keys.D2)
        {
            this.Cursor = new Cursor(Cursor.Current.Handle);
            Cursor.Position = new Point(this._x, this._y + 37);
            Cursor.Clip = new Rectangle(this.Location, this.Size);
        }
        if (e.KeyCode == Keys.D3)
            mouse_event(MOUSEEVENTF_LEFTDOWN, this._x, this._y, 0, 0);
    }

是否有人可以在执行此类操作时向我发送正确的方向?

0 个答案:

没有答案
相关问题