10

意図したとおりに動作するために NotifyIcon のみが必要なプログラムがあります。そのため、プログラムの開始時にメインフォームを非表示にしようとしています。

frmMain_Load では、両方を試しました

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

成功せずに。

NotifyIcon_MouseClick メソッドのように、他のメソッドでも機能しますが、Load 時に非表示にしたいのです。

ここSOの別の質問で、マティアスがこれを提案したところを見ました:

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

これは機能しますが、プログラムを起動すると、フォームが非常に速く点滅するのがわかります。何もないよりはましですが、これに対するより良い解決策があるのではないかと思います。

ありがとう。

4

5 に答える 5

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
于 2009-02-06T21:27:55.250 に答える
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

これでうまくいくはずです!

于 2009-02-06T21:16:37.410 に答える
1

form_shown イベントに this.hide = true を入れることもできます。イベントは load イベントの後に一度だけ発生すると思います。ただし、フォームに多数のコントロールが含まれている場合やコンピューターの処理速度が遅い場合は、ちらつきが発生することがあります。

于 2009-02-06T21:19:56.437 に答える
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;
    }
}

ここでは、実際の例として、私のシステム トレイ アプリケーションの 1 つからの完全なプログラム クラスを示します。

// *********************************************************************
// [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
    }
}
于 2009-02-06T21:21:27.243 に答える
1

フォームで Show または ShowDialog を呼び出さないでください。Application.Run のターゲットをカスタム クラスにして、フォームをインスタンス化し、NotifyIcon インスタンスを表示または作成せず、そこからすべてを処理することができます。

于 2009-02-06T21:02:20.513 に答える