【问题标题】:Put a program in the system tray at startup启动时将程序放入系统托盘
【发布时间】:2008-11-12 11:10:24
【问题描述】:

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

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

我不想从系统托盘以外的任何地方完全删除它,我只想让它最小化。

有什么想法吗?

谢谢

【问题讨论】:

    标签: c# .net vb.net


    【解决方案1】:

    在您的主程序中,您可能有如下形式的一行:

    Application.Run(new Form1());
    

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

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

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

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

    【讨论】:

      【解决方案2】:

      你就是这样做的

      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();
          }
      }
      

      【讨论】:

        【解决方案3】:

        如果您使用的是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 更改窗口样式(有点 hackier):

        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);
        

        【讨论】:

          【解决方案4】:

          有点麻烦,您可以将启动应用程序的快捷方式配置为“最小化运行”吗?这可能会给你你需要的东西!

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


          (来源:unixwiz.net

          【讨论】:

            【解决方案5】:

            由于这是用 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
            

            【讨论】:

              【解决方案6】:

              给你:

              创建 2 个类,1 个继承自 ApplicationContext。另一个只包含一个 Main 例程。我做了一个例子,它有一个表单和一个通知图标,双击时会弹出表单并再次返回。

              记得在我的项目设置中将“Sub Main”设置为您的启动对象,并指向一个真正的 *.ico 文件而不是 f:\TP.ico .. :)

              代码当然应该填充适当的错误处理代码。

              第一类:

              Imports System.threading 
              Imports System.Runtime.InteropServices 
              Imports System.Windows.Forms
              
              
              Public Class Class1
              
                  <System.STAThread()> _
                      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
              

              【讨论】:

                【解决方案7】:

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

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

                【讨论】:

                  猜你喜欢
                  • 2010-10-19
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多