以编程方式移动鼠标光标

发布于 2024-07-15 06:35:12 字数 1711 浏览 7 评论 0原文

首先,我在 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;
    }
}

将以下代码粘贴到按钮的单击事件处理程序中:

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);

这会将鼠标指针移动到按钮的中心。

这段代码工作得很好,但我似乎不知道如何扩展它。 假设我有 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
}

我确信我在这里遗漏了一些简单的东西,但我一生都无法弄清楚它是什么。 预先感谢大家。

鲍勃

To start out I found this code at 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):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦上情书 2024-07-22 06:35:12

我相信您缺少 WM_LBUTTONDOWN 消息的正确 WPARAM,对于左按钮来说是 MK_LBUTTON

 #define MK_LBUTTON          0x0001

I believe that you're missing a correct WPARAM for the WM_LBUTTONDOWN message, which for the left-button is MK_LBUTTON

 #define MK_LBUTTON          0x0001
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文