Windows Form 프로젝트 구성
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FrameworkTest
{
static class Program
{
/// <summary>
/// 해당 애플리케이션의 주 진입점입니다.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
- using 문으로 필요한 *네임스페이스들에 액세스할 수 있게 한다.
- EnableVisualStyles()
: 어플리케이션에 대한 비주얼 스타일을 적용하는 메서드
: 반드시 컨트롤을 만들기 이전에 호출해야한다. 비주얼 스타일로 컨트롤을 그리기 때문이다.
: Form 클래스 객체를 생성하기 이전에 호출된다.
- SetCompatibleTextRenderingDefault(false)
: 어플리케이션의 텍스트 렌더링 방식을 결정한다.
: true일 경우 생성하는 컨트롤이 텍스트 렌더링에 GDI+기반의 Graphics 클래스 사용
: false일 경우 컨트롤이 GDI기반의 TextRenderer 클래스를 사용한다.
- Application.Run(new Form1())
: new Form1()으로 새로운 폼 객체를 하나 생성해 컨텍스트를 매개변수로 보내준다.
: 프로그램 실행 시 처음 화면에 표시하고 메세지 루프를 가동하겠다는 말이다.
* 네임스페이스
: 서로 연관된 클래스, 인터페이스, 구조체, 열거형, 델리게이트 하위 인터페이스 등
여러가지를 하나로 묶어주는 기능을 한다.
: 네임스페이스를 사용하면 서로다른 네임스페이스에서 같은 이름의 클래스가 존재하더라도
충돌이 발생하지 않는다.
예)
namespace 네임스페이스이름
{
네임스페이스에 포함될 항목
}
using 위에서 작성한 네임스페이스이름;
네임스페이스이름.메서드이름;
- Form1.Designer.cs
namespace FrameworkTest
{
partial class Form1
{
/// <summary>
/// 필수 디자이너 변수입니다.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 사용 중인 모든 리소스를 정리합니다.
/// </summary>
/// <param name="disposing">관리되는 리소스를 삭제해야 하면 true이고, 그렇지 않으면 false입니다.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form 디자이너에서 생성한 코드
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// button1
this.button1.Location = new System.Drawing.Point(485, 206);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
// Form1
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(907, 546);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
: 하나의 윈도우 폼을 생성하면 form.cs 와 form.designer.cs 가 생성된다.
: Form1.Designer.cs 는 프로그래머가 수정할 필요가 없다.
: 해당 Designer.cs 파일을 해석하여서 폼화면을 보여준다.
프로그래머가 건드릴 일이 없는 코드를 form.designer.cs에 몰아넣고 나머지 메세지 핸들링과 기타 코드들과 같이 프로그래머가 입력해 주어야하는 코드들을 form.cs에 넣어준다.
: InitializeComponent() 에서는 각 컨트롤과 윈도우들을 초기화 한다.
partial로 클래스나 메서드를 선언하면 선언과 구현을 분리할 수 있다.
컴파일 타임시 메서드가 선언만 되어있고 구현이 되어 있지 않다거나, 기능이 덜 구현되어 있는 일이 발생한다면
클래스 외부에서 사용할 시 문제가 발생한다.
때문에 private로 외부에서 호출하는것을 방지한다.
- Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FrameworkTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Click += new System.EventHandler(this.button1_Click);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World!");
}
}
}
: form.designer.cs 에서 정의한 InitializeComponent() 을 호출한다.