[펌][C샵][WINFORM] Application 클래스

seung-jae hwang·2019년 4월 1일
0

FROM : https://blog.naver.com/go4693/221317894501

Application 클래스

Application 클래스는 윈도우 응용 프로그램을 시작 / 종료 시키는 메서드를 제공하고
윈도우 메시지를 처리합니다.

응용 프로그램을 시작하는 메서드는 Application.Run()
응용 프로그램을 종료하는 메서드는 Application.Exit()

Application 클래스의 메시지 필터링

메시지 필터링은 원하는 이벤트를 걸러서 작동할 수 있게 합니다.
여기서 말하는 이벤트란 키보드, 마우스 클릭을 예로 들 수 있습니다.

윈도우에서 정의하는 메시지는 식별 번호가 있습니다.
메시지 WM_LBUTTONDOWN는 식별 번호는 0x201입니다.

메시지 필터 예제
using System;
using System.Windows.Forms;

namespace MessageFilter
{
class MessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
// WM_PAINT:0x0F WM_NCMOUSEMOVE:0xA0 WM_MOUSEFIRST:0x200 WM_TIMER:0x113
// 위의 메시지는 너무 많이 발생하기 때문에 제외함
if (m.Msg == 0x0F || m.Msg == 0xA0 || m.Msg == 0x200 || m.Msg == 0x113)
return false;
Console.WriteLine("{0} : {1}", m.ToString(), m.Msg);
// 마우스 왼 클릭하면 응용 프로그램 종료
if (m.Msg == 0x201)
Application.Exit();
return true;
}
}
class Program : Form
{
static void Main(string[] args)
{
Application.AddMessageFilter(new MessageFilter());
Application.Run(new Program());
}
}
}

0개의 댓글