Search code examples
.netwpfinteropoffice-interopoutlook-addin

Getting DPI from an outlook application


I am making an outlook addin, and trying to place a window in the center of outlook. in order to achieve that, I used to do the following:

Outlook.Application olApp;
...
dynamic activeWindow = olApp.ActiveWindow();
wpf_ui_control.Left = activeWindow.Left + (activeWindow.Width / 2) - (wpf_ui_control.Width / 2);
wpf_ui_control.Top = activeWindow.Top + (activeWindow.Height / 2) - (wpf_ui_control.Height / 2);

this of course, proved useless when DPI is not 100%.

I've seen a few examples getting DPI from different controls, but none from an outlook.explorer \ outlook.Inspect (the results of .ActiveWindow() ).

How will I go about getting the DPI, or calculating the center in this case?

Thanks


Solution

  • A good solution was actually suggested here: How can I get the DPI in WPF?

    var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
    var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);
    
    var dpiX = (int)dpiXProperty.GetValue(null, null);
    var dpiY = (int)dpiYProperty.GetValue(null, null);