다음을 통해 공유


방법: 시작 Windows Form 숨기기

업데이트: 2007년 11월

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 Form과 달리 기본적으로 어떤 네임스페이스도 가져오지 않기 때문입니다. 네임스페이스를 가져오는 방법은 참조 및 Imports 문(Visual Basic), using 지시문(C# 참조)(Visual C#) 또는 using Directive (C++)(Visual C++)을 참조하십시오. Application.Run()에서는 메시지 펌프를 시작합니다. 메시지 펌프는 일부 응용 프로그램의 동작에 매우 중요하며 응용 프로그램 수명 중에서 종료와 같은 특정 시간 동안 폼 동작에 영향을 미칠 수 있습니다. 자세한 내용은 Application.Run 메서드를 참조하십시오.

  3. 프로젝트의 시작 개체를 Form1 대신 Sub Main으로 변경합니다.

    Visual C#의 경우 이전 코드 예제의 클래스 이름에 시작 개체를 설정합니다. 자세한 내용은 방법: Windows 응용 프로그램의 시작 폼 선택방법: 응용 프로그램의 시작 개체 변경(Visual Basic)을 참조하십시오.

    참고:

    Visual C++의 경우 Windows 기반 응용 프로그램에 대해 이 단계를 건너뜁니다.

  4. F5 키를 눌러 프로젝트를 실행합니다.

    응용 프로그램이 실행되면 Form1의 인스턴스가 잠시 나타나지 않는 동안, 인스턴스를 보여 주는 코드가 실행될 때까지, 먼저 Main() 내의 코드가 실행됩니다. 이러한 방법으로 사용자가 모르게 Form1의 인스턴스에서 백그라운드로 원하는 작업을 수행할 수 있습니다.

참고 항목

작업

방법: 모달 및 모덜리스 Windows Forms 표시

방법: Windows Forms의 화면 위치 설정

기타 리소스

Windows Forms의 모양 변경