Cursor.Current与this.Cursor [英] Cursor.Current vs. this.Cursor

查看:613
本文介绍了Cursor.Current与this.Cursor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有间 Cursor.Current this.Cursor (其中这是一个差异是一个WinForm)在.net中?我一直使用 this.Cursor 并有很好的运气,但我最近开始使用$ C $粉碎,只是嵌入一些code在等待光标块和$ C $美眉使用了 Cursor.Current 属性。我已经看到在互联网上,在那里其他的程序员有一些问题与 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.

我做了一个小测试。我有两个的WinForms。我点击Form1上放置一个按钮,设置 Cursor.Current 属性 Cursors.WaitCursor ,然后显示窗体2。光标并没有任何形式的改变。它仍然 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.

如果我设置 this.Cursor Cursors.WaitCursor 在Form1并显示窗体2按钮点击事件,等待光标只显示Form1上,默认光标在预计窗口2。所以,我还是不知道是什么 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消息,给它一个机会改变光标形状的窗口。像文本框的控制利用了这一优势,改变光标变成我吧。该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,然后做一些需要一段时间,这不会发生。试试这个code,例如:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆