以下是一个使用win api实现notifyIcon和关联菜单的C#示例,适用于控制台程序,不能使用System.Windows.Forms空间:
using System;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool Shell_NotifyIcon(uint dwMessage, ref NOTIFYICONDATA pnid);
[DllImport("user32.dll")]
public static extern IntPtr CreatePopupMenu();
[DllImport("user32.dll")]
public static extern BOOL AppendMenu(IntPtr hMenu, DWORD uFlags, UINT_PTR uIDNewItem, string lpNewItem);
[DllImport("user32.dll")]
public static extern int TrackPopupMenu(IntPtr hMenu, uint uFlags, int x, int y, int nReserved, IntPtr hWnd, IntPtr prcRect);
[DllImport("user32.dll")]
public static extern void DestroyMenu(IntPtr hMenu);
const uint WM_USER = 0x0400;
const uint NIM_ADD = WM_USER + 0x0;
const uint NIM_MODIFY = WM_USER + 0x1;
const uint NIM_DELETE = WM_USER + 0x2;
const uint NIF_ICON = 0x2;
const uint NIF_MESSAGE = 0x1;
const uint NIF_TIP = 0x4;
const uint NIF_INFO = 0x10;
const uint NIM_SETVERSION = WM_USER + 0x4;
const uint NOTIFYICON_VERSION_4 = 0x4;
const int SW_HIDE = 0;
const int SW_SHOW = 5;
const int WM_COMMAND = 0x0111;
const int IDM_HELLO = 1000;
const int IDM_EXIT = 1001;
const int MF_STRING = 0x0000;
const int MF_SEPARATOR = 0x800;
const int TPMS_ALIGN_TOPLEFT = 0x0000;
const int TPM_RETURNCMD = 0x0100;
const int IDI_ICON1 = 101;
[StructLayout(LayoutKind.Sequential)]
public struct NOTIFYICONDATA
{
public uint cbSize;
public IntPtr hWnd;
public uint uID;
public uint uFlags;
public uint uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szTip;
public uint dwState;
public uint dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szInfo;
public uint uVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string szInfoTitle;
public uint dwInfoFlags;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[STAThread]
static void Main(string[] args)
{
var consoleHandle = GetConsoleWindow();
ShowWindow(consoleHandle, SW_HIDE);
var notifyIconData = new NOTIFYICONDATA();
notifyIconData.cbSize = (uint)Marshal.SizeOf(notifyIconData);
notifyIconData.hWnd = consoleHandle;
notifyIconData.uID = 0;
notifyIconData.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
notifyIconData.uCallbackMessage = WM_USER + 1;
notifyIconData.hIcon = LoadIcon(IntPtr.Zero, IDI_INFORMATION);
notifyIconData.szTip = "NotifyIcon Demo";
Shell_NotifyIcon(NIM_ADD, ref notifyIconData);
var hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, IDM_HELLO, "Say Hello");
AppendMenu(hMenu, MF_SEPARATOR, 0, "");
AppendMenu(hMenu, MF_STRING, IDM_EXIT, "Exit");
TRACKMOUSEEVENT tme = new TRACKMOUSEEVENT();
tme.cbSize = Marshal.SizeOf(tme);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = consoleHandle;
while (true)
{
int x = Console.CursorLeft + 1;
int y = Console.CursorTop + 1;
Console.SetCursorPosition(0, 0);
Console.WriteLine("Right-click on the tray icon to show the menu.");
var res = TrackPopupMenu(hMenu, TPM_RETURNCMD | TPMS_ALIGN_TOPLEFT, x, y, 0, consoleHandle, IntPtr.Zero);
if (res == IDM_HELLO)
{
Console.WriteLine("Hello, World!");
}
if (res == IDM_EXIT)
{
break;
}
}
DestroyMenu(hMenu);
Shell_NotifyIcon(NIM_DELETE, ref notifyIconData);
}
}
}
在此示例中,我们使用了以下win api函数:
- Shell_NotifyIcon:用于添加、修改和删除托盘图标。
- CreatePopupMenu:创建一个弹出式菜单。
- AppendMenu:向菜单中添加一个菜单项。
- TrackPopupMenu:根据鼠标当前位置和指定的菜单句柄显示一个弹出式菜单。
- DestroyMenu:释放由CreatePopupMenu创建的菜单句柄。
要使用以上函数,需要在代码中添加相应的DllImport声明。此外,还有一些常量和结构体需要声明,用于传递给win api函数。
控制台窗口默认情况下会在任务栏中显示,在本示例中我们使用了ShowWindow函数隐藏了控制台窗口。
当用户在托盘图标上右键单击时,我们会显示一个弹出式菜单,菜单项包括“Say Hello”和“Exit”。用户单击“Say Hello”菜单项时,会将“Hello, World!”输出到控制台。当用户单击“Exit”菜单项时,程序将退出。