diff options
author | Alex Corrado <alexc@xamarin.com> | 2013-11-12 06:43:53 +0400 |
---|---|---|
committer | Alex Corrado <alexc@xamarin.com> | 2013-11-12 06:45:48 +0400 |
commit | 7b92e6ef15212d29525220d21ce8c035cebfe304 (patch) | |
tree | 8cb50c8e07dc0a8ec66d25847b6d41e443743c98 /Xwt.WPF | |
parent | 67a97ff70ffd5b488a4073ae7abad2173054c6c5 (diff) |
[WPF] Implement WpfDesktopBackend.GetScaleFactor
Diffstat (limited to 'Xwt.WPF')
-rw-r--r-- | Xwt.WPF/Xwt.WPFBackend/WpfDesktopBackend.cs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Xwt.WPF/Xwt.WPFBackend/WpfDesktopBackend.cs b/Xwt.WPF/Xwt.WPFBackend/WpfDesktopBackend.cs index b07c6d3b..1a0fa7e9 100644 --- a/Xwt.WPF/Xwt.WPFBackend/WpfDesktopBackend.cs +++ b/Xwt.WPF/Xwt.WPFBackend/WpfDesktopBackend.cs @@ -24,6 +24,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; +using System.Runtime.InteropServices; using Xwt.Backends; using SWF = System.Windows.Forms; using System.Collections.Generic; @@ -32,6 +33,9 @@ namespace Xwt.WPFBackend { public class WpfDesktopBackend: DesktopBackend { + // http://msdn.microsoft.com/en-us/library/windows/desktop/dd464660(v=vs.85).aspx#determining_the_dpi_scale_factor + const double BASELINE_DPI = 96d; + public WpfDesktopBackend () { Microsoft.Win32.SystemEvents.DisplaySettingsChanged += delegate @@ -40,6 +44,23 @@ namespace Xwt.WPFBackend }; } + public override double GetScaleFactor (object backend) + { + var hdc = GetDC (IntPtr.Zero); + if (hdc == IntPtr.Zero) { + // GetDC failed for some reason + return base.GetScaleFactor (backend); + } + try { + //FIXME: Is it possible for the Y dpi to differ from the X dpi, + // and if so, what should we do about it? + var dpiX = GetDeviceCaps (hdc, LOGPIXELSX); + return dpiX / BASELINE_DPI; + } finally { + ReleaseDC (IntPtr.Zero, hdc); + } + } + #region implemented abstract members of DesktopBackend public override Point GetMouseLocation() @@ -76,6 +97,16 @@ namespace Xwt.WPFBackend } #endregion + + #region P/Invoke + + const int LOGPIXELSX = 88; + const int LOGPIXELSY = 90; + + [DllImport ("user32")] static extern IntPtr GetDC (IntPtr hWnd); + [DllImport ("user32")] static extern int ReleaseDC (IntPtr hWnd, IntPtr hdc); + [DllImport ("gdi32")] static extern int GetDeviceCaps (IntPtr hdc, int nIndex); + #endregion } } |