mirror of
https://github.com/ckaczor/wpf-notifyicon.git
synced 2026-02-17 02:51:51 -05:00
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:
@@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
||||||
@@ -19,11 +20,10 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
|||||||
public const int CallbackMessageId = 0x400;
|
public const int CallbackMessageId = 0x400;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The version of the underlying icon. Defines how
|
/// The ID of the message that is being received if the
|
||||||
/// incoming messages are interpreted.
|
/// taskbar is (re)started.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public NotifyIconVersion Version { get; set; }
|
private uint taskbarRestartMessageId;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Used to track whether a mouse-up event is just
|
/// Used to track whether a mouse-up event is just
|
||||||
@@ -32,6 +32,30 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private bool isDoubleClick;
|
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
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -100,7 +124,74 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
|||||||
#endregion
|
#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>
|
/// <summary>
|
||||||
/// Processes incoming system messages.
|
/// Processes incoming system messages.
|
||||||
@@ -206,7 +297,6 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#region Dispose
|
#region Dispose
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -54,7 +54,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="BalloonIcon.cs" />
|
<Compile Include="BalloonIcon.cs" />
|
||||||
<Compile Include="Interop\TrayLocator.cs" />
|
<Compile Include="Interop\TrayLocator.cs" />
|
||||||
<Compile Include="Interop\WindowMessageSink.Handle.cs" />
|
|
||||||
<Compile Include="Interop\WindowClass.cs" />
|
<Compile Include="Interop\WindowClass.cs" />
|
||||||
<Compile Include="PopupActivationMode.cs" />
|
<Compile Include="PopupActivationMode.cs" />
|
||||||
<Compile Include="RoutedEventHelper.cs" />
|
<Compile Include="RoutedEventHelper.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user