0

Is there any easy way to change default cursors in windows forms?

e.g. I want to change Cursors.SizeWE to a custom cursor.

Currently using a dirty hack like that:

// https://github.com/dotnet/winforms/blob/07a7823865ef0409f34af626f75718c33b246781/src/System.Windows.Forms/src/System/Windows/Forms/Cursors.cs#L14
var propertyInfo = typeof(Cursors).GetField("s_sizeWE", BindingFlags.NonPublic | BindingFlags.Static);
propertyInfo.SetValue(null, myCustomSizeWEcursor);

I also tried changing it in CursorChanged event but it was glitchy and the old cursor was still visible sometimes.

I don't get why it's read-only btw.

Konrad
  • 6,385
  • 12
  • 53
  • 96
  • 2
    Override WndProc() to handle the [WM_SETCURSOR](https://learn.microsoft.com/en-us/windows/win32/menurc/wm-setcursor) notification. – Hans Passant Aug 21 '20 at 17:39
  • That would work but I would need to override WndProc() in every single control https://stackoverflow.com/questions/18279938/how-to-handle-wm-setcursor-in-c-sharp so I can't really do it application-wide there. LParam & 0xFFFF is always == HTCLIENT – Konrad Aug 21 '20 at 18:46
  • @Konrad Why setting a custom cursor icon dont suit your needs? like `Icon ico = new Icon(@"C:\Users\john\Desktop\sys_ico_13.ico"); this.Cursor = new Cursor(ico.Handle);` – Jonathan Applebaum Aug 22 '20 at 06:56
  • @jonathana This won't allow you to change cursor icons when you e.g. resize column in a DataGridView. You would need to create your own resizing functionality from scratch or create a custom DataGridView or other default control from scratch. I already solved the problem with the snippet in the question. I am just curious if there is any cleaner way but I doubt so. – Konrad Aug 22 '20 at 10:11

0 Answers0