发布于 2012-04-20 16:33:51
代码项目文章创建任务托盘应用程序给出了一个非常简单的解释和示例,说明如何创建只存在于System中的应用程序。
基本上,将Program.cs
中的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();
}
}
发布于 2009-06-15 09:46:10
正如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;
}
}
发布于 2009-06-15 10:51:38
我用.NET 1.1编写了一个traybar应用程序,我不需要表单。
首先,将项目的启动对象设置为在模块中定义的Sub Main
。
然后以编程方式创建组件:NotifyIcon
和ContextMenu
。
一定要包括一个MenuItem
“退出”或类似的。
将ContextMenu
绑定到NotifyIcon
。
调用Application.Run()
。
在退出MenuItem
的事件处理程序中,一定要调用set NotifyIcon.Visible = False
,然后调用Application.Exit()
。将所需内容添加到ContextMenu
中并正确处理:)
https://stackoverflow.com/questions/995195
复制相似问题