2011-06-16 25 views
1

當我嘗試將光標更改爲自定義窗口,對於單個窗口,使用SetCursor()函數(使用user32.dll)時,它將更改它,但當鼠標開始移動時,光標更改爲默認值。所以,出現了一個問題,我如何使用自定義光標更改單個窗口的光標?如何更改C#中的窗口的光標?

+2

這將取決於窗口的創建。的WinForms/WPF /控制檯? – spender 2011-06-16 11:02:32

+0

嘗試從「form」屬性更改光標,該屬性將使用該特定窗體的自定義光標。 – Kushal 2011-06-16 11:02:49

+0

您是否試圖影響不同程序中的窗口? – 2011-06-16 11:08:16

回答

0

如何使用表單的Cursor屬性?

this.Cursor = System.Windows.Forms.Cursors.No; 
1

沒有必要使用本地Windows功能。

看看Cursor類,以及可以設置的控件的公開的Cursor屬性。

control.Cursor = Cursors.Hand; 
1

您可以使用光標類程式設計,這樣的改變,

 this.Cursor = Cursors.WaitCursor; 

要改回正常,

 this.Cursor = Cursors.Default; 
+0

我需要使用Unity3d窗口...所以它不能包含任何Windows窗體引用。 – Jesse 2011-06-16 21:52:17

2

我喜歡在try來包裝這個/ finally

try 
{ 
    this.Cursor = Cursors.Wait; 
} 
finally 
{ 
    this.Cursor = Cursors.Default; 
} 

這可確保您實際恢復光標 - 即使發生錯誤。我之前做過的事情(對於複雜的模態對話情境)有一疊光標,並在改變光標前將當前光標移動到堆棧上,在finally子句中再次彈出。

1
public Form1() 
    { 
     this.ClientSize = new System.Drawing.Size(292, 266); 
     this.Text = "Cursor Example"; 

     // The following generates a cursor from an embedded resource. 

     // To add a custom cursor, create a bitmap 
     //  1. Add a new cursor file to your project: 
     //    Project->Add New Item->General->Cursor File 

     // --- To make the custom cursor an embedded resource --- 

     // In Visual Studio: 
     //  1. Select the cursor file in the Solution Explorer 
     //  2. Choose View->Properties. 
     //  3. In the properties window switch "Build Action" to "Embedded Resources" 

     // On the command line: 
     //  Add the following flag: 
     //   /res:CursorFileName.cur,Namespace.CursorFileName.cur 
     //   
     //  Where "Namespace" is the namespace in which you want to use the cursor 
     //  and "CursorFileName.cur" is the cursor filename. 

     // The following line uses the namespace from the passed-in type 
     // and looks for CustomCursor.MyCursor.Cur in the assemblies manifest. 
    // NOTE: The cursor name is acase sensitive. 
     this.Cursor = new Cursor(GetType(), "MyCursor.cur"); 

    } 

http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.aspx