mirror of
https://github.com/ckaczor/wpf-notifyicon.git
synced 2026-01-20 09:45:39 -05:00
WPF NotifyIcon
-------------- Initial import git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@52 9f600761-6f11-4665-b6dc-0185e9171623
This commit is contained in:
55
Source/NotifyIconWpf/Interop/BalloonFlags.cs
Normal file
55
Source/NotifyIconWpf/Interop/BalloonFlags.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// Flags that define the icon that is shown on a balloon
|
||||
/// tooltip.
|
||||
/// </summary>
|
||||
public enum BalloonFlags
|
||||
{
|
||||
/// <summary>
|
||||
/// No icon is displayed.
|
||||
/// </summary>
|
||||
None = 0x00,
|
||||
/// <summary>
|
||||
/// An information icon is displayed.
|
||||
/// </summary>
|
||||
Info = 0x01,
|
||||
/// <summary>
|
||||
/// A warning icon is displayed.
|
||||
/// </summary>
|
||||
Warning = 0x02,
|
||||
/// <summary>
|
||||
/// An error icon is displayed.
|
||||
/// </summary>
|
||||
Error = 0x03,
|
||||
/// <summary>
|
||||
/// Windows XP Service Pack 2 (SP2) and later.
|
||||
/// Use a custom icon as the title icon.
|
||||
/// </summary>
|
||||
User = 0x04,
|
||||
/// <summary>
|
||||
/// Windows XP (Shell32.dll version 6.0) and later.
|
||||
/// Do not play the associated sound. Applies only to balloon ToolTips.
|
||||
/// </summary>
|
||||
NoSound = 0x10,
|
||||
/// <summary>
|
||||
/// Windows Vista (Shell32.dll version 6.0.6) and later. The large version
|
||||
/// of the icon should be used as the balloon icon. This corresponds to the
|
||||
/// icon with dimensions SM_CXICON x SM_CYICON. If this flag is not set,
|
||||
/// the icon with dimensions XM_CXSMICON x SM_CYSMICON is used.<br/>
|
||||
/// - This flag can be used with all stock icons.<br/>
|
||||
/// - Applications that use older customized icons (NIIF_USER with hIcon) must
|
||||
/// provide a new SM_CXICON x SM_CYICON version in the tray icon (hIcon). These
|
||||
/// icons are scaled down when they are displayed in the System Tray or
|
||||
/// System Control Area (SCA).<br/>
|
||||
/// - New customized icons (NIIF_USER with hBalloonIcon) must supply an
|
||||
/// SM_CXICON x SM_CYICON version in the supplied icon (hBalloonIcon).
|
||||
/// </summary>
|
||||
LargeIcon = 0x20,
|
||||
/// <summary>
|
||||
/// Windows 7 and later.
|
||||
/// </summary>
|
||||
RespectQuietTime = 0x80
|
||||
|
||||
}
|
||||
}
|
||||
67
Source/NotifyIconWpf/Interop/IconDataMembers.cs
Normal file
67
Source/NotifyIconWpf/Interop/IconDataMembers.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates which members of a <see cref="NotifyIconData"/> structure
|
||||
/// were set, and thus contain valid data or provide additional information
|
||||
/// to the ToolTip as to how it should display.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum IconDataMembers
|
||||
{
|
||||
/// <summary>
|
||||
/// The message ID is set.
|
||||
/// </summary>
|
||||
Message = 0x01,
|
||||
/// <summary>
|
||||
/// The notification icon is set.
|
||||
/// </summary>
|
||||
Icon = 0x02,
|
||||
/// <summary>
|
||||
/// The tooltip is set.
|
||||
/// </summary>
|
||||
Tip = 0x04,
|
||||
/// <summary>
|
||||
/// State information (<see cref="IconState"/>) is set. This
|
||||
/// applies to both <see cref="NotifyIconData.IconState"/> and
|
||||
/// <see cref="NotifyIconData.StateMask"/>.
|
||||
/// </summary>
|
||||
State = 0x08,
|
||||
/// <summary>
|
||||
/// The ballon ToolTip is set. Accordingly, the following
|
||||
/// members are set: <see cref="NotifyIconData.BalloonText"/>,
|
||||
/// <see cref="NotifyIconData.BalloonTitle"/>, <see cref="NotifyIconData.BalloonFlags"/>,
|
||||
/// and <see cref="NotifyIconData.VersionOrTimeout"/>.
|
||||
/// </summary>
|
||||
Info = 0x10,
|
||||
|
||||
/// <summary>
|
||||
/// Internal identifier is set. Reserved, thus commented out.
|
||||
/// </summary>
|
||||
//Guid = 0x20,
|
||||
|
||||
/// <summary>
|
||||
/// Windows Vista (Shell32.dll version 6.0.6) and later. If the ToolTip
|
||||
/// cannot be displayed immediately, discard it.<br/>
|
||||
/// Use this flag for ToolTips that represent real-time information which
|
||||
/// would be meaningless or misleading if displayed at a later time.
|
||||
/// For example, a message that states "Your telephone is ringing."<br/>
|
||||
/// This modifies and must be combined with the <see cref="Info"/> flag.
|
||||
/// </summary>
|
||||
Realtime = 0x40,
|
||||
/// <summary>
|
||||
/// Windows Vista (Shell32.dll version 6.0.6) and later.
|
||||
/// Use the standard ToolTip. Normally, when uVersion is set
|
||||
/// to NOTIFYICON_VERSION_4, the standard ToolTip is replaced
|
||||
/// by the application-drawn pop-up user interface (UI).
|
||||
/// If the application wants to show the standard tooltip
|
||||
/// in that case, regardless of whether the on-hover UI is showing,
|
||||
/// it can specify NIF_SHOWTIP to indicate the standard tooltip
|
||||
/// should still be shown.<br/>
|
||||
/// Note that the NIF_SHOWTIP flag is effective until the next call
|
||||
/// to Shell_NotifyIcon.
|
||||
/// </summary>
|
||||
UseLegacyToolTips = 0x80
|
||||
}
|
||||
}
|
||||
23
Source/NotifyIconWpf/Interop/IconState.cs
Normal file
23
Source/NotifyIconWpf/Interop/IconState.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// The state of the icon - can be set to
|
||||
/// hide the icon.
|
||||
/// </summary>
|
||||
public enum IconState
|
||||
{
|
||||
/// <summary>
|
||||
/// The icon is visible.
|
||||
/// </summary>
|
||||
Visible = 0x00,
|
||||
/// <summary>
|
||||
/// Hide the icon.
|
||||
/// </summary>
|
||||
Hidden = 0x01,
|
||||
|
||||
/// <summary>
|
||||
/// The icon is shared - currently not supported, thus commented out.
|
||||
/// </summary>
|
||||
//Shared = 0x02
|
||||
}
|
||||
}
|
||||
46
Source/NotifyIconWpf/Interop/MouseEvent.cs
Normal file
46
Source/NotifyIconWpf/Interop/MouseEvent.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// Event flags for clicked events.
|
||||
/// </summary>
|
||||
public enum MouseEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// The mouse was moved withing the
|
||||
/// taskbar icon's area.
|
||||
/// </summary>
|
||||
MouseMove,
|
||||
/// <summary>
|
||||
/// The right mouse button was clicked.
|
||||
/// </summary>
|
||||
IconRightMouseDown,
|
||||
/// <summary>
|
||||
/// The left mouse button was clicked.
|
||||
/// </summary>
|
||||
IconLeftMouseDown,
|
||||
/// <summary>
|
||||
/// The right mouse button was released.
|
||||
/// </summary>
|
||||
IconRightMouseUp,
|
||||
/// <summary>
|
||||
/// The left mouse button was released.
|
||||
/// </summary>
|
||||
IconLeftMouseUp,
|
||||
/// <summary>
|
||||
/// The middle mouse button was clicked.
|
||||
/// </summary>
|
||||
IconMiddleMouseDown,
|
||||
/// <summary>
|
||||
/// The middle mouse button was released.
|
||||
/// </summary>
|
||||
IconMiddleMouseUp,
|
||||
/// <summary>
|
||||
/// The taskbar icon was double clicked.
|
||||
/// </summary>
|
||||
IconDoubleClick,
|
||||
/// <summary>
|
||||
/// The balloon tip was clicked.
|
||||
/// </summary>
|
||||
BalloonToolTipClicked
|
||||
}
|
||||
}
|
||||
39
Source/NotifyIconWpf/Interop/NotifyCommand.cs
Normal file
39
Source/NotifyIconWpf/Interop/NotifyCommand.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// Main operations performed on the
|
||||
/// <see cref="WinApi.Shell_NotifyIcon"/> function.
|
||||
/// </summary>
|
||||
public enum NotifyCommand
|
||||
{
|
||||
/// <summary>
|
||||
/// The taskbar icon is being created.
|
||||
/// </summary>
|
||||
Add = 0x00,
|
||||
/// <summary>
|
||||
/// The settings of the taskbar icon are being updated.
|
||||
/// </summary>
|
||||
Modify = 0x01,
|
||||
/// <summary>
|
||||
/// The taskbar icon is deleted.
|
||||
/// </summary>
|
||||
Delete = 0x02,
|
||||
/// <summary>
|
||||
/// Focus is returned to the taskbar icon. Currently not in use.
|
||||
/// </summary>
|
||||
SetFocus = 0x03,
|
||||
/// <summary>
|
||||
/// Shell32.dll version 5.0 and later only. Instructs the taskbar
|
||||
/// to behave according to the version number specified in the
|
||||
/// uVersion member of the structure pointed to by lpdata.
|
||||
/// This message allows you to specify whether you want the version
|
||||
/// 5.0 behavior found on Microsoft Windows 2000 systems, or the
|
||||
/// behavior found on earlier Shell versions. The default value for
|
||||
/// uVersion is zero, indicating that the original Windows 95 notify
|
||||
/// icon behavior should be used.
|
||||
/// </summary>
|
||||
SetVersion = 0x04
|
||||
}
|
||||
}
|
||||
153
Source/NotifyIconWpf/Interop/NotifyIconData.cs
Normal file
153
Source/NotifyIconWpf/Interop/NotifyIconData.cs
Normal file
@@ -0,0 +1,153 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// A struct that is submitted in order to configure
|
||||
/// the taskbar icon. Provides various members that
|
||||
/// can be configured partially, according to the
|
||||
/// values of the <see cref="IconDataMembers"/>
|
||||
/// that were defined.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct NotifyIconData
|
||||
{
|
||||
/// <summary>
|
||||
/// Size of this structure, in bytes.
|
||||
/// </summary>
|
||||
public uint cbSize;
|
||||
|
||||
/// <summary>
|
||||
/// Handle to the window that receives notification messages associated with an icon in the
|
||||
/// taskbar status area. The Shell uses hWnd and uID to identify which icon to operate on
|
||||
/// when Shell_NotifyIcon is invoked.
|
||||
/// </summary>
|
||||
public IntPtr WindowHandle;
|
||||
|
||||
/// <summary>
|
||||
/// Application-defined identifier of the taskbar icon. The Shell uses hWnd and uID to identify
|
||||
/// which icon to operate on when Shell_NotifyIcon is invoked. You can have multiple icons
|
||||
/// associated with a single hWnd by assigning each a different uID. This feature, however
|
||||
/// is currently not used.
|
||||
/// </summary>
|
||||
public uint TaskbarIconId;
|
||||
|
||||
/// <summary>
|
||||
/// Flags that indicate which of the other members contain valid data. This member can be
|
||||
/// a combination of the NIF_XXX constants.
|
||||
/// </summary>
|
||||
public IconDataMembers ValidMembers;
|
||||
|
||||
/// <summary>
|
||||
/// Application-defined message identifier. The system uses this identifier to send
|
||||
/// notifications to the window identified in hWnd.
|
||||
/// </summary>
|
||||
public uint CallbackMessageId;
|
||||
|
||||
/// <summary>
|
||||
/// A handle to the icon that should be displayed. Just
|
||||
/// <see cref="Icon.Handle"/>.
|
||||
/// </summary>
|
||||
public IntPtr IconHandle;
|
||||
|
||||
/// <summary>
|
||||
/// String with the text for a standard ToolTip. It can have a maximum of 64 characters including
|
||||
/// the terminating NULL. For Version 5.0 and later, szTip can have a maximum of
|
||||
/// 128 characters, including the terminating NULL.
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string ToolTipText;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// State of the icon. Remember to also set the <see cref="StateMask"/>.
|
||||
/// </summary>
|
||||
public IconState IconState;
|
||||
|
||||
/// <summary>
|
||||
/// A value that specifies which bits of the state member are retrieved or modified.
|
||||
/// For example, setting this member to <see cref="Interop.IconState.Hidden"/>
|
||||
/// causes only the item's hidden
|
||||
/// state to be retrieved.
|
||||
/// </summary>
|
||||
public IconState StateMask;
|
||||
|
||||
/// <summary>
|
||||
/// String with the text for a balloon ToolTip. It can have a maximum of 255 characters.
|
||||
/// To remove the ToolTip, set the NIF_INFO flag in uFlags and set szInfo to an empty string.
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string BalloonText;
|
||||
|
||||
/// <summary>
|
||||
/// Mainly used to set the version when <see cref="WinApi.Shell_NotifyIcon"/> is invoked
|
||||
/// with <see cref="NotifyCommand.SetVersion"/>. However, for legacy operations,
|
||||
/// the same member is also used to set timouts for balloon ToolTips.
|
||||
/// </summary>
|
||||
public uint VersionOrTimeout;
|
||||
|
||||
/// <summary>
|
||||
/// String containing a title for a balloon ToolTip. This title appears in boldface
|
||||
/// above the text. It can have a maximum of 63 characters.
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] public string BalloonTitle;
|
||||
|
||||
/// <summary>
|
||||
/// Adds an icon to a balloon ToolTip, which is placed to the left of the title. If the
|
||||
/// <see cref="BalloonTitle"/> member is zero-length, the icon is not shown.
|
||||
/// </summary>
|
||||
public BalloonFlags BalloonFlags;
|
||||
|
||||
/// <summary>
|
||||
/// Windows XP (Shell32.dll version 6.0) and later.<br/>
|
||||
/// - Windows 7 and later: A registered GUID that identifies the icon.
|
||||
/// This value overrides uID and is the recommended method of identifying the icon.<br/>
|
||||
/// - Windows XP through Windows Vista: Reserved.
|
||||
/// </summary>
|
||||
public Guid TaskbarIconGuid;
|
||||
|
||||
/// <summary>
|
||||
/// Windows Vista (Shell32.dll version 6.0.6) and later. The handle of a customized
|
||||
/// balloon icon provided by the application that should be used independently
|
||||
/// of the tray icon. If this member is non-NULL and the <see cref="Interop.BalloonFlags.User"/>
|
||||
/// flag is set, this icon is used as the balloon icon.<br/>
|
||||
/// If this member is NULL, the legacy behavior is carried out.
|
||||
/// </summary>
|
||||
public IntPtr CustomBalloonIconHandle;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a default data structure that provides
|
||||
/// a hidden taskbar icon without the icon being set.
|
||||
/// </summary>
|
||||
/// <param name="handle"></param>
|
||||
/// <returns></returns>
|
||||
public static NotifyIconData CreateDefault(IntPtr handle)
|
||||
{
|
||||
var data = new NotifyIconData();
|
||||
|
||||
data.cbSize = (uint) Marshal.SizeOf(data);
|
||||
|
||||
data.WindowHandle = handle;
|
||||
data.TaskbarIconId = 0x0;
|
||||
data.CallbackMessageId = WindowMessageSink.CallbackMessageId;
|
||||
data.VersionOrTimeout = (uint) NotifyIconVersion.Win95;
|
||||
|
||||
data.IconHandle = IntPtr.Zero;
|
||||
|
||||
//hide initially
|
||||
data.IconState = IconState.Hidden;
|
||||
data.StateMask = IconState.Hidden;
|
||||
|
||||
//set flags
|
||||
data.ValidMembers = IconDataMembers.Message
|
||||
| IconDataMembers.Icon
|
||||
| IconDataMembers.Tip;
|
||||
|
||||
//reset strings
|
||||
data.ToolTipText = data.BalloonText = data.BalloonTitle = String.Empty;
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Source/NotifyIconWpf/Interop/NotifyIconVersion.cs
Normal file
28
Source/NotifyIconWpf/Interop/NotifyIconVersion.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// The notify icon version that is used. The higher
|
||||
/// the version, the more capabilities are available.
|
||||
/// </summary>
|
||||
public enum NotifyIconVersion
|
||||
{
|
||||
/// <summary>
|
||||
/// Default behavior (legacy Win95).
|
||||
/// </summary>
|
||||
Win95 = 0x0,
|
||||
/// <summary>
|
||||
/// Behavior representing Win2000 an higher.
|
||||
/// </summary>
|
||||
Win2000 = 0x3,
|
||||
/// <summary>
|
||||
/// Extended tooltip support, which is available
|
||||
/// for Vista and later.
|
||||
/// </summary>
|
||||
Vista = 0x4
|
||||
}
|
||||
}
|
||||
95
Source/NotifyIconWpf/Interop/TrayLocator.cs
Normal file
95
Source/NotifyIconWpf/Interop/TrayLocator.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
public struct Rect
|
||||
{
|
||||
public int left;
|
||||
public int top;
|
||||
public int right;
|
||||
public int bottom;
|
||||
public override string ToString()
|
||||
{
|
||||
return "(" + left + ", " + top + ") --> (" + right + ", " + bottom + ")";
|
||||
}
|
||||
}
|
||||
|
||||
public struct TaskbarInfo
|
||||
{
|
||||
public int cbSize;
|
||||
public IntPtr WindowHandle;
|
||||
public int uCallbackMessage;
|
||||
public TaskbarPosition Position;
|
||||
public Rect Rectangle;
|
||||
public IntPtr lParam;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public enum TaskbarPosition
|
||||
{
|
||||
Left = 0,
|
||||
Top,
|
||||
Right,
|
||||
Bottom
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Locates the position of the tray area.
|
||||
/// </summary>
|
||||
public class TrayLocator
|
||||
{
|
||||
public enum ABMsg
|
||||
{
|
||||
ABM_NEW = 0,
|
||||
ABM_REMOVE = 1,
|
||||
ABM_QUERYPOS = 2,
|
||||
ABM_SETPOS = 3,
|
||||
ABM_GETSTATE = 4,
|
||||
ABM_GETTASKBARPOS = 5,
|
||||
ABM_ACTIVATE = 6,
|
||||
ABM_GETAUTOHIDEBAR = 7,
|
||||
ABM_SETAUTOHIDEBAR = 8,
|
||||
ABM_WINDOWPOSCHANGED = 9,
|
||||
ABM_SETSTATE = 10
|
||||
}
|
||||
|
||||
public enum ABNotify
|
||||
{
|
||||
ABN_STATECHANGE = 0,
|
||||
ABN_POSCHANGED,
|
||||
ABN_FULLSCREENAPP,
|
||||
ABN_WINDOWARRANGE
|
||||
}
|
||||
|
||||
[DllImport("shell32.dll", EntryPoint = "SHAppBarMessage", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
|
||||
public static extern int SHAppBarMessage(int dwMessage, ref TaskbarInfo pData);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Determines the current location of the taskbar.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static TaskbarInfo GetTaskbarInformation()
|
||||
{
|
||||
TaskbarInfo tbInfo = new TaskbarInfo();
|
||||
tbInfo.cbSize = Marshal.SizeOf(tbInfo);
|
||||
|
||||
//retrieve the bounding rectangle of the Windows taskbar.
|
||||
SHAppBarMessage((int)ABMsg.ABM_GETTASKBARPOS, ref tbInfo);
|
||||
|
||||
return tbInfo;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
80
Source/NotifyIconWpf/Interop/WinApi.cs
Normal file
80
Source/NotifyIconWpf/Interop/WinApi.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// Win32 API imports.
|
||||
/// </summary>
|
||||
internal static class WinApi
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates, updates or deletes the taskbar icon.
|
||||
/// </summary>
|
||||
[DllImport("shell32.Dll")]
|
||||
public static extern bool Shell_NotifyIcon(NotifyCommand cmd, [In]ref NotifyIconData data);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates the helper window that receives messages from the taskar icon.
|
||||
/// </summary>
|
||||
[DllImport("USER32.DLL", EntryPoint = "CreateWindowExW", SetLastError = true)]
|
||||
public static extern IntPtr CreateWindowEx(int dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName,
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, int dwStyle, int x, int y,
|
||||
int nWidth, int nHeight, uint hWndParent, int hMenu, int hInstance,
|
||||
int lpParam);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Processes a default windows procedure.
|
||||
/// </summary>
|
||||
[DllImport("USER32.DLL")]
|
||||
public static extern long DefWindowProc(IntPtr hWnd, uint msg, uint wparam, uint lparam);
|
||||
|
||||
/// <summary>
|
||||
/// Registers the helper window class.
|
||||
/// </summary>
|
||||
[DllImport("USER32.DLL", EntryPoint = "RegisterClassW", SetLastError = true)]
|
||||
public static extern short RegisterClass(ref WindowClass lpWndClass);
|
||||
|
||||
/// <summary>
|
||||
/// Registers a listener for a window message.
|
||||
/// </summary>
|
||||
/// <param name="lpString"></param>
|
||||
/// <returns></returns>
|
||||
[DllImport("User32.Dll", EntryPoint = "RegisterWindowMessageW")]
|
||||
public static extern uint RegisterWindowMessage([MarshalAs(UnmanagedType.LPWStr)] string lpString);
|
||||
|
||||
/// <summary>
|
||||
/// Used to destroy the hidden helper window that receives messages from the
|
||||
/// taskbar icon.
|
||||
/// </summary>
|
||||
/// <param name="hWnd"></param>
|
||||
/// <returns></returns>
|
||||
[DllImport("USER32.DLL", SetLastError = true)]
|
||||
public static extern bool DestroyWindow(IntPtr hWnd);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gives focus to a given window.
|
||||
/// </summary>
|
||||
/// <param name="hWnd"></param>
|
||||
/// <returns></returns>
|
||||
[DllImport("USER32.DLL")]
|
||||
public static extern bool SetForegroundWindow(IntPtr hWnd);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the maximum number of milliseconds that can elapse between a
|
||||
/// first click and a second click for the OS to consider the
|
||||
/// mouse action a double-click.
|
||||
/// </summary>
|
||||
/// <returns>The maximum amount of time, in milliseconds, that can
|
||||
/// elapse between a first click and a second click for the OS to
|
||||
/// consider the mouse action a double-click.</returns>
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
|
||||
public static extern int GetDoubleClickTime();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
31
Source/NotifyIconWpf/Interop/WindowClass.cs
Normal file
31
Source/NotifyIconWpf/Interop/WindowClass.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// Callback delegate which is used by the Windows API to
|
||||
/// submit window messages.
|
||||
/// </summary>
|
||||
public delegate long WindowProcedureHandler(IntPtr hwnd, uint uMsg, uint wparam, uint lparam);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Win API WNDCLASS struct - represents a single window.
|
||||
/// Used to receive window messages.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct WindowClass
|
||||
{
|
||||
public uint style;
|
||||
public WindowProcedureHandler lpfnWndProc;
|
||||
public int cbClsExtra;
|
||||
public int cbWndExtra;
|
||||
public IntPtr hInstance;
|
||||
public IntPtr hIcon;
|
||||
public IntPtr hCursor;
|
||||
public IntPtr hbrBackground;
|
||||
[MarshalAs(UnmanagedType.LPWStr)] public string lpszMenuName;
|
||||
[MarshalAs(UnmanagedType.LPWStr)] public string lpszClassName;
|
||||
}
|
||||
}
|
||||
102
Source/NotifyIconWpf/Interop/WindowMessageSink.Handle.cs
Normal file
102
Source/NotifyIconWpf/Interop/WindowMessageSink.Handle.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides low level code that is used to receive
|
||||
/// window messages without having a window that
|
||||
/// prevents a WPF application from shutting down
|
||||
/// properly.
|
||||
/// </summary>
|
||||
public partial class WindowMessageSink
|
||||
{
|
||||
/// <summary>
|
||||
/// Window class ID.
|
||||
/// </summary>
|
||||
private string WindowId;
|
||||
|
||||
/// <summary>
|
||||
/// Handle for the message window.
|
||||
/// </summary
|
||||
internal IntPtr MessageWindowHandle { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The ID of the message that is being received if the
|
||||
/// taskbar is (re)started.
|
||||
/// </summary>
|
||||
private uint taskbarRestartMessageId;
|
||||
|
||||
/// <summary>
|
||||
/// A delegate that processes messages of the hidden
|
||||
/// native window that receives window messages. Storing
|
||||
/// this reference makes sure we don't loose our reference
|
||||
/// to the message window.
|
||||
/// </summary>
|
||||
private WindowProcedureHandler messageHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Creates the helper message window that is used
|
||||
/// to receive messages from the taskbar icon.
|
||||
/// </summary>
|
||||
private void CreateMessageWindow()
|
||||
{
|
||||
WindowId = "WPFTaskbarIcon_" + Guid.NewGuid().ToString();
|
||||
|
||||
//register window message handler
|
||||
messageHandler = OnWindowMessageReceived;
|
||||
|
||||
// Create a simple window class which is reference through
|
||||
//the messageHandler delegate
|
||||
WindowClass wc;
|
||||
|
||||
wc.style = 0;
|
||||
wc.lpfnWndProc = messageHandler;
|
||||
wc.cbClsExtra = 0;
|
||||
wc.cbWndExtra = 0;
|
||||
wc.hInstance = IntPtr.Zero;
|
||||
wc.hIcon = IntPtr.Zero;
|
||||
wc.hCursor = IntPtr.Zero;
|
||||
wc.hbrBackground = IntPtr.Zero;
|
||||
wc.lpszMenuName = "";
|
||||
wc.lpszClassName = WindowId;
|
||||
|
||||
// Register the window class
|
||||
WinApi.RegisterClass(ref wc);
|
||||
|
||||
// Get the message used to indicate the taskbar has been restarted
|
||||
// This is used to re-add icons when the taskbar restarts
|
||||
taskbarRestartMessageId = WinApi.RegisterWindowMessage("TaskbarCreated");
|
||||
|
||||
// Create the message window
|
||||
MessageWindowHandle = WinApi.CreateWindowEx(0, WindowId, "", 0, 0, 0, 1, 1, 0, 0, 0, 0);
|
||||
|
||||
if (MessageWindowHandle == IntPtr.Zero)
|
||||
{
|
||||
throw new Win32Exception();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Callback method that receives messages from the taskbar area.
|
||||
/// </summary>
|
||||
private long OnWindowMessageReceived(IntPtr hwnd, uint messageId, uint wparam, uint lparam)
|
||||
{
|
||||
if (messageId == taskbarRestartMessageId)
|
||||
{
|
||||
//recreate the icon if the taskbar was restarted
|
||||
//TODO refresh icon
|
||||
}
|
||||
|
||||
ProcessWindowMessage(messageId, wparam, lparam);
|
||||
|
||||
//handle mouse clicks...
|
||||
|
||||
// Pass the message to the default window procedure
|
||||
return WinApi.DefWindowProc(hwnd, messageId, wparam, lparam);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
255
Source/NotifyIconWpf/Interop/WindowMessageSink.cs
Normal file
255
Source/NotifyIconWpf/Interop/WindowMessageSink.cs
Normal file
@@ -0,0 +1,255 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||
{
|
||||
/// <summary>
|
||||
/// Receives messages from the taskbar icon through
|
||||
/// window messages of an underlying helper window.
|
||||
/// </summary>
|
||||
public partial class WindowMessageSink : IDisposable
|
||||
{
|
||||
|
||||
#region members
|
||||
|
||||
/// <summary>
|
||||
/// The ID of messages that are received from the the
|
||||
/// taskbar icon.
|
||||
/// </summary>
|
||||
public const int CallbackMessageId = 0x400;
|
||||
|
||||
/// <summary>
|
||||
/// The version of the underlying icon. Defines how
|
||||
/// incoming messages are interpreted.
|
||||
/// </summary>
|
||||
public NotifyIconVersion Version { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region events
|
||||
|
||||
/// <summary>
|
||||
/// The custom tooltip should be closed or hidden.
|
||||
/// </summary>
|
||||
public event Action<bool> ChangeToolTipStateRequest;
|
||||
|
||||
/// <summary>
|
||||
/// Fired in case the user clicked or moved within
|
||||
/// the taskbar icon area.
|
||||
/// </summary>
|
||||
public event Action<MouseEvent> MouseEventReceived;
|
||||
|
||||
/// <summary>
|
||||
/// Fired if a balloon ToolTip was either displayed
|
||||
/// or closed (indicated by the boolean flag).
|
||||
/// </summary>
|
||||
public event Action<bool> BallonToolTipChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Fired if the taskbar was created. Requires the taskbar
|
||||
/// icon to be reset.
|
||||
/// </summary>
|
||||
public event Action TaskbarCreated;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region construction
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new message sink that receives message from
|
||||
/// a given taskbar icon.
|
||||
/// </summary>
|
||||
/// <param name="version"></param>
|
||||
public WindowMessageSink(NotifyIconVersion version)
|
||||
{
|
||||
Version = version;
|
||||
CreateMessageWindow();
|
||||
}
|
||||
|
||||
|
||||
private WindowMessageSink()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a dummy instance that provides an empty
|
||||
/// pointer rather than a real window handler.<br/>
|
||||
/// Used at design time.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
internal static WindowMessageSink CreateEmpty()
|
||||
{
|
||||
return new WindowMessageSink
|
||||
{
|
||||
MessageWindowHandle = IntPtr.Zero,
|
||||
Version = NotifyIconVersion.Vista
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Process Window Messages
|
||||
|
||||
/// <summary>
|
||||
/// Processes incoming system messages.
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
/// <param name="wParam"></param>
|
||||
/// <param name="lParam"></param>
|
||||
/// <returns></returns>
|
||||
private void ProcessWindowMessage(uint msg, uint wParam, uint lParam)
|
||||
{
|
||||
if (msg != CallbackMessageId) return;
|
||||
|
||||
switch (lParam)
|
||||
{
|
||||
case 0x200:
|
||||
// Debug.WriteLine("MOVE");
|
||||
MouseEventReceived(MouseEvent.MouseMove);
|
||||
break;
|
||||
|
||||
case 0x201:
|
||||
Debug.WriteLine("left down 1");
|
||||
MouseEventReceived(MouseEvent.IconLeftMouseDown);
|
||||
break;
|
||||
|
||||
case 0x202:
|
||||
Debug.WriteLine("left up");
|
||||
MouseEventReceived(MouseEvent.IconLeftMouseUp);
|
||||
break;
|
||||
|
||||
case 0x203:
|
||||
Debug.WriteLine("left click 2");
|
||||
MouseEventReceived(MouseEvent.IconDoubleClick);
|
||||
break;
|
||||
|
||||
case 0x204:
|
||||
Debug.WriteLine("right click 1");
|
||||
MouseEventReceived(MouseEvent.IconRightMouseDown);
|
||||
break;
|
||||
|
||||
case 0x205:
|
||||
Console.Out.WriteLine("right mouse up");
|
||||
MouseEventReceived(MouseEvent.IconRightMouseUp);
|
||||
break;
|
||||
|
||||
case 0x206:
|
||||
//double click with right mouse button - do not trigger event
|
||||
Debug.WriteLine("right click 2");
|
||||
break;
|
||||
|
||||
case 0x207:
|
||||
Debug.WriteLine("middle click 1");
|
||||
MouseEventReceived(MouseEvent.IconMiddleMouseDown);
|
||||
break;
|
||||
|
||||
case 520:
|
||||
Debug.WriteLine("mouse up middle");
|
||||
MouseEventReceived(MouseEvent.IconMiddleMouseUp);
|
||||
break;
|
||||
|
||||
case 0x209:
|
||||
//double click with middle mouse button - do not trigger event
|
||||
Debug.WriteLine("middle click 2");
|
||||
break;
|
||||
|
||||
case 0x402:
|
||||
Debug.WriteLine("balloon shown");
|
||||
BallonToolTipChanged(true);
|
||||
break;
|
||||
|
||||
case 0x403:
|
||||
case 0x404:
|
||||
Debug.WriteLine("balloon close");
|
||||
BallonToolTipChanged(false);
|
||||
break;
|
||||
|
||||
case 0x405:
|
||||
Debug.WriteLine("balloon clicked");
|
||||
MouseEventReceived(MouseEvent.BalloonToolTipClicked);
|
||||
break;
|
||||
|
||||
case 0x406:
|
||||
Debug.WriteLine("show custom tooltip");
|
||||
ChangeToolTipStateRequest(true);
|
||||
break;
|
||||
|
||||
case 0x407:
|
||||
Debug.WriteLine("close custom tooltip");
|
||||
ChangeToolTipStateRequest(false);
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.WriteLine("Unhandled message ID: " + lParam);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region Dispose
|
||||
|
||||
/// <summary>
|
||||
/// Set to true as soon as <see cref="Dispose"/>
|
||||
/// has been invoked.
|
||||
/// </summary>
|
||||
public bool IsDisposed { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the object.
|
||||
/// </summary>
|
||||
/// <remarks>This method is not virtual by design. Derived classes
|
||||
/// should override <see cref="Dispose(bool)"/>.
|
||||
/// </remarks>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
|
||||
// This object will be cleaned up by the Dispose method.
|
||||
// Therefore, you should call GC.SupressFinalize to
|
||||
// take this object off the finalization queue
|
||||
// and prevent finalization code for this object
|
||||
// from executing a second time.
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This destructor will run only if the <see cref="Dispose()"/>
|
||||
/// method does not get called. This gives this base class the
|
||||
/// opportunity to finalize.
|
||||
/// <para>
|
||||
/// Important: Do not provide destructors in types derived from
|
||||
/// this class.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
~WindowMessageSink()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Removes the windows hook that receives window
|
||||
/// messages and closes the underlying helper window.
|
||||
/// </summary>
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
//don't do anything if the component is already disposed
|
||||
if (IsDisposed || !disposing) return;
|
||||
IsDisposed = true;
|
||||
|
||||
WinApi.DestroyWindow(MessageWindowHandle);
|
||||
messageHandler = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user