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

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

问题描述

要开始了,我发现在 HTTP此code:// 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;
    }
}

在按钮的点击事件处理器

粘贴下面code:

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.

这code的伟大工程,但我似乎无法弄清楚如何扩展它一下。比方说,我有Internet Explorer(嵌入在Windows窗体)开一个网页(一些随机的网页,我不知道前手)一个下拉在其列表框中。我修改了上面的code到超过移动光标,并获得列表框下拉(使用如下所示的鼠标点击的方法来删除列表中向下),以及上下移动列表中突出显示的每个项目为将鼠标指针越过,但对我的生活我无法弄清楚如何真正使当前所选项目的鼠标点击,以保持选择。我做它现在下拉列表框的方式只是关闭,选择不会改变。我使用鼠标单击下面的code(这确实让列表下拉):

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.

鲍勃

推荐答案

您应该使用SendInput(<一个href=\"http://msdn.microsoft.com/en-us/library/ms646310%28VS.85%29.aspx\">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天全站免登陆