3

There are a lot of posts online about setting DPI awareness for a Winforms application by calling SetProcessDPIAware() (or by making some related changes to the application's manifest). For example:

How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?

However, I am unsure how and where to call SetProcessDPIAware() in a VSTO add-in for Excel, PowerPoint, etc. I have experimented with this for a while, but cannot get it working as the add-in's various Winforms are still not scaling correctly on high-DPI displays.

Can anyone provide guidance on how to implement DPI awareness in VSTO add-ins, specifically?

Community
  • 1
  • 1
OfficeAddinDev
  • 1,105
  • 3
  • 15
  • 29

2 Answers2

1

You can't influence DPI awareness here, because this is a per-process setting, and your process is Excel itself (which is DPI aware). You can only use the SetProcessDPIAware() function only before your program calls the first DPI related Windows function, which Excel did long before your Add-In even started. However, don't expect Winforms to scale correctly as magic when DPI awareness ist enabled. Enabling DPI awareness means nothing more than: Opening the box of pandora for all the nasty High DPI effects Winforms offers for your pleasure. You will have to fiddle around with control distances and sizes yourself. Statement from a Microsoft official: If you want DPI awareness, don't use Winforms, use WPF If you want Touch, don't use WPF, use UWP...

Heinz Kessler
  • 1,610
  • 11
  • 24
  • 2
    My conclusion would then be that, since UHD and 4K displays are becoming more widely used, Winforms should no longer be used to develop VSTO add-ins for Office unless you are prepared to develop some intense DPI and rescaling logic and have a solution for scaling of non-vector images. No comparison of Winforms vs. WPF I found adequately highlights this, but this should be a top consideration for VSTO developers. – OfficeAddinDev Mar 17 '17 at 19:51
1

The way we solved this in our Chemistry Add-in for Microsoft Word (Chem4Word) was to have a WPF user control hosted inside a WinForms control

WinForm
+- ElementHost
   +- WPF User Control

  • 1
    Right. UHD and 4K displays effectively killed Winforms. Winforms is only good for hosting WPF via ElementHosts when required by custom task panes and modeless dialogs in Excel. – OfficeAddinDev Jan 07 '20 at 21:26