Search code examples
c#c++wpfwinapicursor

WinAPI / WPF: Set system cursor only for application


I have an application whose UI is custom rendered with a theme. I also want to add new non-default-cursors (like the resize cursors when the user intends to resize the window) to match that theme.

With the WinAPI function SetCursor I am only able to change the default pointer for the application, but this is not enough, so I looked up SetSystemCursor which works just fine. Problem is: The latter version changes the cursors system-wide permanently, but I only want them to be changed for my application only.

I thought about copying the previous cursors before I do the SetSystemCursor and re-set them at application exit, but even when I implement a terminate handler it may not be called if the process e.g. just crashes. Also the cursors would still be changed system-wide as long as the application is running.

Specifically for the resize-cursors, I could just drop the window style, make a borderless window, and render/implement the resizing grips and logic myself (then I could just hook the mouse-over events), but that is really just my last resort - I'd rather like to know if it is possible achieve my goal before I do this cumbersome task...

For anyone who is interested: The UI is rendered with WPF, but WPF doesn't provide this functionality either (again, they have it, but just for the default pointer). It's no concern for me to use the WinAPI or other "low-level" calls if I have to, but at the moment it seems there are none that are fitting my needs :(

So if anybody knows how to change the other system cursors (not the default pointer) in WPF or WinAPI just for my application without having to implement custom cursor-logic to my window just for that, you would make my day.


Solution

  • I don't know how to implement this in WPF, but if you want to set the cursor to something other than default, then your window procedure should handle the WM_SETCURSOR message.

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms648382(v=vs.85).aspx