NotifyIconData.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Hardcodet.Wpf.TaskbarNotification.Interop
  4. {
  5. /// <summary>
  6. /// A struct that is submitted in order to configure
  7. /// the taskbar icon. Provides various members that
  8. /// can be configured partially, according to the
  9. /// values of the <see cref="IconDataMembers"/>
  10. /// that were defined.
  11. /// </summary>
  12. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  13. public struct NotifyIconData
  14. {
  15. /// <summary>
  16. /// Size of this structure, in bytes.
  17. /// </summary>
  18. public uint cbSize;
  19. /// <summary>
  20. /// Handle to the window that receives notification messages associated with an icon in the
  21. /// taskbar status area. The Shell uses hWnd and uID to identify which icon to operate on
  22. /// when Shell_NotifyIcon is invoked.
  23. /// </summary>
  24. public IntPtr WindowHandle;
  25. /// <summary>
  26. /// Application-defined identifier of the taskbar icon. The Shell uses hWnd and uID to identify
  27. /// which icon to operate on when Shell_NotifyIcon is invoked. You can have multiple icons
  28. /// associated with a single hWnd by assigning each a different uID. This feature, however
  29. /// is currently not used.
  30. /// </summary>
  31. public uint TaskbarIconId;
  32. /// <summary>
  33. /// Flags that indicate which of the other members contain valid data. This member can be
  34. /// a combination of the NIF_XXX constants.
  35. /// </summary>
  36. public IconDataMembers ValidMembers;
  37. /// <summary>
  38. /// Application-defined message identifier. The system uses this identifier to send
  39. /// notifications to the window identified in hWnd.
  40. /// </summary>
  41. public uint CallbackMessageId;
  42. /// <summary>
  43. /// A handle to the icon that should be displayed. Just
  44. /// <c>Icon.Handle</c>.
  45. /// </summary>
  46. public IntPtr IconHandle;
  47. /// <summary>
  48. /// String with the text for a standard ToolTip. It can have a maximum of 64 characters including
  49. /// the terminating NULL. For Version 5.0 and later, szTip can have a maximum of
  50. /// 128 characters, including the terminating NULL.
  51. /// </summary>
  52. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  53. public string ToolTipText;
  54. /// <summary>
  55. /// State of the icon. Remember to also set the <see cref="StateMask"/>.
  56. /// </summary>
  57. public IconState IconState;
  58. /// <summary>
  59. /// A value that specifies which bits of the state member are retrieved or modified.
  60. /// For example, setting this member to <see cref="TaskbarNotification.Interop.IconState.Hidden"/>
  61. /// causes only the item's hidden
  62. /// state to be retrieved.
  63. /// </summary>
  64. public IconState StateMask;
  65. /// <summary>
  66. /// String with the text for a balloon ToolTip. It can have a maximum of 255 characters.
  67. /// To remove the ToolTip, set the NIF_INFO flag in uFlags and set szInfo to an empty string.
  68. /// </summary>
  69. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  70. public string BalloonText;
  71. /// <summary>
  72. /// Mainly used to set the version when <see cref="WinApi.Shell_NotifyIcon"/> is invoked
  73. /// with <see cref="NotifyCommand.SetVersion"/>. However, for legacy operations,
  74. /// the same member is also used to set timeouts for balloon ToolTips.
  75. /// </summary>
  76. public uint VersionOrTimeout;
  77. /// <summary>
  78. /// String containing a title for a balloon ToolTip. This title appears in boldface
  79. /// above the text. It can have a maximum of 63 characters.
  80. /// </summary>
  81. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
  82. public string BalloonTitle;
  83. /// <summary>
  84. /// Adds an icon to a balloon ToolTip, which is placed to the left of the title. If the
  85. /// <see cref="BalloonTitle"/> member is zero-length, the icon is not shown.
  86. /// </summary>
  87. public BalloonFlags BalloonFlags;
  88. /// <summary>
  89. /// Windows XP (Shell32.dll version 6.0) and later.<br/>
  90. /// - Windows 7 and later: A registered GUID that identifies the icon.
  91. /// This value overrides uID and is the recommended method of identifying the icon.<br/>
  92. /// - Windows XP through Windows Vista: Reserved.
  93. /// </summary>
  94. public Guid TaskbarIconGuid;
  95. /// <summary>
  96. /// Windows Vista (Shell32.dll version 6.0.6) and later. The handle of a customized
  97. /// balloon icon provided by the application that should be used independently
  98. /// of the tray icon. If this member is non-NULL and the <see cref="TaskbarNotification.Interop.BalloonFlags.User"/>
  99. /// flag is set, this icon is used as the balloon icon.<br/>
  100. /// If this member is NULL, the legacy behavior is carried out.
  101. /// </summary>
  102. public IntPtr CustomBalloonIconHandle;
  103. /// <summary>
  104. /// Creates a default data structure that provides
  105. /// a hidden taskbar icon without the icon being set.
  106. /// </summary>
  107. /// <param name="handle"></param>
  108. /// <returns>NotifyIconData</returns>
  109. public static NotifyIconData CreateDefault(IntPtr handle)
  110. {
  111. var data = new NotifyIconData();
  112. if (Environment.OSVersion.Version.Major >= 6)
  113. {
  114. //use the current size
  115. data.cbSize = (uint) Marshal.SizeOf(data);
  116. }
  117. else
  118. {
  119. //we need to set another size on xp/2003- otherwise certain
  120. //features (e.g. balloon tooltips) don't work.
  121. data.cbSize = 952; // NOTIFYICONDATAW_V3_SIZE
  122. //set to fixed timeout
  123. data.VersionOrTimeout = 10;
  124. }
  125. data.WindowHandle = handle;
  126. data.TaskbarIconId = 0x0;
  127. data.CallbackMessageId = WindowMessageSink.CallbackMessageId;
  128. data.VersionOrTimeout = (uint) NotifyIconVersion.Win95;
  129. data.IconHandle = IntPtr.Zero;
  130. //hide initially
  131. data.IconState = IconState.Hidden;
  132. data.StateMask = IconState.Hidden;
  133. //set flags
  134. data.ValidMembers = IconDataMembers.Message
  135. | IconDataMembers.Icon
  136. | IconDataMembers.Tip;
  137. //reset strings
  138. data.ToolTipText = data.BalloonText = data.BalloonTitle = string.Empty;
  139. return data;
  140. }
  141. }
  142. }