C# 以编程方式移动鼠标光标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/647236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:38:23  来源:igfitidea点击:

Moving mouse cursor programmatically

c#internet-explorermousepinvokecursor

提问by Beaker

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. 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):

这段代码效果很好,但我似乎无法弄清楚如何扩展它。假设我将 Internet Explorer(嵌入在 Windows 窗体中)打开到一个网页(一些我事先不知道的随机页面),其中有一个下拉列表框。我修改了上面的代码以将光标移到列表框上并使列表框下拉(使用如下所示的鼠标单击方法将列表下拉),并在列表中上下移动突出显示每个项目作为鼠标指针过去了,但对于我的生活,我无法弄清楚如何实际使鼠标单击当前选定的项目以保持选择。我现在这样做的方式是下拉列表框关闭并且选择没有改变。我正在使用以下代码进行鼠标单击(这确实使列表下拉):

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

鲍勃

采纳答案by Michael

You should use SendInput (http://msdn.microsoft.com/en-us/library/ms646310(VS.85).aspx) to synthesize mouse click events instead of using SendMessages directly.

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

回答by arul

I believe that you're missing a correct WPARAMfor the WM_LBUTTONDOWNmessage, which for the left-button is MK_LBUTTON

我相信您错过了正确WPARAMWM_LBUTTONDOWN消息,左侧按钮是 MK_LBUTTON

 #define MK_LBUTTON          0x0001