相关疑难解决方法(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;
    }
}
Run Code Online (Sandbox Code Playgroud)

将以下代码粘贴到按钮的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);
Run Code Online (Sandbox Code Playgroud)

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

这段代码效果很好,但我似乎无法弄清楚如何扩展它.假设我有一个Internet浏览器(嵌入在windows窗体中)打开一个网页(一些我手头不知道的随机页面),里面有一个下拉列表框.我已修改上面的代码,将光标移动到下拉列表框(使用下面显示的鼠标单击方法将列表向下拖放),并在列表中上下移动,突出显示每个项目作为鼠标指针过去,但对于我的生活,我无法弄清楚如何实际让鼠标点击当前选定的项目来保持选择.我正在这样做的方式现在下拉列表框只是关闭,选择不会改变.我正在使用以下代码进行鼠标单击(这会使列表下拉):

private static void MouseClick(int x, int y, IntPtr handle) //handle for the browser …
Run Code Online (Sandbox Code Playgroud)

c# mouse pinvoke internet-explorer cursor

21
推荐指数
2
解决办法
4万
查看次数

标签 统计

c# ×1

cursor ×1

internet-explorer ×1

mouse ×1

pinvoke ×1