模拟鼠标移动

4
我有一个带图片的ListView在我的用户控件里。当我把鼠标从图片上移开时,图片会重新绘制,使旧的图片显示出来。但是,当我第二次将鼠标移到同一张图片上时,它不想重新绘制,但是当我从ListView里选中另一个项目再回到该项时它就可行了。我想我可以模拟鼠标事件或者您可以给我更好的建议。
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

它可以运行,但我看到了鼠标的一道微光。


2
"bringing the picture" = 拖放 (drag & drop)?“nourish” 是什么意思?“chapels” "navozhu"? - Rup
1个回答

10
为了模拟鼠标移动、按键点击等操作,您可以尝试使用mouse_event API函数。请注意,它使用的是mickeys而不是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 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);

API函数"mouse_event"在虚拟的65536x65536屏幕上操作。这个屏幕上的坐标被称为mickeys - 可能是以米奇老鼠命名的 :). 例如(引用自MSDN) "...一个mickey是鼠标移动的量..." - Dmitry Bychenko
谢谢。但我该如何让鼠标只停留在Forme上是一个开始UTB x = 0 y = 0的问题,然后我将示例x = 50 y = 50放入其中。 - guxago

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接