2009-02-06 78 views
10

我有一个程序,只需要一个NotifyIcon按预期工作。所以我一直试图让程序启动时隐藏主窗体。启动时隐藏表格

在frmMain_Load,我都尝试

this.Hide(); 
this.Visible = false; 

没有成功。

他们在其他方法中工作,如NotifyIcon_MouseClick方法,但我希望它隐藏在Load。

我在另外一个问题在这里看到的那么,马蒂亚斯建议是:

BeginInvoke(new MethodInvoker(delegate 
{ 
    Hide(); 
})); 

这个工作,但是当我启动该程序,我可以看到闪烁的形式实快。这比没有好,但我想知道是否有更好的解决方案。

谢谢。

回答

15
// In Your Program.cs Convert This 
static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new Form1()); 
} 

// To This 
static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Form1 TheForm = new Form1(); 
    Application.Run(); 
} 

// Call Application.Exit() From Anywhere To Stop Application.Run() Message Pump and Exit Application 
2

不要在表单上调用Show或ShowDialog,你可以让你的Application.Run目标是一个自定义的类,然后实例化一个表单并且不会显示或创建一个NotifyIcon实例并处理所有的事情。

+0

你也可以调用Application.Run()不带任何参数。 当你的应用程序完成后调用应用程序。出口(); – VBNight 2009-02-06 21:15:50

+0

如果您需要获取某些事件通知(例如电源事件等),而您确实需要一个(不可见的)表单来获取它们,则这不起作用。 – 2009-02-06 21:17:50

+0

感谢您的回复。我是否必须创建自己的自定义表单类,或者我可以在某处删除Show()行吗?如果我必须创建自己的自定义类,那我该怎么做?我一直在代码中寻找,但我没有发现任何有趣的东西。 – sippa 2009-02-06 21:22:42

5

有一个简单的方法,如果你的程序的Visual Studio生成的Program.cs文件默认:

[STAThread] 
static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault (false); 
    Application.Run (new MainForm()); 
} 

调用Run简单的事实会的确使窗体可见。试着做你的窗体的属性如下:

  1. 设置WindowStateMinimized
  2. 设置ShowInTaskbarfalse

这应该做的伎俩!

1

您也可以将this.hide = true放入form_shown事件中。我相信事件只会在加载事件之后触发一次。尽管如果你的表单有很多控件和/或计算机速度很慢,你可能会看到闪烁。

1

如果您的程序不需要运行窗体,那么最好的方法是根本没有窗体。在程序代码中设置您的NotifyIcon,然后输入一个循环,直到您想通过设置某个值或调用某种方法退出该程序。在此示例中,将UserExitCalled设置为true(Program.UserExitCalled = true)将导致程序退出。这里是一个简单的例子:

static class Program { 
    internal static Boolean UserExitCalled; 

    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 

     // Setup your tray icon here 

     while (!UserExitCalled) { 
      Application.DoEvents(); // Process windows messages 
      Thread.Sleep(1); 
     } 

     return; 
    } 
} 

这里从我的一个系统托盘应用程序的完整程序类作为工作示例。

// ********************************************************************* 
// [DCOM Productions .NET] 
// [DPDN], [Visual Studio Launcher] 
// 
// THIS FILE IS PROVIDED "AS-IS" WITHOUT ANY WARRANTY OF ANY KIND. ANY 
// MODIFICATIONS TO THIS FILE IN ANY WAY ARE YOUR SOLE RESPONSIBILITY. 
// 
// [Copyright (C) DCOM Productions .NET All rights reserved.] 
// ********************************************************************* 

namespace VisualStudioLauncher 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Windows.Forms; 
    using System.Threading; 
    using VisualStudioLauncher.Common.Objects; 
    using VisualStudioLauncher.Forms; 
    using System.Drawing; 
    using VisualStudioLauncher.Common.Data; 
    using System.IO; 
static class Program 
{ 
    #region Properties 

    private static ProjectLocationList m_ProjectLocationList; 
    /// <summary> 
    /// Gets or Sets the ProjectsLocationList 
    /// </summary> 
    public static ProjectLocationList ProjectLocationList 
    { 
     get 
     { 
      return m_ProjectLocationList; 
     } 

     set 
     { 
      m_ProjectLocationList = value; 
     } 
    } 

    private static ShellProcessList m_ShellProcessList = null; 
    /// <summary> 
    /// Gets or Sets the ShellProcessList 
    /// </summary> 
    public static ShellProcessList ShellProcessList 
    { 
     get 
     { 
      return m_ShellProcessList; 
     } 

     set 
     { 
      m_ShellProcessList = value; 
     } 
    } 

    private static NotifyIcon m_TrayIcon; 
    /// <summary> 
    /// Gets the programs tray application. 
    /// </summary> 
    public static NotifyIcon TrayIcon 
    { 
     get 
     { 
      return m_TrayIcon; 
     } 
    } 

    private static bool m_UserExitCalled; 
    /// <summary> 
    /// Gets a value indicating whether the user has called for an Application.Exit 
    /// </summary> 
    public static bool UserExitCalled 
    { 
     get 
     { 
      return m_UserExitCalled; 
     } 

     set 
     { 
      m_UserExitCalled = value; 
     } 
    } 

    // TODO: Finish implementation, then use this for real. 
    private static ApplicationConfiguration m_ApplicationConfiguration = null; 
    /// <summary> 
    /// Gets the application configuration 
    /// </summary> 
    public static ApplicationConfiguration ApplicationConfiguration 
    { 
     get 
     { 
      if (m_ApplicationConfiguration == null) 
       m_ApplicationConfiguration = ApplicationConfiguration.LoadConfigSection(@"./settings.config"); 

      return m_ApplicationConfiguration; 
     } 
    } 


    #endregion 

    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main(string[] args) 
    { 
     if (args.Length > 0) 
     { 
      if (args[0].ToLower() == "-rmvptr") 
      { 
       for (int i = 1; i < args.Length; i++) { 
        try { 
         if (File.Exists(Application.StartupPath + @"\\" + args[i])) { 
          File.Delete(Application.StartupPath + @"\\" + args[i]); 
         } 
        } 
        catch { /* this isn't critical, just convenient */ } 
       } 
      } 
     } 

     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 

     SplashForm splashForm = new SplashForm(); 
     splashForm.Show(); 

     while (!UserExitCalled) 
     { 
      Application.DoEvents(); 
      Thread.Sleep(1); 
     } 

     if (m_TrayIcon != null) 
     { 
      m_TrayIcon.Icon = null; 
      m_TrayIcon.Visible = false; 
      m_TrayIcon.Dispose(); 

      GC.Collect(); 
     } 
    } 

    #region System Tray Management 

    public static void SetupTrayIcon() 
    { 
     m_TrayIcon = new NotifyIcon(); 
     m_TrayIcon.Text = Resources.UserInterfaceStrings.ApplicationName; 
     m_TrayIcon.Visible = false; // This will be set visible when the context menu is generated 
     m_TrayIcon.MouseDoubleClick += new MouseEventHandler(m_TrayIcon_MouseDoubleClick); 

     if (Orcas.IsInstalled) 
     { 
      m_TrayIcon.Icon = Orcas.Icon; 
     } 
     else if (Whidbey.IsInstalled) { 
      m_TrayIcon.Icon = Whidbey.Icon; 
     } 
     else { 
      m_TrayIcon.Icon = SystemIcons.Warning; 
      m_TrayIcon.Text = "Visual Studio is not installed. VSL cannot run properly."; 
     } 
    } 

    static void m_TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e) 
    { 
     if (e.Button != MouseButtons.Left) 
     { 
      return; 
     } 

     SettingsForm settingsForm = new SettingsForm(); 
     settingsForm.Show(); 
    } 

    #endregion 
} 

}

1

我所做的只是改变这一属性: Application.OpenForms["Form1"].Opacity = 0;