Share via


방법: 명령줄에서 Windows Forms 응용 프로그램 만들기

다음 절차에서는 Windows Forms 응용 프로그램을 만들고 실행하기 위해 명령줄에서 수행해야 하는 기본 단계를 설명합니다. Visual Studio에서는 이러한 절차를 폭넓게 지원합니다. 자세한 내용은 다음을 참조하십시오. 연습: 간단한 Windows Form 만들기연습: 간단한 Windows Form 만들기연습: 간단한 Windows Form 만들기연습: 간단한 Windows Form 만들기연습: 간단한 Windows Form 만들기.

절차

폼을 만들려면

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

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

    Public Class Form1
        Inherits Form
    
    public class Form1 : Form
    
  3. Form1의 기본 생성자를 만듭니다.

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

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

    1. STAThreadAttribute를 Main 메서드에 적용하여 Windows Forms 응용 프로그램이 단일 스레드 아파트임을 지정합니다.

    2. EnableVisualStyles를 호출하여 응용 프로그램에 Windows XP 모양을 제공합니다.

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

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

응용 프로그램을 컴파일하고 실행하려면

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

  2. 폼을 컴파일합니다.

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

      또는

    • Visual Basic을 사용하는 경우에는 vbc form1.vb /r:system.dll,system.drawing.dll,system.windows.forms.dll을 입력합니다.

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

컨트롤 추가 및 이벤트 처리

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

Windows Forms 응용 프로그램을 만드는 방법뿐만 아니라 이벤트 기반 프로그래밍과 사용자 입력 처리 방법에 대해서도 이해해야 합니다. 자세한 내용은 Windows Forms에서 이벤트 처리기 만들기사용자 입력 처리를 참조하십시오.

단추 컨트롤을 선언하고 클릭 이벤트를 처리하려면

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

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

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

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

    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
    
    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);
    }
    
  4. 단추에 대한 Click 이벤트를 처리하는 메서드를 만듭니다.

  5. 클릭 이벤트 처리기에서 "Hello World"라는 메시지가 담긴 MessageBox를 표시합니다.

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

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

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

    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles button1.Click
    
    button1.Click += new EventHandler(button1_Click);
    
  7. 이전 절차에서 설명한 대로 응용 프로그램을 컴파일하고 실행합니다.

예제

다음 코드 예제는 이전 절차의 예제를 포함하여 완성된 예제입니다.

Imports System
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
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());
        }
    }
}

코드 컴파일

  • 코드를 컴파일하려면 응용 프로그램을 컴파일하고 실행하는 방법을 설명한 이전 절차의 설명을 따릅니다.

참고 항목

참조

Form

Control

기타 리소스

Windows Forms의 모양 변경

Windows Forms 응용 프로그램 강화

Windows Forms 시작