以编程方式移动鼠标光标 [英] Moving mouse cursor programmatically

查看:175
本文介绍了以编程方式移动鼠标光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始时,我在 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;
    }
}

将以下代码粘贴到按钮的click事件处理程序中: / p>

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

推荐答案

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

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.

这篇关于以编程方式移动鼠标光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆