2017-01-14 61 views
0

我制作了一个具有标签(用于“请稍候”消息)和后台工作人员的小型可重复使用的模式窗体。 (我们将其称为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按预期工作。它显示并启动背景工作。唯一让我磨牙的地方就是光标。我错过了明显的东西吗?

在此先感谢。

+1

一种方式将是一个'名单

'在一个静态类,每次实例化一个形式,它添加到列表中,那么当你要设置的每个表格光标,你可以遍历列表。事情是,当你想要一个Modal单线程方法时,为什么还要使用Background Worker?就像一个后台工作人员的目的是释放应用程序,让用户在主线程上做其他事情 –

+0

这不会令人遗憾。问题在于窗户是模态的。所有游标都使用Application.UseWaitCursor正确设置。它似乎只是一个模态窗体的窗口行为。 – Emilie217

+0

而使用后台工作者的目的是释放用户界面,而不管用户是否希望能够做其他事情。即使用户不能对它做任何事情,我也不希望剩下的UI被冻结。它看起来很奇怪。 – Emilie217

回答

0

看起来,我相信这是一个限制“按设计”,你可能希望诉诸使用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; 

...

// ======一套Windows CURSOR ======================= =================

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

// ======一套Windows CURSOR ============= ===========================

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

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

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

// ====== 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); 
    } 
}