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
This commit is contained in:
Philipp Sumi
2009-03-30 23:04:09 +00:00
parent 6cc31cea5b
commit 229042b075
3 changed files with 96 additions and 110 deletions

View File

@@ -1,103 +0,0 @@
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()
{
//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();
}
}
/// <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 (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);
}
}
}

View File

@@ -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;
/// <summary>
/// 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.
/// </summary>
public NotifyIconVersion Version { get; set; }
private uint taskbarRestartMessageId;
/// <summary>
/// Used to track whether a mouse-up event is just
@@ -32,6 +32,30 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// </summary>
private bool isDoubleClick;
/// <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>
/// Window class ID.
/// </summary>
internal string WindowId { get; private set; }
/// <summary>
/// Handle for the message window.
/// </summary
internal IntPtr MessageWindowHandle { get; private set; }
/// <summary>
/// The version of the underlying icon. Defines how
/// incoming messages are interpreted.
/// </summary>
public NotifyIconVersion Version { get; set; }
#endregion
@@ -100,7 +124,74 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
#endregion
#region Process Window Messages
#region CreateMessageWindow
/// <summary>
/// Creates the helper message window that is used
/// to receive messages from the taskbar icon.
/// </summary>
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
/// <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 (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);
}
/// <summary>
/// Processes incoming system messages.
@@ -206,7 +297,6 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
#endregion
#region Dispose
/// <summary>

View File

@@ -54,7 +54,6 @@
<ItemGroup>
<Compile Include="BalloonIcon.cs" />
<Compile Include="Interop\TrayLocator.cs" />
<Compile Include="Interop\WindowMessageSink.Handle.cs" />
<Compile Include="Interop\WindowClass.cs" />
<Compile Include="PopupActivationMode.cs" />
<Compile Include="RoutedEventHelper.cs" />