2017-01-14 66 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); 
    } 
}