How can i change cursor for a window in C#?
When i'm trying to change cursor to a custom one, for a single window, with Set开发者_JAVA百科Cursor() function (using user32.dll), it changes it, but when mouse starts moving, cursor changes to default one. So, there's a question appeared, how can i change cursor for a single window, with a custom cursor?
I like to wrap this in a try
/ finally
:
try
{
this.Cursor = Cursors.Wait;
}
finally
{
this.Cursor = Cursors.Default;
}
This ensures that you actually revert the cursor back - even if an error happens. What I've also done in the past (for complicated modal dialog situations) is have a stack of cursors and push the current cursor on to the stack before changing the cursor, popping it off again in the finally
clause.
There is no need to use native Windows functions.
Take a look at the Cursor
class, and the exposed Cursor
property of controls, which you can set.
control.Cursor = Cursors.Hand;
You can change it using the cursor class programtically, like this,
this.Cursor = Cursors.WaitCursor;
To change it back to normal,
this.Cursor = Cursors.Default;
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
how about using Cursor property of the form?
this.Cursor = System.Windows.Forms.Cursors.No;
精彩评论