Reputation: 83
I made a tiny re-usable modal form that has a label (for a "Please wait" message) and a background worker. (We'll call it WaitForm) The form is meant to be re-usable within the application.
When "Load" fires, it will call the backgroundworker's DoWork event (which is delegated so that any code that calls on this form can do its own operation).
While it is running, I want all forms to display a wait cursor. Because this form is modal, the wait cursor will only appear while the user is hovering over the WaitForm. If you move your mouse and hover over the parent form instead, the cursor changes back to the default arrow.
I've tried the following, both individually and in combinations with others :
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.
The WaitForm works as intended. It displays and launches the backgroundworker. The only thing that makes me grind my teeth is the cursor. Am I missing something obvious?
Upvotes: 0
Views: 7305
Reputation: 2069
Four years later (now, 2022) the DllImport method feels a bit depricated and the second answer may be useful for an ASP controller, however it does not work for Net 4.7.2 and Winforms as asked.
Use this.Cursor = Cursors.WaitCursor; instead, in a Form method.
Restore the arrow cursor with this.Cursor = Cursors.Arrow;
Upvotes: 0
Reputation: 1921
for a waiting cursor in a user controller I use:
this.UseWaitCursor = true;
Upvotes: 0
Reputation: 65554
Looks like a limitation that I believe is "By Design", you might want to resort to using a Win32 API to override the standard Winform behaviour.
[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;
...
//========SET WINDOWS CURSOR========================================
IntPtr cursor = LoadCursorFromFile("example.cur");
bool ret_val = SetSystemCursor(cursor, OCR_NORMAL);
//========SET WINDOWS CURSOR========================================
//========SET FORM CURSOR========================================
IntPtr cursor = LoadCursorFromFile("example.cur");
ColoredCursor = new Cursor(cursor);
this.Cursor = ColoredCursor;
//========SET FORM CURSOR========================================
//========SET FORM CURSOR FROM IMAGE==============================
Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
Graphics.FromImage(hh);
IntPtr ptr = hh.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
//========SET FORM CURSOR FROM IMAGE==================================
Ref: http://www.pinvoke.net/default.aspx/user32.setcursor
For additional examples see here: 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);
}
}
Upvotes: 1