From 229042b075d9ad2da61f179dbfef073d8aead07f Mon Sep 17 00:00:00 2001 From: Philipp Sumi Date: Mon, 30 Mar 2009 23:04:09 +0000 Subject: [PATCH] WPF NotifyIcon -------------- CHG Merged partial classes of WindowMessageSink.cs into a single file. git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@54 9f600761-6f11-4665-b6dc-0185e9171623 --- .../Interop/WindowMessageSink.Handle.cs | 103 ------------------ .../Interop/WindowMessageSink.cs | 102 ++++++++++++++++- Source/NotifyIconWpf/NotifyIconWpf.csproj | 1 - 3 files changed, 96 insertions(+), 110 deletions(-) delete mode 100644 Source/NotifyIconWpf/Interop/WindowMessageSink.Handle.cs diff --git a/Source/NotifyIconWpf/Interop/WindowMessageSink.Handle.cs b/Source/NotifyIconWpf/Interop/WindowMessageSink.Handle.cs deleted file mode 100644 index d99dd34..0000000 --- a/Source/NotifyIconWpf/Interop/WindowMessageSink.Handle.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System; -using System.ComponentModel; - -namespace Hardcodet.Wpf.TaskbarNotification.Interop -{ - /// - /// Provides low level code that is used to receive - /// window messages without having a window that - /// prevents a WPF application from shutting down - /// properly. - /// - public partial class WindowMessageSink - { - /// - /// Window class ID. - /// - private string WindowId; - - /// - /// Handle for the message window. - /// - /// The ID of the message that is being received if the - /// taskbar is (re)started. - /// - private uint taskbarRestartMessageId; - - /// - /// 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. - /// - private WindowProcedureHandler messageHandler; - - - /// - /// Creates the helper message window that is used - /// to receive messages from the taskbar icon. - /// - private void CreateMessageWindow() - { - //generate a unique ID for the window - WindowId = "WPFTaskbarIcon_" + DateTime.Now.Ticks; - - //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(); - } - } - - - - /// - /// Callback method that receives messages from the taskbar area. - /// - private long OnWindowMessageReceived(IntPtr hwnd, uint messageId, uint wparam, uint lparam) - { - if (messageId == taskbarRestartMessageId) - { - //recreate the icon if the taskbar was restarted (e.g. due to Win Explorer shutdown) - TaskbarCreated(); - } - - //forward message - ProcessWindowMessage(messageId, wparam, lparam); - - // Pass the message to the default window procedure - return WinApi.DefWindowProc(hwnd, messageId, wparam, lparam); - } - - } -} \ No newline at end of file diff --git a/Source/NotifyIconWpf/Interop/WindowMessageSink.cs b/Source/NotifyIconWpf/Interop/WindowMessageSink.cs index 3e59a39..9a42b85 100644 --- a/Source/NotifyIconWpf/Interop/WindowMessageSink.cs +++ b/Source/NotifyIconWpf/Interop/WindowMessageSink.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Diagnostics; namespace Hardcodet.Wpf.TaskbarNotification.Interop @@ -19,11 +20,10 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop public const int CallbackMessageId = 0x400; /// - /// The version of the underlying icon. Defines how - /// incoming messages are interpreted. + /// The ID of the message that is being received if the + /// taskbar is (re)started. /// - public NotifyIconVersion Version { get; set; } - + private uint taskbarRestartMessageId; /// /// Used to track whether a mouse-up event is just @@ -32,6 +32,30 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop /// private bool isDoubleClick; + /// + /// 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. + /// + private WindowProcedureHandler messageHandler; + + /// + /// Window class ID. + /// + internal string WindowId { get; private set; } + + /// + /// Handle for the message window. + /// + /// The version of the underlying icon. Defines how + /// incoming messages are interpreted. + /// + public NotifyIconVersion Version { get; set; } + #endregion @@ -100,7 +124,74 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop #endregion - #region Process Window Messages + #region CreateMessageWindow + + /// + /// Creates the helper message window that is used + /// to receive messages from the taskbar icon. + /// + private void CreateMessageWindow() + { + //generate a unique ID for the window + WindowId = "WPFTaskbarIcon_" + DateTime.Now.Ticks; + + //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(); + } + } + + #endregion + + + #region Handle Window Messages + + /// + /// Callback method that receives messages from the taskbar area. + /// + private long OnWindowMessageReceived(IntPtr hwnd, uint messageId, uint wparam, uint lparam) + { + if (messageId == taskbarRestartMessageId) + { + //recreate the icon if the taskbar was restarted (e.g. due to Win Explorer shutdown) + TaskbarCreated(); + } + + //forward message + ProcessWindowMessage(messageId, wparam, lparam); + + // Pass the message to the default window procedure + return WinApi.DefWindowProc(hwnd, messageId, wparam, lparam); + } + /// /// Processes incoming system messages. @@ -206,7 +297,6 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop #endregion - #region Dispose /// diff --git a/Source/NotifyIconWpf/NotifyIconWpf.csproj b/Source/NotifyIconWpf/NotifyIconWpf.csproj index a3b1416..f0afef6 100644 --- a/Source/NotifyIconWpf/NotifyIconWpf.csproj +++ b/Source/NotifyIconWpf/NotifyIconWpf.csproj @@ -54,7 +54,6 @@ -