DpiHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. /**************************************************************************\
  11. Copyright Microsoft Corporation. All Rights Reserved.
  12. \**************************************************************************/
  13. namespace Standard
  14. {
  15. using System;
  16. using System.Diagnostics.CodeAnalysis;
  17. using System.Windows;
  18. using System.Windows.Media;
  19. internal static class DpiHelper
  20. {
  21. private static Matrix _transformToDevice;
  22. private static Matrix _transformToDip;
  23. [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
  24. static DpiHelper()
  25. {
  26. using (SafeDC desktop = SafeDC.GetDesktop())
  27. {
  28. // Can get these in the static constructor. They shouldn't vary window to window,
  29. // and changing the system DPI requires a restart.
  30. int pixelsPerInchX = NativeMethods.GetDeviceCaps(desktop, DeviceCap.LOGPIXELSX);
  31. int pixelsPerInchY = NativeMethods.GetDeviceCaps(desktop, DeviceCap.LOGPIXELSY);
  32. _transformToDip = Matrix.Identity;
  33. _transformToDip.Scale(96d / (double)pixelsPerInchX, 96d / (double)pixelsPerInchY);
  34. _transformToDevice = Matrix.Identity;
  35. _transformToDevice.Scale((double)pixelsPerInchX / 96d, (double)pixelsPerInchY / 96d);
  36. }
  37. }
  38. /// <summary>
  39. /// Convert a point in device independent pixels (1/96") to a point in the system coordinates.
  40. /// </summary>
  41. /// <param name="logicalPoint">A point in the logical coordinate system.</param>
  42. /// <returns>Returns the parameter converted to the system's coordinates.</returns>
  43. public static Point LogicalPixelsToDevice(Point logicalPoint)
  44. {
  45. return _transformToDevice.Transform(logicalPoint);
  46. }
  47. /// <summary>
  48. /// Convert a point in system coordinates to a point in device independent pixels (1/96").
  49. /// </summary>
  50. /// <param name="logicalPoint">A point in the physical coordinate system.</param>
  51. /// <returns>Returns the parameter converted to the device independent coordinate system.</returns>
  52. public static Point DevicePixelsToLogical(Point devicePoint)
  53. {
  54. return _transformToDip.Transform(devicePoint);
  55. }
  56. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  57. public static Rect LogicalRectToDevice(Rect logicalRectangle)
  58. {
  59. Point topLeft = LogicalPixelsToDevice(new Point(logicalRectangle.Left, logicalRectangle.Top));
  60. Point bottomRight = LogicalPixelsToDevice(new Point(logicalRectangle.Right, logicalRectangle.Bottom));
  61. return new Rect(topLeft, bottomRight);
  62. }
  63. public static Rect DeviceRectToLogical(Rect deviceRectangle)
  64. {
  65. Point topLeft = DevicePixelsToLogical(new Point(deviceRectangle.Left, deviceRectangle.Top));
  66. Point bottomRight = DevicePixelsToLogical(new Point(deviceRectangle.Right, deviceRectangle.Bottom));
  67. return new Rect(topLeft, bottomRight);
  68. }
  69. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  70. public static Size LogicalSizeToDevice(Size logicalSize)
  71. {
  72. Point pt = LogicalPixelsToDevice(new Point(logicalSize.Width, logicalSize.Height));
  73. return new Size { Width = pt.X, Height = pt.Y };
  74. }
  75. public static Size DeviceSizeToLogical(Size deviceSize)
  76. {
  77. Point pt = DevicePixelsToLogical(new Point(deviceSize.Width, deviceSize.Height));
  78. return new Size(pt.X, pt.Y);
  79. }
  80. }
  81. }