如何在C#中更改窗口的光标?

时间:2011-06-16 11:00:30

标签: c# window cursor

当我尝试将光标更改为自定义窗口时,对于单个窗口,使用SetCursor()函数(使用user32.dll),它会更改它,但是当鼠标开始移动时,光标将更改为默认值。所以,出现了一个问题,如何使用自定义光标更改单个窗口的光标?

5 个答案:

答案 0 :(得分:2)

我想将其包含在try / finally

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

这确保您实际还原光标 - 即使发生错误也是如此。我过去也做过的事情(对于复杂的模态对话框情况)有一堆游标,在更改光标之前将当前光标推送到堆栈,然后在finally子句中再次弹出它。 / p>

答案 1 :(得分:1)

无需使用本机Windows功能。

查看Cursor类,以及可以设置的控件的公开Cursor属性。

control.Cursor = Cursors.Hand;

答案 2 :(得分:1)

您可以使用游标类以编程方式更改它,如下所示,

     this.Cursor = Cursors.WaitCursor;

要将其恢复正常,

     this.Cursor = Cursors.Default; 

答案 3 :(得分: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

答案 4 :(得分:0)

如何使用表单的Cursor属性?

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