Windows 窗体应用程序中未抗锯齿的手形光标
好的,那么您知道 Windows Vista 和 Windows 7 MS 中如何更改手形光标(当您将鼠标悬停在超链接上时显示的光标),并向其添加更多细节,使其具有抗锯齿效果并且边缘美观平滑吗?
那么,为什么 Windows 窗体应用程序中的情况与此不同呢?
我厌倦了看着一个看起来像穴居人画的蹩脚手形光标。
有没有一种方法可以以编程方式告诉它显示系统中实际安装的那个?我查看了 Windows 目录中的 Cursors 文件夹,发现旧的手形光标根本不存在!那么为什么 WinForms 仍然使用旧的呢?我怎样才能“升级”它?
Okay, so you know how in Windows Vista and Windows 7 MS changed the Hand Cursor (the one that shows up when you hover over a hyperlink), and added more detail to it so it's antialiased and nice and smooth around the edges?
Well, why isn't it like that in Windows Forms apps?
I'm sick off looking at a crappy hand cursor that looks like it was drawn by a caveman.
Is there a way to programmatically tell it to display the one that's actually installed in the system? I looked in the Cursors folder in my Windows directory, and the old hand cursor isn't even there! So why is WinForms still using the old one? How can I 'upgrade' it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,WinForms 控件仍然使用 Windows 98/2000 附带的老式手形光标。它缺乏 Aero 光标所具有的抗锯齿效果。这是因为 .NET Framework 包含其自己的硬编码游标,它使用该游标而不是系统默认游标。我认为这是因为 .NET 的早期版本针对的是 Windows 95 等操作系统,这些操作系统没有与此光标捆绑在一起,但还没有进行考古学来证明这一点。
幸运的是,强制它使用正确的方法很容易。您只需告诉操作系统您希望它使用默认的手形光标,然后无论用户在哪个版本的 Windows 上运行您的程序,即使他们已经更改了默认的鼠标光标,它都是正确的主题。
最简单的方法是对现有控件进行子类化,覆盖
WndProc
函数 拦截WM_SETCURSOR
消息,并告诉它使用系统IDC_HAND
光标。您只需要一点 P/Invoke 魔法。以下代码是使用
LinkLabel
控件的示例:Yes, the WinForms controls still use the old-school hand cursor, as shipped with Windows 98/2000. It lacks the anti-aliasing effects that the one included with the Aero cursors does. This is because the .NET Framework includes its own hard-coded cursor, which it uses instead of the system default. I presume this is because early versions of .NET were targeting operating systems like Windows 95 that didn't come bundled with this cursor, but haven't done the archaeology to prove it.
Fortunately, it's easy enough to force it to use the right one. You just have to tell the operating system you want it to use the default hand cursor, and then it will be correct no matter what version of Windows the user runs your program on, and even if they've changed their mouse cursors from the default theme.
The simplest way of doing that is to subclass the existing control, override the
WndProc
function to intercept theWM_SETCURSOR
message, and tell it to use the systemIDC_HAND
cursor. You just need a little bit of P/Invoke magic.The following code is an example of how that might look using the
LinkLabel
control:请原谅我复活一年前的话题!
在搞乱了原始解决方案并查看了 反映 LinkLabel 源代码,我“终于”找到了一种快速而干净的方法:
这个类实际上做了我们想要的事情:它显示正确的系统手形光标而不闪烁,并且仅在控件的 LinkArea 上执行此操作。
Excuse me for resurrecting a year-old thread!!!
After messing around with the original solution and taking a look at the reflected LinkLabel source code, I "finally" found a quick yet clean way of doing it :
This class actually does what we want: It shows the proper system hand cursor without flickering and does this only on the LinkArea of the control.
您需要将光标更改为系统手形光标。为此,您需要处理
WM_SETCURSOR
并检查OverrideCursor
是否为Cursors.Hand
,然后通过调用将其更改为系统光标设置光标
:You need to change the cursor to system hand cursor. To do so, you need to handle
WM_SETCURSOR
and check ifOverrideCursor
isCursors.Hand
then change it to the system cursor by callingSetCursor
:很抱歉收回这篇旧帖子,但我对此也有某种解决方案。
如果您需要在应用程序范围内应用系统光标而不接触旧控件,请在应用程序启动时使用它:
Sorry for getting this old post back, but i also have some kind of solution for this.
If you need to apply the systemcursor application wide without touching old controls, use this at applicationstart:
在不创建新控件的情况下执行此操作需要更改控件的光标并创建自定义链接标签,否则它将无法工作
我们通过添加一个标签来创建自定义链接标签,更改字体下划线并更改其前景色并添加点击事件
抱歉只有 vb .net 代码,您可以使用在线转换器
编辑:缺少一些代码
Doing that without creating a new control wee need to change the Control's Cursor AND create a custom linklabel or else it wouldn't work
we create the custom linklabel by adding a label changing the font underline and changing it's fore color and add an click event
sorry only have vb .net code you might use an online converter
EDIT: some code was missing