表格外的滑鼠游标 [英] Mouse cursor outside of form
问题描述
我有一个用c ++编写的旧颜色选择器实用程序,几年前我编写了该代码,并希望使用c#进行重写.
I have an old color picker utility written c++ that I coded years back and want to rewrite using c#.
我实现了全局挂钩以从屏幕上拾取像素,依此类推.一切都很好,但是...
I implemented the global hook to pick pixels off the screen and so on. Everything is ok but...
一旦鼠标移到表格外并移至桌面,十字光标就会恢复为指针.对于我的c ++代码(实际上是MFC),这不会发生.
The cross cursor reverts to the pointer once the mouse moves outside the form and onto the desktop. This does not happen with my c++ code (MFC actually).
这是如何在c#中完成的?
How is this accomplished in c#?
谢谢大家.
(我正在使用此 http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C 作为挂钩)
(I'm using this http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C for the hook)
推荐答案
解决方案(或解决方法)是模拟鼠标单击事件的第一部分.这样会将鼠标锁定在调用窗口上,从而保留选定的光标.
The solution (or a workaround) is to simulate the first part of a mouse click event. This will lock the mouse on the calling window, thus preserving the chosen cursor.
[DllImport( "user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall )]
public static extern void mouse_event( uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo );
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
然后在代码中启用鼠标捕获后:
And then after enabling the mouse capture in the code:
mouse_event(
MOUSEEVENTF_LEFTDOWN,
(uint)Cursor.Position.X,
(uint)Cursor.Position.Y,
0,
0 );
this.Cursor = Cursors.Cross;
希望有帮助.
这篇关于表格外的滑鼠游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!