AppBarInfo.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Some interop code taken from Mike Marshall's AnyForm
  2. using System;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices;
  5. namespace Hardcodet.Wpf.TaskbarNotification.Interop
  6. {
  7. public class AppBarInfo
  8. {
  9. [DllImport("user32.dll")]
  10. private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  11. [DllImport("shell32.dll")]
  12. private static extern uint SHAppBarMessage(uint dwMessage, ref APPBARDATA data);
  13. [DllImport("user32.dll")]
  14. private static extern int SystemParametersInfo(uint uiAction, uint uiParam,
  15. IntPtr pvParam, uint fWinIni);
  16. private const int ABE_BOTTOM = 3;
  17. private const int ABE_LEFT = 0;
  18. private const int ABE_RIGHT = 2;
  19. private const int ABE_TOP = 1;
  20. private const int ABM_GETTASKBARPOS = 0x00000005;
  21. // SystemParametersInfo constants
  22. private const uint SPI_GETWORKAREA = 0x0030;
  23. private APPBARDATA m_data;
  24. public ScreenEdge Edge
  25. {
  26. get { return (ScreenEdge) m_data.uEdge; }
  27. }
  28. public Rectangle WorkArea
  29. {
  30. get { return GetRectangle(m_data.rc); }
  31. }
  32. private Rectangle GetRectangle(RECT rc)
  33. {
  34. return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
  35. }
  36. public void GetPosition(string strClassName, string strWindowName)
  37. {
  38. m_data = new APPBARDATA();
  39. m_data.cbSize = (uint) Marshal.SizeOf(m_data.GetType());
  40. IntPtr hWnd = FindWindow(strClassName, strWindowName);
  41. if (hWnd != IntPtr.Zero)
  42. {
  43. uint uResult = SHAppBarMessage(ABM_GETTASKBARPOS, ref m_data);
  44. if (uResult != 1)
  45. {
  46. throw new Exception("Failed to communicate with the given AppBar");
  47. }
  48. }
  49. else
  50. {
  51. throw new Exception("Failed to find an AppBar that matched the given criteria");
  52. }
  53. }
  54. public void GetSystemTaskBarPosition()
  55. {
  56. GetPosition("Shell_TrayWnd", null);
  57. }
  58. public enum ScreenEdge
  59. {
  60. Undefined = -1,
  61. Left = ABE_LEFT,
  62. Top = ABE_TOP,
  63. Right = ABE_RIGHT,
  64. Bottom = ABE_BOTTOM
  65. }
  66. [StructLayout(LayoutKind.Sequential)]
  67. private struct APPBARDATA
  68. {
  69. public uint cbSize;
  70. public IntPtr hWnd;
  71. public uint uCallbackMessage;
  72. public uint uEdge;
  73. public RECT rc;
  74. public int lParam;
  75. }
  76. [StructLayout(LayoutKind.Sequential)]
  77. private struct RECT
  78. {
  79. public int left;
  80. public int top;
  81. public int right;
  82. public int bottom;
  83. }
  84. }
  85. }