是的,也可以不使用Form窗口来实现NotifyIcon及右键菜单的功能。步骤如下:
-
新建一个控制台应用程序。
-
添加以下引用:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
- 定义NotifyIcon和ContextMenu:
private static NotifyIcon notifyIcon = null;
private static ContextMenu contextMenu = null;
- 使用Win32 API创建窗口(WndProc)及修改窗口可见性:
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOW = 5;
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MINIMIZE = 0xF020;
const int SC_RESTORE = 0xF120;
if (m.Msg == WM_SYSCOMMAND)
{
switch (m.WParam.ToInt32())
{
case SC_MINIMIZE:
ShowWindow(this.Handle, SW_HIDE);
break;
case SC_RESTORE:
ShowWindow(this.Handle, SW_SHOWNORMAL);
break;
}
}
base.WndProc(ref m);
}
private static void ShowForm()
{
IntPtr hWnd = FindWindow(null, "My Application"); //窗口标题
if (hWnd != IntPtr.Zero)
{
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form frm = new Form();
frm.Name = "My Application";
frm.ShowInTaskbar = false;
frm.WindowState = FormWindowState.Minimized;
frm.Show();
int GWL_STYLE = -16;
int WS_EX_TOOLWINDOW = 0x00000080;
int value = GetWindowLong(frm.Handle, GWL_STYLE);
SetWindowLong(frm.Handle, GWL_STYLE, (value & ~WS_EX_TOOLWINDOW));
Application.Run(frm);
}
}
这里使用了ShowForm()方法来首次显示窗口,并通过FindWindow()和SetForegroundWindow()方法来唤起窗口。
- 初始化NotifyIcon和ContextMenu:
private static void InitNotifyIcon()
{
if (notifyIcon == null)
{
notifyIcon = new NotifyIcon();
notifyIcon.Visible = true;
notifyIcon.Icon = new Icon("icon.ico"); // 托盘图标
notifyIcon.Text = "My Application"; // 提示文本
// 增加右键菜单
contextMenu = new ContextMenu();
contextMenu.MenuItems.Add(new MenuItem("菜单项1", menuItem1_Click));
contextMenu.MenuItems.Add(new MenuItem("菜单项2", menuItem2_Click));
notifyIcon.ContextMenu = contextMenu;
// 双击托盘图标,显示窗口
notifyIcon.DoubleClick += delegate (object sender, EventArgs args)
{
ShowForm();
};
}
}
private static void menuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked Menu Item 1!");
}
private static void menuItem2_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked Menu Item 2!");
}
- 在Main()方法中初始化NotifyIcon:
static void Main(string[] args)
{
InitNotifyIcon();
Application.Run();
}
- 完整代码示例如下:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace NotifyIconExample
{
class Program : Form
{
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOW = 5;
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
private static NotifyIcon notifyIcon = null;
private static ContextMenu contextMenu = null;
[STAThread]
static void Main(string[] args)
{
InitNotifyIcon();
Application.Run();
}
private static void InitNotifyIcon()
{
if (notifyIcon == null)
{
notifyIcon = new NotifyIcon();
notifyIcon.Visible = true;
notifyIcon.Icon = new Icon("icon.ico"); // 托盘图标
notifyIcon.Text = "My Application"; // 提示文本
// 增加右键菜单
contextMenu = new ContextMenu();
contextMenu.MenuItems.Add(new MenuItem("菜单项1", menuItem1_Click));
contextMenu.MenuItems.Add(new MenuItem("菜单项2", menuItem2_Click));
notifyIcon.ContextMenu = contextMenu;
// 双击托盘图标,显示窗口
notifyIcon.DoubleClick += delegate (object sender, EventArgs args)
{
ShowForm();
};
}
}
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MINIMIZE = 0xF020;
const int SC_RESTORE = 0xF120;
if (m.Msg == WM_SYSCOMMAND)
{
switch (m.WParam.ToInt32())
{
case SC_MINIMIZE:
ShowWindow(this.Handle, SW_HIDE);
break;
case SC_RESTORE:
ShowWindow(this.Handle, SW_SHOWNORMAL);
break;
}
}
base.WndProc(ref m);
}
private static void ShowForm()
{
IntPtr hWnd = FindWindow(null, "My Application"); //窗口标题
if (hWnd != IntPtr.Zero)
{
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form frm = new Form();
frm.Name = "My Application";
frm.ShowInTaskbar = false;
frm.WindowState = FormWindowState.Minimized;
frm.Show();
int GWL_STYLE = -16;
int WS_EX_TOOLWINDOW = 0x00000080;
int value = GetWindowLong(frm.Handle, GWL_STYLE);
SetWindowLong(frm.Handle, GWL_STYLE, (value & ~WS_EX_TOOLWINDOW));
Application.Run(frm);
}
}
private static void menuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked Menu Item 1!");
}
private static void menuItem2_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked Menu Item 2!");
}
}
}
通过以上代码,可以实现无界面程序中的NotifyIcon及右键菜单功能,同时也能通过控制台的方式管理应用程序。