windows programming ielearning.kocw.net/contents4/document/lec/2013/chungbuk/... · 2013-12-02 ·...

20
Windows Programming I 소프트웨어학과 HCI 프로그래밍 강좌

Upload: others

Post on 24-Feb-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Windows Programming I

소프트웨어학과 HCI 프로그래밍 강좌

Page 2: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Windows Programming

NET Windows 프로그래밍 환경

WinForm(Windows Form)

• 익히기 쉽고 높은 생산성 제공

• 코드 작성 방법

– Console application에서 C# 코드로 직접 작성

– Windows application에서 form designer 사용 작성

WPF(Windows Presentation Foundation)

• 세련된 UI와 화려한 효과를 제공

• WinForm보다 복잡

Page 3: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Windows Programming

Console application으로 개발하는 경우

Page 4: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Windows Programming

System.Windows.Forms 어셈블리 추가

솔루션 탐색기 -> 참조 -> 참조추가 ->.NET

Page 5: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Windows Programming

System.Windows.Forms.Form 상속

Page 6: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Windows Programming

using namespace을 사용한 코드 간소화

Page 7: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Application 클래스

Application 클래스의 역할

원도우 응용 프로그램을 시작시키고 종료시키는 메소드 제공

• Application.Run( ) : 위도우 시작

• Application.Exit( ) : 윈도우 종료

– 응용 프로그램이 소유한 모든 윈도우를 닫은 후 Run( ) 메소드가 반환되도록 함

– Application.Run( ) 다음에 자원 정리 등의 코드 삽입하면, 프로그램 종료시 정리작업 수행 가능

Page 8: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Application 클래스

Application 클래스의 역할

원도우 메시지(message) 처리

• Form 클래스는 여러가지 event (일종의 delegate)를 정의하여 제공

• Form을 상속받은 클래스의 event에 event handler 메소드 설정

class MyForm : System.Windows.Forms.Form { } class MainApp { static void Main(string[ ] args) { MyForm form = new MyForm( ); form.Click += new EventHandler( (sender, eventArgs) => { Application.Exit(); }); Application.Run(form); } }

Page 9: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

// delegate void System.EventHandler (object sender, EventArgs e);

Page 10: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

메시지 필터링

Application 클래스의 메시지 필터링(message filtering)

메시지 전달과정

• 사용자가 마우스나 키보드 같은 하드웨어를 제어하면 인터럽트가 발생

• 윈도우 운영체제는 인터럽트 받아, 이를 바탕으로 윈도우 메시지(Windows Message) 생성

• 메시지(이벤트)를 받아야 하는 응용 프로그램 전달

메시지 필터링

• Application 클래스는 응용 프로그램에게 전달된 메시지 중에서 관심 있는 것만 걸러 볼 수 있도록 함

이벤트 처리기 이벤트 발생

인터럽트 발생

메시지 발생 메시지 필터

Page 11: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

메시지 필터링

Application 클래스의 메시지 필터링

메시지 필터는 IMessageFilter 인터페이스의 PreFilterMessage 메소드를 구현하여 생성

Application.AddMessageFilter() 메소드를 통해 메시지 필터를 설치

public interface IMessageFilter { bool PreFilterMessage(ref Message m); }

class MessageFilter : IMessageFilter { public bool PreFilterMessage(ref Message m) { if (m.Msg == 0x0F || m.Msg == 0x200 || m.Msg == 0x113) return false; return true; } }

// PreFilterMessage가 false를 return 하면 message가 전달되지 않음

Application.AddMessageFilter(new MessageFilter());

Page 12: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

메시지 필터링

0x0F : WM_PAINT 0x200 : WM_MOUSEMOVE 0x113 : WM_TIMER 0x201 : WM_LBUTTONDOWN

Page 13: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

메시지 필터링

Message 구조체의 프로퍼티

HWnd : 메시지를 받는 윈도우의 핸들(handle).

• Handle: 윈도우의 인스턴스를 식별하고 관리하기 위해 운영체제가 부여한 번호

Msg : 메시지 ID

LParam : 메시지를 처리하는 데 필요한 정보 포함

WParam : 메시지 처리에 필요한 부가 정보 포함

Result : 메시지 처리에 대한 응답. 윈도우 운영체제에 반환되는 값 지정

Page 14: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Form 클래스

Form 클래스

응용프로그램에서 나타내는 윈도우를 생성 관리하는 클래스

System.Windows.Forms에 포함

운영체제에서 전달해주는 일부 메시지에 대해 event 구현

event에 event handler 메소드를 등록하여 해당 메시지를 처리

• 예. Form 인스턴스(즉, 윈도우) 위에서 마우스 좌 클릭

1. WM_LBUTTONDOWN 메시지가 Form 인스턴스에 전달

2. 등록된 event handler가 호출되어 실행

// event handler 선언 private void Form_MouseDown(object sender, MouseEventArgs e) { MessageBox.Show(“안녕하세요!”); } public MyForm() { //이벤트 처리기를 이벤트(MouseDown) 에 연결 this.MouseDown += new MouseEventHandler (this.Form_MouseDown); }

// public event MouseEventHandler MouseDown;

Page 15: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Form 클래스

Page 16: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Form 클래스

delegate void MouseEventHandler(object sender, MouseEventArgs e)

sender

이벤트(메시지)가 발생한 객체

MouseEventArgs 프로퍼티

Button : 마우스 어떤 버턴에서 발생했는지 (좌, 우, 가운데)

Clicks : 클릭 횟수

Delta : 휘의 회전 방향과 거리

X : 이벤트가 발생한 폼 또는 컨트롤 상의 x 좌표

Y : 이벤트가 발생한 폼 또는 컨트롤 상의 y 좌표

Page 17: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

Form 클래스

Form의 윈도우 모양 관련한 일부 프로퍼티

Width : 윈도우 너비

Height : 윈도우 높이

BackColor : 배경 색깔

BackgroundImage : 배경 이미지

Opacity : 투명도

MaximizeBox : 최대화 버튼 설치 여부

MinimizeBox : 최소화 버튼 설치 여부

Text : 윈도우 제목

Page 18: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기
Page 19: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기

윈도우 컨트롤

컨트롤(control) 윈도우 운영체제가 제공하는 사용자 인터페이스 요소

메뉴, 콤보박스, 리스트뷰, 버튼, 텍스트박스 등

컨트롤을 Form 위에 올리는 과정 컨트롤 인스턴스 생성

컨트롤 프로퍼티 값 설정

컨트롤의 이벤트에 이벤트 처리기 등록

Form에 컨트롤 추가

Button button = new Button();

button.Text = "Click Me!"; button.Left = 100; button.Top = 50;

button.Click += (object sender, EventArgs e) => { MessageBox.Show(“Ding Dong!"); };

MainApp form = new MainApp(); form.Controls.Add(button);

Page 20: Windows Programming Ielearning.kocw.net/contents4/document/lec/2013/Chungbuk/... · 2013-12-02 · Windows Programming NET Windows 프로그래밍 환경 WinForm(Windows Form) •익히기