模拟鼠标移动鼠标

2023-09-04 02:01:09 作者:懵懂少年✘

我的ListView在我的用户形象。当我把画面我已经从图像画面移除鼠标时重绘图片滋养岁。 但是,当我将第二次在同样的画面并不想重画,但是当我带走了ListView控件的教堂,并再次navozhu作品。我想我可以做出仿鼠标。或者告诉我更好的东西。

I have ListView with Images in my UserControl. When I am bringing the picture I have is redraw the picture when removing the mouse from the image picture nourish old. But when I am bringing a second time on the same picture does not want to repaint but when I take away for the chapels of ListView and again navozhu works. I thought I could make an imitation mouse. Or tell me something better.

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

这工作,但我看到鼠标的一线希望。

It work but I see a glimmer of the mouse.

推荐答案

为了模拟鼠标移动,其按钮的点击等。您可以尝试 mouse_event API函数。要小心,它的工作原理与的米奇的没有的像素的:

In order to simulate mouse moves, its buttons' clicks etc. you may try mouse_event API function. Be careful, it works with mickeys not pixels:

的http://msdn.microsoft.com/en-us/library/windows/desktop/ms646260(v=vs.85).aspx

    [DllImport("User32.dll",
               EntryPoint = "mouse_event",
               CallingConvention = CallingConvention.Winapi)]
    internal static extern void Mouse_Event(int dwFlags,
                                            int dx,
                                            int dy,
                                            int dwData,
                                            int dwExtraInfo);

    [DllImport("User32.dll",
               EntryPoint = "GetSystemMetrics",
               CallingConvention = CallingConvention.Winapi)]
    internal static extern int InternalGetSystemMetrics(int value);

   ...

   // Move mouse cursor to an absolute position to_x, to_y and make left button click:
   int to_x = 500;
   int to_y = 300;

   int screenWidth = InternalGetSystemMetrics(0);
   int screenHeight = InternalGetSystemMetrics(1);

   // Mickey X coordinate
   int mic_x = (int) System.Math.Round(to_x * 65536.0 / screenWidth);
   // Mickey Y coordinate
   int mic_y = (int) System.Math.Round(to_y * 65536.0 / screenHeight);

   // 0x0001 | 0x8000: Move + Absolute position
   Mouse_Event(0x0001 | 0x8000, mic_x, mic_y, 0, 0);
   // 0x0002: Left button down
   Mouse_Event(0x0002, mic_x, mic_y, 0, 0);
   // 0x0004: Left button up
   Mouse_Event(0x0004, mic_x, mic_y, 0, 0);