xyz*_*xyz 209 .net c# system-tray winforms
要使Windows窗体应用程序在系统托盘中运行,我需要做什么?
不是可以最小化托盘的应用程序,而是仅存在于托盘中的应用程序,只有图标,工具提示和"右键单击"菜单.
Mic*_*ens 153
使用NotifyIcon的基本答案是正确的,但是,像许多.NET一样,正确地执行它有许多细微之处.Brad提到的教程给出了很好的基础知识,但没有解决这些问题:
我刚刚在Simple-Talk.com上发表了一篇文章,详细介绍了这些要点,提供了一个托盘应用程序框架,您可以立即使用它,还有一个完整的,真实的示例应用程序,可以在实践中显示所有内容.请参阅2010年11月发布的"在.NET中创建托盘应用程序:实用指南".
Faw*_*Izy 73
代码项目文章Creating a Tasktray Application提供了一个非常简单的解释和创建仅在系统托盘中存在的应用程序的示例.
基本上改变Application.Run(new Form1());行,Program.cs而不是启动一个继承自的类ApplicationContext,并让该类的构造函数初始化aNotifyIcon
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ese 19
我采用了.NET Core已接受的答案,使用建议的替代品来替换已弃用的类:
程序.cs
namespace TrayOnlyWinFormsDemo
{
internal static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new MyCustomApplicationContext());
}
}
}
Run Code Online (Sandbox Code Playgroud)
MyCustomApplicationContext.cs
using TrayOnlyWinFormsDemo.Properties; // Needed for Resources.AppIcon
namespace TrayOnlyWinFormsDemo
{
public class MyCustomApplicationContext : ApplicationContext
{
private NotifyIcon trayIcon;
public MyCustomApplicationContext()
{
trayIcon = new NotifyIcon()
{
Icon = Resources.AppIcon,
ContextMenuStrip = new ContextMenuStrip()
{
Items = { new ToolStripMenuItem("Exit", null, Exit) }
},
Visible = true
};
}
void Exit(object? sender, EventArgs e)
{
trayIcon.Visible = false;
Application.Exit();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*isF 17
正如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)));
Run Code Online (Sandbox Code Playgroud)
此代码仅显示系统托盘中的图标:
this.notifyIcon.Visible = true; // Shows the notify icon in the system tray
Run Code Online (Sandbox Code Playgroud)
如果您有表格(无论出于何种原因),将需要以下内容:
this.ShowInTaskbar = false; // Removes the application from the taskbar
Hide();
Run Code Online (Sandbox Code Playgroud)
右键单击以获取上下文菜单是自动处理的,但如果要在左键单击上执行某些操作,则需要添加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;
}
}
Run Code Online (Sandbox Code Playgroud)
M.T*_*ini 15
我用.NET 1.1编写了一个托盘栏应用程序,我不需要表格.
首先,将项目的启动对象设置为Sub Main,在模块中定义.
然后以编程方式创建组件:NotifyIcon和ContextMenu.
一定要包括MenuItem"退出"或类似.
绑定ContextMenu到NotifyIcon.
调用Application.Run().
在Quit的事件处理程序MenuItem中确保调用set NotifyIcon.Visible = False,然后Application.Exit().添加你需要的ContextMenu和正确处理:)
Wol*_*lf5 10
Form1从代码中删除.Form1.NotifyIcon该类创建系统托盘图标(为其指定一个图标).NotifyIcon鼠标点击做出反应,并在右键和左键单击之间进行区分,设置上下文菜单并显示按下了哪个按钮(右/左).Application.Run()保持应用程序运行Application.Exit()以退出.或者a bool bRunning = true; while(bRunning){Application.DoEvents(); Thread.Sleep(10);}.然后设置bRunning = false;退出应用程序."系统托盘"应用程序只是一个常规的win表单应用程序,唯一的区别是它在Windows系统托盘区域中创建了一个图标.为了使用NotifyIcon组件创建sys.tray图标,您可以在Toolbox(公共控件)中找到它,并修改它的属性:Icon,tool tip.它还使您能够处理鼠标单击和双击消息.
还有一件事,为了实现外观和感觉或标准托盘应用程序.在主窗体show事件中添加followinf行:
private void MainForm_Shown(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
Hide();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
179708 次 |
| 最近记录: |