我需要做什么才能使Windows窗体应用程序能够在系统托盘中运行?
不是一个可以最小化到托盘的应用程序,而是一个只存在于托盘中的应用程序
一个图标 一个工具提示,和 一个“右键”菜单。
我需要做什么才能使Windows窗体应用程序能够在系统托盘中运行?
不是一个可以最小化到托盘的应用程序,而是一个只存在于托盘中的应用程序
一个图标 一个工具提示,和 一个“右键”菜单。
当前回答
在。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 };
}
其他回答
在。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 };
}
Create a new Windows Application with the wizard. Delete Form1 from the code. Remove the code in Program.cs starting up the Form1. Use the NotifyIcon class to create your system tray icon (assign an icon to it). Add a contextmenu to it. Or react to NotifyIcon's mouseclick and differenciate between Right and Left click, setting your contextmenu and showing it for which ever button (right/left) was pressed. Application.Run() to keep the app running with Application.Exit() to quit. Or a bool bRunning = true; while(bRunning){Application.DoEvents(); Thread.Sleep(10);}. Then set bRunning = false; to exit the app.
下面是我如何用Visual Studio 2010, .NET 4做到的
Create a Windows Forms Application, set 'Make single instance application' in properties Add a ContextMenuStrip Add some entries to the context menu strip, double click on them to get the handlers, for example, 'exit' (double click) -> handler -> me.Close() Add a NotifyIcon, in the designer set contextMenuStrip to the one you just created, pick an icon (you can find some in the VisualStudio folder under 'common7...') Set properties for the form in the designer: FormBorderStyle:none, ShowIcon:false, ShowInTaskbar:false, Opacity:0%, WindowState:Minimized Add Me.Visible=false at the end of Form1_Load, this will hide the icon when using Ctrl + Tab Run and adjust as needed.
“系统托盘”应用程序只是一个普通的win表单应用程序,唯一的区别是它在windows系统托盘区域创建了一个图标。为了创建sys。托盘图标使用NotifyIcon组件,你可以在工具箱(常用控件)中找到它,并修改它的属性:图标,工具提示。它还允许您处理鼠标单击和双击消息。
还有一件事,为了实现外观和感觉或标准托盘应用程序。添加followinf行在你的主窗体显示事件:
private void MainForm_Shown(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
Hide();
}
代码项目文章创建任务托盘应用程序给出了创建只存在于系统托盘中的应用程序的非常简单的解释和示例。
基本上改变应用程序。运行中(新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();
}
}