When the LoadCursor function is used to load a cursor from a resource, the resulting HCURSOR can be used across different monitors and always appears at the correct size.
ie: typically:
- on a 96 dpi monitor the 32x32 resource is used,
- on a 192 dpi monitor the 64x64 resource is used.
However, when a cursor is programmatically created from memory (say using LookupIconIdFromDirectoryEx and CreateIconFromResourceEx) the resulting cursor has a fixed resolution. This means it appears at the wrong size on at least one monitor in a mixed DPI multi-monitor setup.
I also checked out the LoadCursorFromFile and it too provides this dynamic resolution behaviour like LoadCursor.
Is there a way to programmatically create a cursor that dynamically switches depending which monitor it's shown on? What magic is going on behind the scenes for cursors loaded with LoadCursor to work differently?
After much experimentation I finally discovered that WPF can load cursors from resource and memory streams and get the correct DPI behaviour if the
scaleWithDpioption is used:Looking at the reference source it ends up in the function
LoadFromStreamwhich loads the stream by writing it to a temporary file and the loading from the file. See sourceTo sum up:
LR_DEFAULTSIZEflag passed to theLoadImagefunction.