Misbehaving Button in wrong location when DPI scaling is above 96

Add-in Express™ Support Service
That's what is more important than anything else

Misbehaving Button in wrong location when DPI scaling is above 96
 
Rhys Hicks




Posts: 10
Joined: 2021-01-21
Hi,

We are running into a bit of issue with one of our UserControls that sits on a AddInExpress Panel. The app is running in Word.

Essentially we have a button on a UserControl thats button.Text is changed multiple times throughout the application life-cycle. On this button we have the AutoSize properties setup correctly (set to True and GrowAndShrink), and this works fine on regular screen DPI resolutions @ 96.

However coming into an issue when a user of our app is on a DPI of 120 (or more) - the button will sometimes appear fine and adjust to the text, however we are seeing issues where it will not adjust to the full width of the text and it also re-locates the button in the centre of the usercontrol instead of bottom right as it is setup to be. When this happens, if we adjust the size of the whole AddInExpress panel just slightly, the button is re-adjusted correctly and then re-located to the correct position.

If feels like under certain circumstances the drawing of this button is somehow interrupted during the initial load and then correctly set when you make some minor adjustment to the AddInExpress pane.

Anyone have any ideas whats going on or what else I can try? We are using
AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F)
and
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
for all our controls.

What I will add is that we instantiate all of our user controls programmatically, and add to a user control that has a TableLayoutPanel that we use to order the user controls on our application. The button and user control in question are nested 2 controls deep if that helps?

ADXWordTaskPane
      - TableLayoutPanel
          - UserControl 1
          - UserControl 2
          - UserControl 3
              - UserControl 3a <= a Button on this control has the PROBLEM when DPI is 120+
              - UserControl 3b



If I can provide anymore information let me know.
Posted 17 Aug, 2021 09:24:10 Top
Alexander Solomenko


Add-in Express team


Posts: 140
Joined: 2009-02-27
Hello,

Could you please send us a demo project? Please find the support email address in {Add-in Express installation folder}\readme.txt. And make sure your email contains a link to this topic.
Regards,
Aleksandr Solomenko
Posted 17 Aug, 2021 10:57:43 Top
Rhys Hicks




Posts: 10
Joined: 2021-01-21
Hi Alexander,

Unfortunately I have tried to recreate this scenario in a demo project but it does not show the same behavior. This behavior is a small part of a much bigger app, this behavior also seems to be isolated to this user control. I have not seen this before. It seems like before the button can fully render it is interrupted which leaves it out of place and with a slightly shorter width than what its supposed to be. This works fine under normal DPI 96
Posted 17 Aug, 2021 22:22:25 Top
Alexander Solomenko


Add-in Express team


Posts: 140
Joined: 2009-02-27
Hello Rhys,

We suppose the issue is related to multi-DPI support. It looks like the DPI context of the button or the control on which the button is placed is different from the DPI context where we try to do some manipulations with this button. You can get and set this context by using the following functions: GetThreadDpiAwarenessContext, SetThreadDpiAwarenessContext, GetWindowDpiAwarenessContext (see MSDN help).
If these contexts (see above) are not the same, the control’s behavior may be unpredictable.

It will be useful for you to read these Microsoft articles and our blog to understand more on this topic.

https://docs.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows
https://www.add-in-express.com/creating-addins-blog/2021/06/10/office-addins-dpi-awareness-research-analysis/
https://www.add-in-express.com/creating-addins-blog/2021/06/17/dpi-scaling-details-office-addins/
Regards,
Aleksandr Solomenko
Posted 18 Aug, 2021 03:57:48 Top
Rhys Hicks




Posts: 10
Joined: 2021-01-21
Thank you Alexander for this information! I have used parts of the demo app in the blog you linked (https://www.add-in-express.com/creating-addins-blog/2021/05/25/windows-mutli-dpi-scaling-in-an-office-com-add-ins/) to get an idea on current values of the DPI.

So it looks like the ADXWordTaskpane and the button on the nested control both think they are using DPI 96, while the control which the button sits on thinks its DPI 120. Below is the printout of these results, also note the WindowAwareness of the Control cannot be determined:

ADXWordTaskPane
txtThreadAwareness - DPI_AWARENESS_PER_MONITOR_AWARE
txtProcessAwareness - Process_Per_Monitor_DPI_Aware
txtWindowAwareness - DPI_AWARENESS_PER_MONITOR_AWARE
txtWindowDpi - 96


Problematic Button
txtThreadAwareness - DPI_AWARENESS_PER_MONITOR_AWARE
txtProcessAwareness - Process_Per_Monitor_DPI_Aware
txtWindowAwareness - DPI_AWARENESS_PER_MONITOR_AWARE
txtWindowDpi - 96


UserControl
txtThreadAwareness - DPI_AWARENESS_PER_MONITOR_AWARE
txtProcessAwareness - Process_Per_Monitor_DPI_Aware
txtWindowAwareness - can't determine
txtWindowDpi - 120


HANDLE 1050932 ADX
txtThreadAwareness - DPI_AWARENESS_PER_MONITOR_AWARE
txtProcessAwareness - Process_Per_Monitor_DPI_Aware
txtWindowAwareness - DPI_AWARENESS_PER_MONITOR_AWARE
txtWindowDpi - 96


HANDLE 2427082 buttonSeeMore
txtThreadAwareness - DPI_AWARENESS_SYSTEM_AWARE
txtProcessAwareness - Process_System_DPI_Aware
txtWindowAwareness - DPI_AWARENESS_PER_MONITOR_AWARE
txtWindowDpi - 96


HANDLE 0 Control
txtThreadAwareness - DPI_AWARENESS_SYSTEM_AWARE
txtProcessAwareness - Process_System_DPI_Aware
txtWindowAwareness - can't determine
txtWindowDpi - 120


(This is just a snippet of the log)
Posted 18 Aug, 2021 05:43:15 Top
Alexander Solomenko


Add-in Express team


Posts: 140
Joined: 2009-02-27
Rhys,

You should understand that DPI and DPIAwarenessContext are different things. DPI is a property that refers to the monitor and DPIAwarenessContext is a property of the thread in which the code is executed.
Regards,
Aleksandr Solomenko
Posted 18 Aug, 2021 06:49:44 Top
Rhys Hicks




Posts: 10
Joined: 2021-01-21
HI Aleksandr,

Thanks for the clarification - this was the same code used in the example I mentioned, I was only interested in DPI anyways well but I thought if it could provide more information then I logged that as well.
Posted 18 Aug, 2021 08:27:43 Top