我需要做什么才能使Windows 窗体应用程序能够在系统托盘中运行?
不是可以最小化到托盘的应用程序,而是只会存在于托盘中的应用程序,无非就是
- 一个图标
- 工具提示,以及
- “右键单击”菜单。
代码项目文章创建任务托盘应用程序提供了一个非常简单的解释和示例,用于创建仅存在于系统托盘中的应用程序。
基本上将Application.Run(new Form1());
行更改为Program.cs
启动一个继承自的类ApplicationContext
,并让该类的构造函数初始化一个NotifyIcon
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyCustomApplicationContext());
}
}
public class MyCustomApplicationContext : ApplicationContext
{
private NotifyIcon trayIcon;
public MyCustomApplicationContext ()
{
// Initialize Tray Icon
trayIcon = new NotifyIcon()
{
Icon = Resources.AppIcon,
ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Exit", Exit)
}),
Visible = true
};
}
void Exit(object sender, EventArgs e)
{
// Hide tray icon, otherwise it will remain shown until user mouses over it
trayIcon.Visible = false;
Application.Exit();
}
}
正如 mat1t 所说 - 您需要将 NotifyIcon 添加到您的应用程序,然后使用类似以下代码的内容来设置工具提示和上下文菜单:
this.notifyIcon.Text = "This is the tooltip";
this.notifyIcon.ContextMenu = new ContextMenu();
this.notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Option 1", new EventHandler(handler_method)));
此代码仅在系统托盘中显示图标:
this.notifyIcon.Visible = true; // Shows the notify icon in the system tray
如果您有表格(无论出于何种原因),则需要以下内容:
this.ShowInTaskbar = false; // Removes the application from the taskbar
Hide();
右键单击以获取上下文菜单是自动处理的,但是如果您想在左键单击时执行某些操作,则需要添加一个 Click 处理程序:
private void notifyIcon_Click(object sender, EventArgs e)
{
var eventArgs = e as MouseEventArgs;
switch (eventArgs.Button)
{
// Left click to reactivate
case MouseButtons.Left:
// Do your stuff
break;
}
}
我用 .NET 1.1 编写了一个托盘栏应用程序,我不需要表单。
首先,将项目的启动对象设置为 Sub Main
,在模块中定义。
然后以编程方式创建组件:NotifyIcon
和ContextMenu
.
一定要包括MenuItem
“退出”或类似的。
绑定ContextMenu
到NotifyIcon
.
调用Application.Run()
.
在 Quit 的事件处理程序中MenuItem
一定要调用 set NotifyIcon.Visible = False
,然后Application.Exit()
。添加您需要的内容ContextMenu
并正确处理:)
Form1
从代码中删除。Form1
.NotifyIcon
该类创建您的系统托盘图标(为其分配一个图标)。NotifyIcon
鼠标单击做出反应并区分右键单击和左键单击,设置上下文菜单并显示按下哪个按钮(右/左)。Application.Run()
保持应用程序运行Application.Exit()
退出。或者一个bool bRunning = true; while(bRunning){Application.DoEvents(); Thread.Sleep(10);}
. 然后设置bRunning = false;
退出应用程序。“系统托盘”应用程序只是一个普通的win表单应用程序,唯一的区别是它在windows系统托盘区域创建一个图标。使用 NotifyIcon 组件创建 sys.tray 图标,您可以在工具箱(公共控件)中找到它,并修改它的属性:图标,工具提示。它还使您能够处理鼠标单击和双击消息。
还有一件事,以实现外观或标准托盘应用程序。在主窗体显示事件中添加以下行:
private void MainForm_Shown(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
Hide();
}
据我所知,您仍然必须使用表单编写应用程序,但在表单上没有控件并且永远不会将其设置为可见。使用 NotifyIcon(可以在此处找到的 MSDN 示例)来编写您的应用程序。
这是我使用Visual Studio 2010和 .NET 4的方法
它是通知区域应用程序非常友好的框架......将 NotificationIcon 添加到基本表单并将自动生成的代码更改为以下代码就足够了:
public partial class Form1 : Form
{
private bool hidden = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
//this.WindowState = FormWindowState.Minimized;
this.Hide();
hidden = true;
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
if (hidden) // this.WindowState == FormWindowState.Minimized)
{
// this.WindowState = FormWindowState.Normal;
this.Show();
hidden = false;
}
else
{
// this.WindowState = FormWindowState.Minimized;
this.Hide();
hidden = true;
}
}
}
notifyIcon1->ContextMenu = gcnew
System::Windows::Forms::ContextMenu();
System::Windows::Forms::MenuItem^ nIItem = gcnew
System::Windows::Forms::MenuItem("Open");
nIItem->Click += gcnew System::EventHandler(this, &your_class::Open_NotifyIcon);
notifyIcon1->ContextMenu->MenuItems->Add(nIItem);
您可以创建表单,对其进行修改,然后将其Application.Run
作为参数传递给 。:
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form1();
form.Hide();
form.Opacity = 0;
form.ShowInTaskbar = false;
Application.Run(form);
}
}
在设计时将您的NotifyIcon
和ContextMenu
(如果需要)作为常规应用程序添加到您的表单中。确保您的Notifyicon
isVisible
并且具有关联的图标。这还可以让您使用以后出于任何原因可能需要的表格
只需添加
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
到您的表单对象。您只会在系统托盘上看到一个图标。