0

I have a button on a (c#) WinForm, and when it is pressed (mouse down then up) I would like to change the mouse cursor to a custom icon. I would like that custom cursor icon to remain regardless of mouse position on the screen area (over source app, other apps, desktop, etc.) until the mouse is clicked (mouse down then up). After this second click I want the cursor to revert back to its default behavior.

I'm currently using the global mouse hook method outlined by Dan Silk (with adjustment from Hans Passant) to capture global mouse move and click events.

I think I need to intercept (and subsequently stop) WM_SETCURSOR messages (which according to Hans follow any mouse move). However I'm not sure how to do this for things beyond the source app, which Reza Aghaei outlined as follows:

const int WM_SETCURSOR = 0x0020;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_SETCURSOR)
        Cursor.Current = myCustomCursor;
    else
        base.WndProc(ref m);
}

When I tried to use the above WndProc method for adjusting the cursor for just the source app, I still got cursor flickering. Is there a proper way to stop the WM_SETCURSOR message sending/posting?

Any help or suggestions would be most appreciated!


UPDATE

I decided to go at my problem from a different angle to avoid fighting WM_SETCURSOR messages entirely. What I have now works fine, however if there is an answer floating out there you are welcome to post it for posterity.

Exergist
  • 157
  • 12

1 Answers1

1

Couple of these events works for me globally no matter which application is mouse over:

private static Cursor _customCursor = new Cursor(@"C:\path\Hand.cur");

private void button1_MouseDown(object sender, MouseEventArgs e)
{
    Cursor = _customCursor;
}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    Cursor = Cursors.Default;
}

Beer custom cursor outside the form

Does it cover your needs or I have missed something important here?

Tomas Paul
  • 490
  • 5
  • 13
  • I believe those will only work while the cursor is within the bounds of the parent form where these methods reside. – Exergist Apr 15 '20 at 21:09
  • Hi @Exergist, I pasted the screenshot from my test WinForms application just to show how does it look. – Tomas Paul Apr 15 '20 at 21:51