Short solution for short problems

Wait cursor for Windows forms

I think that every good developer has to be aware not to invent the wheel again and again. I found a great opensource project that helps window forms applications build in a wait cursor every time the system is doing some work.

You will need to reference the dll in your project and put these 2 lines somewhere in the startup of your application:

ApplicationWaitCursor.Cursor = Cursors.WaitCursor;

That’s it!

ApplicationWaitCursor.Delay = new TimeSpan(0, 0, 0, 1, 0);  // Delay of 1 second
ApplicationWaitCursor.Delay = new TimeSpan(0, 0, 0, 0, 250);  // Delay of 250 mili seconds

You can find a demo project and the source code here: http://www.codeproject.com/cs/miscctrl/WaitCursor.asp

I used it myself and must admit that this code works really fine. You can see that the developer has put some effort in it! 

I only notice one small problem during testing. When I clicked on a scroll bar in a listbox, and hold the mouse button down for a while to scroll, the wait cursor appeared.

You can filter events, below I adapted the ApplicationWaitCursor.cs and added the WM_LBUTTONDOWN message. I declared the varible : private const int WM_LBUTTONDOWN = 0x0201; on top and added this code:||
m.Msg == WM_LBUTTONDOWN to the if in the MessageFilter.PreFilterMessage method. Now when I left click on the mouse and keep it down, the wait cursor is not shown. But now every time whe do a left mouse click, the wait cursor will not be shown. So I decided to leave it this way and set the delay at 500 ms. Apart from this small problem, the application is now much user friendly because every time the system is doing some work, the wait cursor informs this to the user.

Here are some pre filtered messages I pickup up from comments on the post:

bool IMessageFilter.PreFilterMessage(ref Message m)
if (
m.Msg == WM_RBUTTONUP || 
m.Msg == WM_SYSKEYUP ||
_cursor.Enabled = false;
...Message defitions are:
private const int WM_LBUTTONDOWN = 0x0201;
private const int WM_RBUTTONUP = 0x0205;
private const int WM_CONTEXTMENU = 0x007B;
private const int WM_SYSKEYUP = 0x0105;
private const int WM_SYSKEYDOWN = 0x0104
The list of all mouse events: http://msdn2.microsoft.com/en-us/library/ms674824.aspx