I am creating a non-DPI-aware Winforms application. This application works fine on displays with high DPI scaling; Windows simply scales up the application as it should (it looks a bit fuzzy, but that's fine).
The problem is that the mouse cursor, which is created from a Bitmap
object using CreateIconIndirect
, is not scaled along with the application window, making it look really tiny.
Here's the code I use to create the mouse cursor from a Bitmap object:
[DllImport("user32.dll")]
private static extern bool GetIconInfo(IntPtr hIcon, out IconInfo pIconInfo);
[DllImport("user32.dll")]
private static extern IntPtr CreateIconIndirect(ref IconInfo icon);
/// <summary>
/// Creates a new Cursor object from the specified bitmap and hotspot.
/// </summary>
private static Cursor CreateCursor(Bitmap bmp, Point hotSpot)
{
IntPtr handle = bmp.GetHicon();
IconInfo tmp = new IconInfo();
GetIconInfo(handle, out tmp);
tmp.xHotspot = hotSpot.X;
tmp.yHotspot = hotSpot.Y;
tmp.fIcon = false;
handle = CreateIconIndirect(ref tmp);
return new Cursor(handle);
}
private struct IconInfo
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
So my question is: How can I get a cursor created using CreateIconIndirect to scale properly with the display's DPI scaling, without make my application DPI-aware?
I initially thought that I could simply get the display's DPI scaling and upscale my Bitmap myself, but none of the solutions for retrieving DPI in the following two posts work for me, presumably because my app isn't DPI-aware and would need to retrieve the DPI for the specific display on which the app is running. How to get Windows Display settings? How To Get DPI in C# .NET
This is on Windows 10 x64 using .Net 4.6.
Thanks!