Share via


방법: 명령줄에서 Windows Forms 애플리케이션 만들기

다음 절차에서는 명령줄에서 Windows Forms 애플리케이션을 만들고 실행하기 위해 완료해야 하는 기본 단계를 설명합니다. Visual Studio에서는 이러한 절차가 광범위하게 지원됩니다. 연습: WPF에서 Windows Forms 컨트롤 호스트를 참조하세요.

프로시저

폼을 만들려면

  1. 빈 코드 파일에서 다음 Imports 또는 using 문을 입력합니다.

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Windows.Forms
    
  2. Form 클래스에서 상속되는 Form1이라는 클래스를 선언합니다.

    public class Form1 : Form
    
    Public Class Form1
        Inherits Form
    
  3. Form1에 대한 매개 변수 없는 생성자를 만듭니다.

    이후 절차에서 생성자에 더 많은 코드를 추가합니다.

    public Form1() {}
    
    Public Sub New()
    
    End Sub
    
  4. 클래스에 Main 메서드를 추가합니다.

    1. C# Main 메서드에 STAThreadAttribute를 적용하여 Windows Forms 애플리케이션을 단일 스레드 아파트로 지정합니다. (Visual Basic으로 개발된 Windows 양식 애플리케이션은 기본적으로 단일 스레드 아파트 모델을 사용하므로 Visual Basic에는 특성이 필요하지 않습니다.)

    2. EnableVisualStyles를 호출하여 애플리케이션에 운영 체제 스타일을 적용합니다.

    3. 폼 인스턴스를 만들고 실행합니다.

    [STAThread]
    public static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
    
    
        Public Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
    
        End Sub
    End Class
    

애플리케이션을 컴파일하고 실행하려면

  1. .NET Framework 명령 프롬프트에서 Form1 클래스를 만든 디렉터리로 이동합니다.

  2. 폼을 컴파일합니다.

    • C#을 사용하는 경우 csc form1.cs를 입력합니다.

      -or-

    • Visual Basic을 사용하는 경우 vbc form1.vb를 입력합니다.

  3. 명령 프롬프트에 다음을 입력합니다. Form1.exe

컨트롤 추가 및 이벤트 처리

이전 절차의 단계에서는 컴파일 및 실행되는 기본 Windows Form을 만드는 방법만 보여 주었습니다. 다음 절차에서는 컨트롤을 만들고 폼에 추가한 다음 컨트롤에 대한 이벤트를 처리하는 방법을 보여 줍니다. Windows Forms에 추가할 수 있는 컨트롤에 대한 자세한 내용은 Windows Forms 컨트롤을 참조하세요.

Windows Forms 애플리케이션을 만드는 방법을 이해하는 것은 물론 이벤트 기반 프로그래밍 및 사용자 입력을 처리하는 방법도 이해해야 합니다. 자세한 내용은 Windows Forms에서 이벤트 처리기 만들기사용자 입력 처리를 참조하세요.

단추 컨트롤을 선언하고 해당 click 이벤트를 처리하려면

  1. button1이라는 단추 컨트롤을 선언합니다.

  2. 생성자에서 단추를 만들고 해당 Size, LocationText 속성을 설정합니다.

  3. 폼에 단추를 추가합니다.

    다음 코드 예제에서는 단추 컨트롤을 선언하는 방법을 보여 줍니다.

    public Button button1;
    public Form1()
    {
        button1 = new Button();
        button1.Size = new Size(40, 40);
        button1.Location = new Point(30, 30);
        button1.Text = "Click me";
        this.Controls.Add(button1);
        button1.Click += new EventHandler(button1_Click);
    }
    
    Public WithEvents button1 As Button
    
    Public Sub New()
        button1 = New Button()
        button1.Size = New Size(40, 40)
        button1.Location = New Point(30, 30)
        button1.Text = "Click me"
        Me.Controls.Add(button1)
       
    End Sub
    
  4. 단추에 대한 Click 이벤트를 처리할 메서드를 만듭니다.

  5. click 이벤트 처리기에서 "Hello World" 메시지와 함께 MessageBox를 표시합니다.

    다음 코드 예제에서는 단추 컨트롤의 click 이벤트를 처리하는 방법을 보여 줍니다.

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Hello World");
    }
    
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
        MessageBox.Show("Hello World")
    End Sub
    
  6. 만든 메서드에 Click 이벤트를 연결합니다.

    다음 코드 예제에서는 이벤트를 메서드에 연결하는 방법을 보여 줍니다.

    button1.Click += new EventHandler(button1_Click);
    
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
    
  7. 이전 절차에서 설명한 대로 애플리케이션을 컴파일 및 실행합니다.

예제

다음 코드 예제는 이전 프로시저의 전체 예제입니다.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace FormWithButton
{
    public class Form1 : Form
    {
        public Button button1;
        public Form1()
        {
            button1 = new Button();
            button1.Size = new Size(40, 40);
            button1.Location = new Point(30, 30);
            button1.Text = "Click me";
            this.Controls.Add(button1);
            button1.Click += new EventHandler(button1_Click);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World");
        }
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }
}
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form
    Public WithEvents button1 As Button

    Public Sub New()
        button1 = New Button()
        button1.Size = New Size(40, 40)
        button1.Location = New Point(30, 30)
        button1.Text = "Click me"
        Me.Controls.Add(button1)
       
    End Sub
   
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
        MessageBox.Show("Hello World")
    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form1())

    End Sub
End Class

참고 항목