本文介绍了在C#中移动鼠标(坐标单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Teamviewer像一款有趣的软件,该软件允许一个人查看另一个人的屏幕,然后单击并进行所有操作.无论如何,我已经完成了大多数的套接字工作,但是我不知道如何使鼠标单击正常工作.这是我在网上找到的以编程方式移动鼠标的代码:

I'm trying to make Teamviewer like piece of software for fun, which allows one person to view another person's screen and click and all that. Anyway, I have most all of the socket stuff done, but I don't know how to get the mouse clicks to work correctly. Here is the code I found online for moving the mouse programmatically:

  public static class VirtualMouse
{
    // import the necessary API function so .NET can
    // marshall parameters appropriately
    [DllImport("user32.dll")]
    static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

    // constants for the mouse_input() API function
    private const int MOUSEEVENTF_MOVE = 0x0001;
    private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const int MOUSEEVENTF_LEFTUP = 0x0004;
    private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
    private const int MOUSEEVENTF_RIGHTUP = 0x0010;
    private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
    private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
    private const int MOUSEEVENTF_ABSOLUTE = 0x8000;


    // simulates movement of the mouse.  parameters specify changes
    // in relative position.  positive values indicate movement
    // right or down
    public static void Move(int xDelta, int yDelta)
    {
        mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0);
    }


    // simulates movement of the mouse.  parameters specify an
    // absolute location, with the top left corner being the
    // origin
    public static void MoveTo(int x, int y)
    {
        mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0);
    }


    // simulates a click-and-release action of the left mouse
    // button at its current position
    public static void LeftClick()
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
    }
}

现在,我想使用MoveTo方法移动鼠标,但是任何移动都需要疯狂的高数字.无论如何,我可以将坐标移动到此处以像素为单位在屏幕上的位置吗?抱歉,如果这似乎是一个显而易见的问题,但是我已经用Google搜索了近一个小时,而且找不到关于鼠标x和y位置使用的单位的任何讨论,所以我无法设置任何类型的使一个面板上的点击与用户屏幕上的点击相匹配的公式.

Now I want to move the mouse using the MoveTo method, but it requires crazy high numbers for any movement. Is there anyway I can match coordinates for moving here to the position on screen in pixels? Sorry if this seems like an obvious question, but I've googled for almost an hour and I can't find any discussion of what units are being used for the mouse x and y position, so I can't set up any sort of formula to match clicks on one panel to clicks on the user's screen.

推荐答案

来自微软的文档:

您可以使用它来将以像素为单位的输入转换为所需的值,如下所示:

You can use that to convert the input in pixels to the desired value, like this:

var inputXinPixels = 200;
var inputYinPixels = 200;
var screenBounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
var outputX = inputXinPixels * 65535 / screenBounds.Width;
var outputY = inputYinPixels * 65535 / screenBounds.Height;
MoveTo(outputX, outputY);

请记住,这可能不适用于多台显示器.另请注意,该文档显示:

Please keep in mind that this may not be correct for multiple monitors. Also notice that the documention says:

附录:为上述公式可能不是最好的.基于针对AutoHokey进行的研究内部的以下代码效果更好:

Addendum: As pointed by J3soon the above formula might not be the best. Based on research done for AutoHokey the internal the following code works better:

var outputX = (inputXinPixels * 65536 / screenBounds.Width) + 1;
var outputY = (inputYinPixels * 65536 / screenBounds.Height) + 1;

请参见 AutoHotkey源代码以供参考.

如果我在您的位置,则可以使用Cursor.Position.以下代码按预期工作:

If I were in your position I would use Cursor.Position. The following code works as expected:

System.Windows.Forms.Cursor.Position = new System.Drawing.Point(200, 200);

是的,它将鼠标指针放置在屏幕的坐标(200、200)像素上(在LinqPad上测试).

Yes, it places the mouse pointer in the coordinates (200, 200) pixels of the screen [Tested on LinqPad].

附录:我了解了 System.Windows.Forms.Cursor.Position 在内部的用途-至少在Windows上为Mono.这是对 SetCursorPos 的调用.不需要怪异的坐标转换.

Addendum: I had a look at what does System.Windows.Forms.Cursor.Position use internally - On Mono on Windows at least. It is a call to SetCursorPos. No weird coordinate conversion needed.

这篇关于在C#中移动鼠标(坐标单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:21
查看更多