I'm attempting to finalise high DPI support for an Office COM Add-In running .NET Framework 4.6.2. I'm following the principles outlined by Microsoft for handling DPI Scaling in Office. The only approach that can be used for Office add-ins is to set the DPI awareness per-thread, as the typical manifest and startup solutions aren't available.
The form I'm scaling is a Forms window hosting a WPF UserControl in an Element Host. This was proposed as a working solution and I've observed it being used successfully in demo code provided by Add-In Express.
As recommended by Microsoft, I'm opening my form after setting the thread DPI context to Per Monitor. Using this, the demo code provided above by Add-In Express is being used to apply a LayoutTransform
to the UserControl, and scale all elements to the correct size.
private void ApplyScale(int dpi)
{
var source = (HwndSource) PresentationSource.FromVisual(this);
if (source is null) return;
var wpfDpi = 96 * source.CompositionTarget.TransformToDevice.M11;
var scaleFactor = dpi / wpfDpi;
var scaleTransform = Math.Abs(scaleFactor - 1.0) < 0.001 ? null : new ScaleTransform(scaleFactor, scaleFactor);
this.SetValue(LayoutTransformProperty, scaleTransform);
}
This is working as expected, and the window is successfully scaling on each monitor with differing DPI, increasing the size of both window and contents to the correct size.
The issue arises when I attempt to interact with the scaled content. On monitors above 100% scaling, a percentage of the right and bottom edges of the window cannot be interacted with correctly. I have Save and Cancel buttons at the bottom right corner of the form, and when mousing over them the hover event will flicker, but they cannot be clicked normally. This is also true of a ListView featured in the form.
I can confirm the thread is definitely set to Per Monitor awareness, and the visual scaling is working perfectly. No elements are overlapping, and the size of the area that can't be clicked increases with scale.
I'm two days into debugging this and have attempted everything I can think of. What could be causing this?