csharp
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct NOTIFYICONDATA
{
public int cbSize;
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szTip;
}
// 设置 ICON、ToolTip、回调消息等信息
var data = new NOTIFYICONDATA();
data.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
data.hWnd = Handle; // 窗口句柄,可以是任何一个窗口
data.uID = 0;
data.uFlags = 0x00000010; // NIF_ICON | NIF_TIP | NIF_MESSAGE
data.uCallbackMessage = WM_USER; // 自定义消息号
data.hIcon = iconHandle; // ICON句柄
data.szTip = "测试消息"; // Tooltip文本
csharp
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern bool Shell_NotifyIcon(uint dwMessage, ref NOTIFYICONDATA pnid);
// 添加到通知区域
if (Shell_NotifyIcon(0x00000000 | 0x00000002, ref data)) // NIM_ADD | NIM_SETVERSION
{
Debug.WriteLine("Notification button added to the notification area.");
}
csharp
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace NotificationButtonSample
{
class Program
{
const int WM_USER = 0x0400;
static void Main(string[] args)
{
// 创建 NOTIFYICONDATA
var data = new NOTIFYICONDATA();
data.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
data.hWnd = Process.GetCurrentProcess().MainWindowHandle;
data.uID = 0;
data.uFlags = 0x00000010; // NIF_ICON | NIF_TIP | NIF_MESSAGE
data.uCallbackMessage = WM_USER;
data.hIcon = LoadIcon(IntPtr.Zero, new IntPtr(32512)); // 获取系统设置的默认图标
data.szTip = "测试消息";
// 添加到通知区域
if (Shell_NotifyIcon(0x00000000 | 0x00000002, ref data)) // NIM_ADD | NIM_SETVERSION
{
Debug.WriteLine("Notification button added to the notification area.");
}
// 移动通知栏按钮
IntPtr taskBarHWND = FindWindow("Shell_TrayWnd", null);
if (taskBarHWND != IntPtr.Zero)
{
IntPtr notificationAreaHWND = FindWindowEx(taskBarHWND, IntPtr.Zero, "TrayNotifyWnd", null);
if (notificationAreaHWND != IntPtr.Zero)
{
IntPtr buttonHWND = FindWindowEx(notificationAreaHWND, IntPtr.Zero, "Button", null);
if (buttonHWND != IntPtr.Zero)
{
SetWindowPos(buttonHWND, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
Debug.WriteLine("Notification button moved to the first position.");
}
}
}
// 移除通知区域按钮
if (Shell_NotifyIcon(0x00000002, ref data)) // NIM_DELETE
{
Debug.WriteLine("Notification button removed from the notification area.");
}
}
const int SWP_NOMOVE = 0x0002;
const int SWP_NOSIZE = 0x0001;
const int SWP_NOZORDER = 0x0004;
const int SWP_SHOWWINDOW = 0x0040;
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool Shell_NotifyIcon(uint dwMessage, ref NOTIFYICONDATA pnid);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct NOTIFYICONDATA
{
public int cbSize;
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szTip;
}
}
}
注意:在移动通知栏按钮之前需要先添加到通知区域。此外,为了方便起见,上面的代码中直接使用了系统设置的默认图标。具体应用中,应该根据实际需要替换为自定义图标,而且也需要在不需要使用图标时释放相关资源。