TrayInfo.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Some interop code taken from Mike Marshall's AnyForm
  2. using System.Drawing;
  3. namespace Hardcodet.Wpf.TaskbarNotification.Interop
  4. {
  5. /// <summary>
  6. /// Resolves the current tray position.
  7. /// </summary>
  8. public static class TrayInfo
  9. {
  10. /// <summary>
  11. /// Gets the position of the system tray.
  12. /// </summary>
  13. /// <returns>Tray coordinates.</returns>
  14. public static Point GetTrayLocation()
  15. {
  16. int space = 2;
  17. var info = new AppBarInfo();
  18. info.GetSystemTaskBarPosition();
  19. Rectangle rcWorkArea = info.WorkArea;
  20. int x = 0, y = 0;
  21. switch (info.Edge)
  22. {
  23. case AppBarInfo.ScreenEdge.Left:
  24. x = rcWorkArea.Right + space;
  25. y = rcWorkArea.Bottom;
  26. break;
  27. case AppBarInfo.ScreenEdge.Bottom:
  28. x = rcWorkArea.Right;
  29. y = rcWorkArea.Bottom - rcWorkArea.Height - space;
  30. break;
  31. case AppBarInfo.ScreenEdge.Top:
  32. x = rcWorkArea.Right;
  33. y = rcWorkArea.Top + rcWorkArea.Height + space;
  34. break;
  35. case AppBarInfo.ScreenEdge.Right:
  36. x = rcWorkArea.Right - rcWorkArea.Width - space;
  37. y = rcWorkArea.Bottom;
  38. break;
  39. }
  40. return GetDeviceCoordinates(new Point {X = x, Y = y});
  41. }
  42. /// <summary>
  43. /// Recalculates OS coordinates in order to support WPFs coordinate
  44. /// system if OS scaling (DPIs) is not 100%.
  45. /// </summary>
  46. /// <param name="point">Point</param>
  47. /// <returns>Point</returns>
  48. public static Point GetDeviceCoordinates(Point point)
  49. {
  50. return new Point
  51. {
  52. X = (int)(point.X / SystemInfo.DpiFactorX),
  53. Y = (int)(point.Y / SystemInfo.DpiFactorY)
  54. };
  55. }
  56. }
  57. }