More on DPI-awareness

I already blogged about Visual Studio extensions and DPI-awareness, where I detailed my experience converting my MZ-Tools extension to DPI-awareness for the most part (I haven’t updated the toolbar buttons yet because the required API only works on VS 2015 and higher).

I also mentioned how to run Visual Studio or setups with DPI virtualization (DPI-unaware) on high DPI displays, to overcome some display problems that can happen designing Windows Forms or running some old SDK setups.

My problems didn’t end there, though. One of my customers reported me a bug that happened when using a laptop with a retina display connected to a non-retina external display. I was familiar with the problem because I have suffered it too, since I own a Surface Pro 4 with a dock station that uses a non-retina display. With some combination of undocking / restart / docking, I could end with this problem using MZ-Tools 8.0 for VBA in the VBA editor of Office (click to enlarge):

I thought that I was the only person with that problem but after the bug report I knew it affected other users. Also, I noticed that when the problem happened, Notepad++ was also affected in the document tabs, and it was already reported here, here and here:

My add-in uses Windows Forms, but Notepad++ uses GDI, so it is not a problem of Windows Forms but of GDI.

Aa a workaround, I had to change all the forms and usercontrols of my add-in to detect if the font was “too big” and in such case adjust its size. Technically, the System.Drawing.SystemFonts.DefaultFont is returned huge. It happens that internally it calls GetStockObject(DEFAULT_GUI_FONT), which according to MSDN is not recommended. So, if the add-in detects that the font of the form/usercontrol is much bigger (say, two points) than the size of System.Drawing.SystemFonts.MessageBoxFont (which is not affected by the problem), then it changes the size. The problem is convoluted by the fact that I cannot reproduce it at will.

It seems that the DPI-awareness problem is so pervasive that Microsoft has invested a lot of work to fix or alleviate it on the Windows 10 Anniversary Update introducing the concept of “Mixed-Mode” DPI scaling, also known as sub-process DPI scaling, via the concept of the DPI-awareness context (MSDN documentation here) and I am glad to see that the investment will continue heavily on the new Windows  10 Creators Update, due later this year, that will introduce new enhancements, including a new DPI-awareness context (“Per-Monitor V2”), automatic DPI scaling for GDI-based apps, new Windows Forms DPI scaling improvements (for people like me still using Windows Forms) and other improvements for Internet Explorer and Windows desktop icons (that I suffer too). You can watch the following video by James Clarke and Peter Felts explaining the improvements and showing cool demos (new post and sample to follow soon):