简体   繁体   English

以编程方式移动鼠标光标

[英]Moving mouse cursor programmatically

To start out I found this code at http://swigartconsulting.blogs.com/tech_blender/2005/08/how_to_move_the.html :首先,我在http://swigartconsulting.blogs.com/tech_blender/2005/08/how_to_move_the.html找到了这段代码:

public class Win32
{
    [DllImport("User32.Dll")]
    public static extern long SetCursorPos(int x, int y);

    [DllImport("User32.Dll")]
    public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;
    }
}

Paste the following code in the button's click eventhandler:将以下代码粘贴到按钮的单击事件处理程序中:

Win32.POINT p = new Win32.POINT();
p.x = button1.Left + (button1.Width / 2);
p.y = button1.Top + (button1.Height / 2);

Win32.ClientToScreen(this.Handle, ref p);
Win32.SetCursorPos(p.x, p.y);

This will move the mouse pointer to the center of the button.这会将鼠标指针移动到按钮的中心。

This code works great, but I can't seem to figure out how to extend it a bit.这段代码效果很好,但我似乎无法弄清楚如何扩展它。 Let's say I have internet explorer (embedded in a windows form) open to a web page (some random page I don't know about before hand) with a drop down list box in it.假设我将 Internet Explorer(嵌入在 Windows 窗体中)打开到一个网页(一些我事先不知道的随机页面),其中有一个下拉列表框。 I've modified the above code to move the cursor over and get the list box to drop down(using the mouse click method shown below to drop the list down), and also move up and down the list highlighting each item as the mouse pointer goes over, but for the life of me I cannot figure out how to actually make the mouse click on the currently selected item to keep the selection.我修改了上面的代码,将光标移到下拉列表框上(使用如下所示的鼠标单击方法下拉列表),并在列表中上下移动突出显示每个项目作为鼠标指针继续,但对于我的生活,我无法弄清楚如何实际使鼠标单击当前选定的项目以保持选择。 The way I'm doing it now the drop down list box just closes and the selection isn't changed.我现在这样做的方式是下拉列表框刚刚关闭并且选择没有改变。 I'm using the following code for the mouse click (which does get the list to drop down):我正在使用以下代码进行鼠标单击(这确实使列表下拉):

private static void MouseClick(int x, int y, IntPtr handle) //handle for the browser window
{
    IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates
    IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl)

    const uint downCode = 0x201; // Left click down code
    const uint upCode = 0x202; // Left click up code

    SendMessage(handle, downCode, wParam, lParam); // Mouse button down
    SendMessage(handle, upCode, wParam, lParam); // Mouse button up
}

I'm sure I'm missing something simple here, but for the life of me cannot figure out what it is.我确定我在这里遗漏了一些简单的东西,但对于我的生活,我无法弄清楚它是什么。 Thanks in advance everyone.提前谢谢大家。

Bob鲍勃

您应该使用SendInput( http://msdn.microsoft.com/en-us/library/ms646310 ( VS.85 ) .aspx )来合成鼠标单击事件,而不是直接使用SendMessages。

我相信你错过了WM_LBUTTONDOWN消息的正确WPARAM ,对于左键是MK_LBUTTON

 #define MK_LBUTTON          0x0001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM