在启动时将程序放入系统托盘中

我按照常用链接提示将应用程序减少到系统托盘: http : //www.developer.com/net/csharp/article.php/3336751现在它可以工作,但仍然存在问题:我的应用程序已显示什么时候开始; 我希望它直接在系统托盘中启动。 我试图在Load事件中最小化并隐藏它,但它什么也没做。

编辑:我可以,如海报建议,修改快捷方式属性,但我宁愿使用代码:我没有完全控制安装软件的每台计算机。

我不想从除了systray之外的任何地方完全删除它,我只是想让它开始最小化。

有任何想法吗 ?

谢谢

在你的主程序中,你可能有一行表格:

Application.Run(new Form1()); 

这将强制显示表单。 您需要创建表单,但不要将其传递给Application.Run

 Form1 form = new Form1(); Application.Run(); 

请注意,在调用Application.ExitThread()之前,程序现在不会终止。 最好从FormClosed事件的处理程序执行此FormClosed

 private void Form1_FormClosed(object sender, FormClosedEventArgs e) { Application.ExitThread(); } 

这就是你如何做到的

 static class Program { [STAThread] static void Main() { NotifyIcon icon = new NotifyIcon(); icon.Icon = System.Drawing.SystemIcons.Application; icon.Click += delegate { MessageBox.Show("Bye!"); icon.Visible = false; Application.Exit(); }; icon.Visible = true; Application.Run(); } } 

如果您使用的是NotifyIcon ,请尝试将ShowInTaskbar更改为false。

要从Alt + Tab屏幕中删除它,请尝试更改窗口边框样式; 我相信一些工具窗口样式没有出现……

就像是:

 using System; using System.Windows.Forms; class MyForm : Form { NotifyIcon sysTray; MyForm() { sysTray = new NotifyIcon(); sysTray.Icon = System.Drawing.SystemIcons.Asterisk; sysTray.Visible = true; sysTray.Text = "Hi there"; sysTray.MouseClick += delegate { MessageBox.Show("Boo!"); }; ShowInTaskbar = false; FormBorderStyle = FormBorderStyle.SizableToolWindow; Opacity = 0; WindowState = FormWindowState.Minimized; } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new MyForm()); } } 

如果它仍然出现在Alt + Tab中,您可以通过p / invoke更改窗口样式(有点粗糙):

 protected override void OnLoad(EventArgs e) { base.OnLoad(e); IntPtr handle = this.Handle; int currentStyle = GetWindowLong(handle, GWL_EXSTYLE); SetWindowLong(handle, GWL_EXSTYLE, currentStyle | WS_EX_TOOLWINDOW); } private const int GWL_EXSTYLE = -20, WS_EX_TOOLWINDOW = 0x00000080; [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern int SetWindowLong(IntPtr window, int index, int value); [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern int GetWindowLong(IntPtr window, int index); 

作为一个细节,你可以配置启动你的应用程序的快捷方式“运行最小化”? 这可能会给你你所需要的!

像这样:(图片只是谷歌的一个例子)……

运行最小化http://sofzh.miximages.com/c%23/ygpm-shortcut-4.gif

由于这是用vb.net标记的,这是我在刚完成的Windows服务和控制器应用程序中所做的,在项目中添加代码模块,在Sub Main()中设置NotifyIcon及其相关的上下文菜单,然后设置应用程序的启动对象到Sub Main()而不是Form。

 Public mobNotifyIcon As NotifyIcon Public WithEvents mobContextMenu As ContextMenu Public Sub Main() mobContextMenu = New ContextMenu SetupMenu() mobNotifyIcon = New NotifyIcon() With mobNotifyIcon .Icon = My.Resources.NotifyIcon .ContextMenu = mobContextMenu .BalloonTipText = String.Concat("Monitor the EDS Transfer Service", vbCrLf, "Right click icon for menu") .BalloonTipIcon = ToolTipIcon.Info .BalloonTipTitle = "EDS Transfer Monitor" .Text = "EDS Transfer Service Monitor" AddHandler .MouseClick, AddressOf showBalloon .Visible = True End With Application.Run() End Sub Private Sub SetupMenu() With mobContextMenu .MenuItems.Add(New MenuItem("Configure", New EventHandler(AddressOf Config))) .MenuItems.Add("-") .MenuItems.Add(New MenuItem("Start", New EventHandler(AddressOf StartService))) .MenuItems.Add(New MenuItem("Stop", New EventHandler(AddressOf StopService))) .MenuItems.Add("-") .MenuItems.Add(New MenuItem("Exit", New EventHandler(AddressOf ExitController))) End With GetServiceStatus() End Sub 

在Config()中,我创建了一个表单实例并显示它。

 Private Sub Config(ByVal sender As Object, ByVal e As EventArgs) Using cs As New ConfigureService cs.Show() End Using End Sub 

干得好:

创建2个类,1个inheritance自ApplicationContext。 另一个只包含一个Main例程。 我做了一个有一个表单和一个通知图标的例子,当双击时会显示该表单然后再返回。

请记住在My Project设置中将“Sub Main”设置为启动对象,并指向一个真实的* .ico文件,而不是f:\ TP.ico .. 🙂

代码当然应该填充适当的error handling代码。

1类:

 Imports System.threading Imports System.Runtime.InteropServices Imports System.Windows.Forms Public Class Class1  _ Public Shared Sub Main() Try System.Windows.Forms.Application.EnableVisualStyles() System.Windows.Forms.Application.DoEvents() System.Windows.Forms.Application.Run(New Class2) Catch invEx As Exception Application.Exit() End Try End Sub 'Main End Class 

等级2:

 Imports System.Windows.Forms Imports System.drawing Public Class Class2 Inherits System.Windows.Forms.ApplicationContext Private WithEvents f As New System.Windows.Forms.Form Private WithEvents nf As New System.Windows.Forms.NotifyIcon Public Sub New() f.Size = New Drawing.Size(50, 50) f.StartPosition = FormStartPosition.CenterScreen f.WindowState = Windows.Forms.FormWindowState.Minimized f.ShowInTaskbar = False nf.Visible = True nf.Icon = New Icon("f:\TP.ico") End Sub Private Sub nf_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles nf.DoubleClick If f.WindowState <> Windows.Forms.FormWindowState.Minimized Then f.WindowState = Windows.Forms.FormWindowState.Minimized f.Hide() Else f.WindowState = Windows.Forms.FormWindowState.Normal f.Show() End If End Sub Private Sub f_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles f.FormClosed Application.Exit() End Sub End Class 

这将向您展示如何使用NotifyIcon将启动控制为最小化或正常以及更多。

更多信息: http : //code.msdn.microsoft.com/TheNotifyIconExample