将光标更改为等待整个应用程序的光标

时间:2017-01-14 15:37:45

标签: c# winforms cursor

我做了一个很小的可重复使用的模态表单,它有一个标签(用于“请等待”消息)和后台工作程序。 (我们称之为WaitForm) 该表单可在应用程序中重复使用。

当“加载”触发时,它将调用backgroundworker的DoWork事件(委托它,以便调用此表单的任何代码都可以执行自己的操作)。

当它正在运行时,我希望所有表单都显示一个等待光标。因为此表单是模态的,所以只有当用户将鼠标悬停在WaitForm上时,才会显示等待光标。如果您移动鼠标并将鼠标悬停在父窗体上,则光标会变回默认箭头。

我已经尝试了以下内容,无论是单独还是与其他人合并:

Application.UseWaitCursor = true;
this.Cursor = Cursors.WaitCursor;
this.Cursor.Current = Cursors.WaitCursor;

_Parent.Cursor = Cursors.WaitCursor;  //I tried to pass the calling parent form as a parameter in the constructor of the "WaitForm" so that I can set its cursor.

WaitForm按预期工作。它显示并启动后台工作程序。唯一让我磨牙的是光标。我错过了一些明显的东西吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

看起来像我认为是“按设计”的限制,您可能希望使用Win32 API来覆盖标准的Winform行为。

[DllImport("user32.dll")]
static extern IntPtr LoadCursorFromFile(string lpFileName);

[DllImport("user32.dll")]
static extern IntPtr SetCursor(IntPtr hCursor);

[DllImport("user32.dll")]
static extern bool SetSystemCursor(IntPtr hcur, uint id);

private const uint OCR_NORMAL = 32512;

static Cursor ColoredCursor;

...

// ======== SET WINDOWS CURSOR ================================== ==

    IntPtr cursor = LoadCursorFromFile("example.cur");
    bool ret_val = SetSystemCursor(cursor, OCR_NORMAL);

// ======== SET WINDOWS CURSOR ================================== ==

// ======== SET FORM CURSOR ================================== ==

    IntPtr cursor = LoadCursorFromFile("example.cur");
    ColoredCursor = new Cursor(cursor);
    this.Cursor = ColoredCursor;

// ======== SET FORM CURSOR ================================== ==

// ========从图像设置表格游标==============================

    Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
    Graphics.FromImage(hh);
    IntPtr ptr = hh.GetHicon();
    Cursor c = new Cursor(ptr);
    this.Cursor = c;

// ========从图像设置表格游标================================ ==

参考:http://www.pinvoke.net/default.aspx/user32.setcursor

有关其他示例,请参阅此处:https://social.msdn.microsoft.com/Forums/windows/en-US/9ea0bf74-760f-4f40-b64c-0cf7b0a56939/save-custom-cursor?forum=winforms

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1 {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
      Bitmap bmp = Properties.Resources.Image1;
      bmp.MakeTransparent(Color.White);
      IntPtr hIcon = bmp.GetHicon();
      Icon ico = Icon.FromHandle(hIcon);
      Cursor cur = new Cursor(hIcon);
      using (FileStream fs = new FileStream(@"c:\temp\test.cur", FileMode.Create, FileAccess.Write))
        ico.Save(fs);
      cur.Dispose();
      ico.Dispose();
      DestroyIcon(hIcon);

      // Test it
      cur = new Cursor(@"c:\temp\test.cur");
      this.Cursor = cur;
    }
    [DllImport("user32.dll")]
    extern static bool DestroyIcon(IntPtr handle);
  }
}

答案 1 :(得分:0)

用于我使用的用户控制器中的等待光标:

this.UseWaitCursor = true;