C# 윈폼(Application 클래스)
C#.NET 프레임워크에서는 간단히 윈도우를 만들 수 있도록 Winform 클래스 라이브러리를 제공하며 다음과 같은 방법으로 윈도우를 생성한다.
System.Windows.Forms.Form 클래스에서 파생된 윈도우폼 클래스 선언
위에서 만든 클래스의 인스턴스를 System.Windows.Forms.Application.Run() 메소드에 매개변수로 넘겨 호출하면서 윈폼 응용 프로그램을 시작한다.
Application 클래스의 기능은 윈도우 응용프로그램을 시작하고 종료시키고, 윈도우 메시지를 처리하는 일을 한다.
응용프로그램의 시작은 Application.Run(), 응용프로그램의 종료는 Application.Exit()인데 Exit() 메소드가 호출되었다고 바로 종료되는 것은 아니다. 이 메소드가 하는 일은 응용프로그램의 모든 윈도우를 닫은 후 Run() 메소드가 Return되도록 하는 것이므로 Run() 메소드 뒷부분에 프로그램이 종료되면서 할 일이 있다면 해당 코드를 기술하면 된다.
[별도 이벤트처리 메소드를 만들고 델리게이트의 인자로]
System.Windows.Forms를 참조 추가해야 한다.
using System;
using System.Windows.Forms;
class Program : Form
{
static void Main(string[] args)
{
Program form = new Program();
form.Click += new EventHandler(form.Form_Click);
Console.WriteLine("윈도우 시작...");
Application.Run(form);
Console.WriteLine("윈도우 종료...");
}
void Form_Click(object sender, EventArgs e)
{
Console.WriteLine("폼클릭 이벤트...");
Application.Exit();
}
}
[람다식을 이용한 무명함수로 이벤트 처리]
using System;
using System.Windows.Forms;
namespace ConsoleApplication9
{
class Program : Form {
static void Main(string[] args) {
Program form = new Program();
form.Click += new EventHandler(
(sender, eventArgs) =>
{
Console.WriteLine("폼클릭 이벤트...");
Application.Exit();
});
Console.WriteLine("윈도우 시작...");
Application.Run(form);
Console.WriteLine("윈도우 종료...");
}
}
}
#윈폼, #시샵윈폼, #Application객체, #윈폼강좌, #윈폼강의
댓글 없음:
댓글 쓰기