George Shepherd's Windows Forms FAQ
Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion.

27. Windows Forms Cursors

WinForms FAQ Home
   27.1 I set the wait cursor using Cursor.Current = Cursors.WaitCursor;. Why does does it disappear before I want it to?
   27.2 How do I change the cursor for a control?
   27.3 How to convert a Cursor class to a .cur file?
   27.4 How to load and display a cursor from a resource manifest?



27.1 I set the wait cursor using Cursor.Current = Cursors.WaitCursor;. Why does does it disappear before I want it to?


Setting the Current property changes the cursor and stops the processing of mouse events. Setting the cursor back to Cursors.Default restarts the mouse event processing and displays the proper cursor for each control. If a DoEvents is called before you reset the cursor back to the default, this will also start up the mouse event processing and you will lose the particular cursor you set. So, if your WaitCursor is disappearing, one explanation might be that DoEvents is getting called.

Here is some code that sets a WaitCursor.

     Cursor oldCursor = Cursor.Current;
     Cursor.Current = Cursors.WaitCursor;
     try
     {
          // Do your processing that takes time...
          // eg. let's wait for 2 seconds...
          DateTime dt = DateTime.Now.AddSeconds(2);
          while(dt > DateTime.Now)
          { //do nothing     
          }
     }
     finally
     {
          Cursor.Current = oldCursor;
     }


27.2 How do I change the cursor for a control?


Try

     button1.Cursor = new System.Windows.Forms.Cursor(@"C:\winnt\cursors\hnodrop.cur");


27.3 How to convert a Cursor class to a .cur file?


     protected void WriteCursorToFile(Cursor cursor, string fileName)
     {
          byte[] blob = TypeDescriptor.GetConverter(typeof(System.Windows.Forms.Cursor)).ConvertTo(cursor, typeof(byte[])) as byte[];

          if(blob != null)
          {
               FileStream fileStream = new FileStream(fileName, FileMode.Create);
               fileStream.Write(blob, 0, blob.Length);
               fileStream.Flush();
               fileStream.Close();
          }
          else
               MessageBox.Show("Unable to convert Cursor to byte[]");
     }


27.4 How to load and display a cursor from a resource manifest?


System.IO.Stream strm = null;
try
{
     string curName = "WindowsApplication1.Cursor1.cur";
     strm = this.GetType().Assembly.GetManifestResourceStream(curName);
     this.Cursor = new Cursor(strm);
}
catch(Exception ex)
{
     MessageBox.Show(ex.Message.ToString());
}
finally
{
     if(strm != null)
          strm.Close();
}

© 2001-2009 Copyright Syncfusion Inc. All rights reserved.  |  Privacy Policy  |  Contact  |  Sitemap