我需要做什么才能使Windows窗体应用程序能够在系统托盘中运行?
不是一个可以最小化到托盘的应用程序,而是一个只存在于托盘中的应用程序
一个图标 一个工具提示,和 一个“右键”菜单。
我需要做什么才能使Windows窗体应用程序能够在系统托盘中运行?
不是一个可以最小化到托盘的应用程序,而是一个只存在于托盘中的应用程序
一个图标 一个工具提示,和 一个“右键”菜单。
当前回答
简单的添加
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
到你的表单对象。 你只会在系统托盘上看到一个图标。
其他回答
在。net 6中,我必须像这样工作我的类的核心:
private NotifyIcon trayIcon;
private ContextMenuStrip contextMenu1;
private ToolStripMenuItem menuItem1;
public MyCustomApplicationContext()
{
contextMenu1 = new System.Windows.Forms.ContextMenuStrip();
menuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += new System.EventHandler(Exit);
this.contextMenu1.Items.AddRange(
new System.Windows.Forms.ToolStripMenuItem[] {this.menuItem1 });
trayIcon = new NotifyIcon(){Icon = Resources.AppIcon, ContextMenuStrip = this.contextMenu1, Visible = true };
}
这是一个非常友好的框架通知区域应用程序…将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;
}
}
}
I've wrote a traybar app with .NET 1.1 and I didn't need a form. First of all, set the startup object of the project as a Sub Main, defined in a module. Then create programmatically the components: the NotifyIcon and ContextMenu. Be sure to include a MenuItem "Quit" or similar. Bind the ContextMenu to the NotifyIcon. Invoke Application.Run(). In the event handler for the Quit MenuItem be sure to call set NotifyIcon.Visible = False, then Application.Exit(). Add what you need to the ContextMenu and handle properly :)
您可以创建表单,修改它,然后将它传递给应用程序。作为参数运行。:
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);
}
}
添加你的通知图标和上下文菜单(如果需要)到你的表单在设计时作为一个常规的应用程序。确保你的通知图标是可见的,并有一个相关的图标。这还将允许您使用以后可能出于任何原因需要的表单
代码项目文章创建任务托盘应用程序给出了创建只存在于系统托盘中的应用程序的非常简单的解释和示例。
基本上改变应用程序。运行中(新Form1 ());来启动一个继承自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();
}
}