Cursor.Current 与 this.Cursor [英] Cursor.Current vs. this.Cursor
问题描述
.Net 中的 Cursor.Current
和 this.Cursor
(其中 this
是一个 WinForm)有区别吗?我一直使用 this.Cursor
并且很幸运,但我最近开始使用 CodeRush 并且只是在Wait Cursor"块中嵌入了一些代码,而 CodeRush 使用了 Cursor.Current
属性.我在 Internet 上和工作中看到其他程序员在使用 Cursor.Current
属性时遇到了一些问题.这让我想知道两者是否有区别.提前致谢.
Is there a difference between Cursor.Current
and this.Cursor
(where this
is a WinForm) in .Net? I've always used this.Cursor
and have had very good luck with it but I've recently started using CodeRush and just embedded some code in a "Wait Cursor" block and CodeRush used the Cursor.Current
property. I've seen on the Internet and at work where other programmers have had some problems with the Cursor.Current
property. It just got me to wondering if there is a difference in the two. Thanks in advance.
我做了一个小测试.我有两个winform.我单击 form1 上的一个按钮,将 Cursor.Current
属性设置为 Cursors.WaitCursor
,然后显示 form2.两种形式的光标都不会改变.它仍然是 Cursors.Default
(指针)光标.
I did a little test. I have two winforms. I click a button on form1, set the Cursor.Current
property to Cursors.WaitCursor
and then show form2. The cursor doesn't change on either form. It remains Cursors.Default
(pointer) cursor.
如果我在form1上的按钮点击事件中将this.Cursor
设置为Cursors.WaitCursor
并显示form2,则等待光标只显示在form1上,默认光标为在预期的 form2 上.所以,我仍然不知道 Cursor.Current
是做什么的.
If I set this.Cursor
to Cursors.WaitCursor
in the button click event on form1 and show form2, the wait cursor only shows on form1 and the default cursor is on form2 which is expected. So, I still don't know what Cursor.Current
does.
推荐答案
Windows 向包含鼠标光标的窗口发送 WM_SETCURSOR 消息,使其有机会更改光标形状.像 TextBox 这样的控件利用了这一点,将光标更改为 I-bar.Control.Cursor 属性确定将使用什么形状.
Windows sends the window that contains the mouse cursor the WM_SETCURSOR message, giving it an opportunity to change the cursor shape. A control like TextBox takes advantage of that, changing the cursor into a I-bar. The Control.Cursor property determines what shape will be used.
Cursor.Current 属性直接改变形状,无需等待 WM_SETCURSOR 响应.在大多数情况下,这种形状不太可能长期存在.只要用户移动鼠标,WM_SETCURSOR 就会将其改回 Control.Cursor.
The Cursor.Current property changes the shape directly, without waiting for a WM_SETCURSOR response. In most cases, that shape is unlikely to survive for long. As soon as the user moves the mouse, WM_SETCURSOR changes it back to Control.Cursor.
UseWaitCursor 属性已在 .NET 2.0 中添加,以便更轻松地显示沙漏.不幸的是,它不能很好地工作.它需要 WM_SETCURSOR 消息来更改形状,并且当您将该属性设置为 true 然后执行一些需要一段时间的操作时不会发生这种情况.试试这个代码,例如:
The UseWaitCursor property was added in .NET 2.0 to make it easier to display an hourglass. Unfortunately, it doesn't work very well. It requires a WM_SETCURSOR message to change the shape and that won't happen when you set the property to true and then do something that takes a while. Try this code for example:
private void button1_Click(object sender, EventArgs e) {
this.UseWaitCursor = true;
System.Threading.Thread.Sleep(3000);
this.UseWaitCursor = false;
}
光标永远不会改变.为了使之成形,您还需要使用 Cursor.Current.这是一个简单的帮助类:
The cursor never changes. To whack that into shape, you'll need to use Cursor.Current as well. Here is a little helper class to make it easy:
using System;
using System.Windows.Forms;
public class HourGlass : IDisposable {
public HourGlass() {
Enabled = true;
}
public void Dispose() {
Enabled = false;
}
public static bool Enabled {
get { return Application.UseWaitCursor; }
set {
if (value == Application.UseWaitCursor) return;
Application.UseWaitCursor = value;
Form f = Form.ActiveForm;
if (f != null && f.Handle != IntPtr.Zero) // Send WM_SETCURSOR
SendMessage(f.Handle, 0x20, f.Handle, (IntPtr)1);
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}
并像这样使用它:
private void button1_Click(object sender, EventArgs e) {
using (new HourGlass()) {
System.Threading.Thread.Sleep(3000);
}
}
这篇关于Cursor.Current 与 this.Cursor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!