在发布时隐藏表单

时间:2009-02-06 20:59:50

标签: c# winforms forms hide

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

在frmMain_Load中,我尝试了两个

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

没有成功。

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

我在另一个问题中看到了马蒂亚斯提出的这个问题:

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

这很有效,但是当我启动程序时,我可以看到表单快速闪烁。它总比没有好,但我想知道是否有更好的解决方案。

感谢。

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:5)

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

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

调用Run的简单事实确实会使表单可见。尝试在表单的属性中执行以下操作:

  1. WindowState设为Minimized
  2. ShowInTaskbar设为false
  3. 这应该可以解决问题!

答案 2 :(得分:2)

不要在表单上调用Show或ShowDialog,您可以让Application.Run定位一个自定义类,然后实例化一个表单,不显示或创建NotifyIcon实例,并从那里处理所有内容。

答案 3 :(得分:1)

您也可以在form_shown事件中输入this.hide = true。我相信该事件仅在加载事件之后触发一次。如果您的表单有很多控件和/或计算机很慢,您可能会看到轻微的闪烁。

答案 4 :(得分: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
    }
}

答案 5 :(得分:1)

我只是更改了这个属性: Application.OpenForms["Form1"].Opacity = 0;