次の方法で共有


方法 : スタートアップ Windows フォームを非表示にする

アプリケーションの起動時に Windows ベースのアプリケーションのメイン フォームを非表示にするには、アプリケーションのスタートアップ ロジックを別のクラスに移動する必要があります。 単純に Visible プロパティを false に設定することはできません。

アプリケーションの有効期間をフォームの有効期間から分離した後は、フォームの表示と非表示を切り替えることができます。アプリケーションの起動に使用したクラスを "閉じた" ときにアプリケーションが終了するためです。

注意

コードの実行中にモジュールが非表示であるため、次に示すプロシージャには、スタートアップ モジュールにメッセージ ボックスを追加する手順が含まれています。これは単に、アプリケーションが実行中であることを示しています。

フォームが開始時に非表示になるように設定するには

  1. 次のいずれかを実行します。

    1. Visual Basic の場合は、プロジェクトを右クリックして [モジュールの追加] をクリックすることにより、Windows ベースのアプリケーションにモジュールを追加します。

    2. Visual C# で、新しいクラスを作成します。

    3. Visual C++ の場合は、Windows ベースのアプリケーションの Form1.cpp を開きます。

      Windows ベースのアプリケーションの作成の詳細については、「方法: 新しい Windows フォーム アプリケーション プロジェクトを作成する」を参照してください。

  2. モジュール (またはクラス) 内で、プロジェクトのスタートアップ オブジェクトとして機能する Main サブルーチンを作成します。

    そのためのコード例を次に示します。

    Sub Main()
       ' Instantiate a new instance of Form1.
       Dim f1 as New Form1()
       ' Display a messagebox. This shows the application is running, 
       ' yet there is nothing shown to the user. This is the point at 
       ' which you customize your form.
       System.Windows.Forms.MessageBox.Show( _
          "The application is running now, but no forms have been shown.")
       ' Customize the form.
       f1.Text = "Running Form"
       ' Show the instance of the form modally.
       f1.ShowDialog()
    End Sub
    
    // All methods must be contained in a class.
    // This class is added to the namespace containing the Form1 class.
    class MainApplication
    {
       public static void Main()
       {
          // Instantiate a new instance of Form1.
          Form1 f1 = new Form1();
          // Display a messagebox. This shows the application 
          // is running, yet there is nothing shown to the user. 
          // This is the point at which you customize your form.
          System.Windows.Forms.MessageBox.Show("The application "
             + "is running now, but no forms have been shown.");
          // Customize the form.
          f1.Text = "Running Form";
          // Show the instance of the form modally.
          f1.ShowDialog();
       }
    }
    
    // You can use this Mian method in the Program class (Program.jsl):
    public static void main(String[] args)
    {
       // Instantiate a new instance of Form1.
       Form1 f1 = new Form1();
       // Display a messagebox. This shows the application 
       // is running, yet there is nothing shown to the user. 
       // This is the point at which you customize your form.
       System.Windows.Forms.MessageBox.Show("The application "
          + "is running now, but no forms have been shown.");
       // Customize the form.
       f1.set_Text("Running Form");
       // Show the instance of the form modally.
       f1.ShowDialog();
    }
    
    void Main()
    {
       // Instantiate a new instance of Form1.
       Form1^ f1 = gcnew Form1();
       // Display a messagebox. This shows the application 
       // is running, yet there is nothing shown to the user. 
       // This is the point at which you customize your form.
       System::Windows::Forms::MessageBox::Show(
          "The application is running now, "
          "but no forms have been shown.");
       // Customize the form.
       f1->Text = "Running Form";
       // Show the instance of the form modally.
       f1->ShowDialog();
    }
    

    注意

    上記のコード例内のメッセージ ボックスは、完全修飾名前空間で指定します。これは、作成したモジュールが、標準の Windows フォームとは異なり、既定で名前空間をインポートしないためです。 名前空間のインポートの詳細については、「参照と Imports ステートメント」(Visual Basic の場合)、「using ディレクティブ (C# リファレンス)」(Visual C# の場合)、または「using Directive (C++)」(Visual C++ の場合) を参照してください。 Application.Run() は、メッセージ ポンプを起動します。これは特定のアプリケーションの動作には不可欠であり、シャットダウン時など、アプリケーションのライフサイクルの特定期間におけるフォームの動作に影響を与えます。 詳細については、「Run メソッド」を参照してください。

  3. プロジェクトのスタートアップ オブジェクトを Form1 から Sub Main に変更します。

    Visual C# については、スタートアップ オブジェクトを前のコード例のクラス名に設定します。 詳細については、「方法 : Windows アプリケーションでスタートアップ フォームを選択する」および「方法 : アプリケーションのスタートアップ オブジェクトを変更する (Visual Basic)」を参照してください。

    注意

    Visual C++ の Windows ベース アプリケーションについてはこの手順を省略してください。

  4. F5 キーを押してプロジェクトを実行します。

    アプリケーションが実行されると、 Main() 内のコードが最初に実行され、Form1 のインスタンスはそれを表示するコードが実行されるまで非表示の状態で存在します。 これにより、ユーザーに知られることなくバックグラウンドで、Form1 のインスタンス内で自由な処理を行うことができます。

参照

処理手順

方法 : Windows フォームをモーダルおよびモードレスで表示する

方法 : Windows フォームの画面上の位置を設定する

その他の技術情報

Windows フォームの表示形式の変更