Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 ·...

22
1 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY KOREA UNIVERSITY http://idb.korea.ac.kr DB & Mining LAB. Korea Univ. Windows Programming 1 Win32 API 최종 업데이트: 2012. 08. 14

Transcript of Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 ·...

Page 1: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

1 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

http://idb.korea.ac.kr

DB & Mining LAB.

Korea Univ.

Windows Programming 1

Win32 API

최종 업데이트: 2012. 08. 14

Page 2: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

2 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

2

컴퓨터 시스템의 주요 구성 요소

Page 3: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

3 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

3

컴퓨터 하드웨어 구성

Fetch : 메인 메모리에 저장되어 있는 명령어를 CPU로

Decode : 컨트롤 유닛에 의해 분석

Execution : ALU에 의해 연산

레지스터 : 실행의 중간 결과나 적은 양의 자료를 임시로 저장

하는 장치

CPU 내부의 레지스터 : 프로그램 카운터(Program Counter),

명령어 레지스터(Instruction Register), 기억장치 주소 레지스

터(Memory Address Register), 기억장치 버퍼 레지스터

(Memory buffer Register), 입출력 주소 레지스터(I/O

Address Register), 입출력 버퍼 레지스터(I/O Buffer Register)

버스 : 컴퓨터에서 두 개 혹은 그 이상의 장치들을 연결하는 공

유 전송 매체

- 주소 버스 : 데이터가 읽혀지거나 쓰여질 기억 장소의 주소를

전송

- 데이터 버스 : 모듈들 사이의 데이터 전송 통로

- 제어 버스 : 데이터 버스와 주소 버스의 사용을 제어하는 신호

들을 전송.

Ex) 기억장치 읽기/쓰기, I/O 읽기/쓰기, 신호 전송 확인

(transfer acknowledge), 버스 요구(bus request), 버스 승인

(bus grant) 인터럽트 요구, 확인, 등등

Page 4: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

4 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

4

프로세스란?

운영체제 : 멀티 프로세스(Multi-Process)

프로세스란 것이 여러 개 존재

인터넷 익스플로러 아이콘을 클릭

실행파일 iexplorer.exe 라는 프로그램 실행된 것

익스플로러 창은 프로세스이다.

아이콘을 다시 클릭하면 또 다른 익스플로러 창이 뜨고, 2번째 프로세스 생성됨

프로세스 스케줄링

Page 5: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

5 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY 윈도우 프로그래밍

API(Application Programming Interface)

Windows System Call

SDK(Software Development Kit )

윈도우 프로그램의 작성에 사용할 수 있는 C 함수 라이브러리

모든 윈도우 프로그래밍 환경에서 사용 가능

MFC(Microsoft Foundation Class)

Visual C++ Class Library

RAD(Rapid Application Development)

Visual Basic, PowerBuilder, Delphi

실행속도 느리고, 실행파일 큼

Page 6: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

6 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

메시지(Message)

메시지 큐(Message Queue)

메시지 루프(Message Loop)

윈도우 프로시저(Window Procedure)

윈도우 프로그래밍 동작 방식

Page 7: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

7 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

윈도우에 발생하는 모든 사건

WM_CREATE, WM_DESTROY, WM_MOUSEMOVE, WM_KEYDOWN ….

winuser.h에 정의

각 윈도우에 어떤 사건이 발생하는지를 감시하여 이를 메시지로 만들어내는 것은 기본적으로 운영체제의 책임이다

메시지가 발생하면 메시지는 해당 윈도우가 속한 프로그램의 메시지 큐로 들어간다

메시지

Page 8: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

8 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

하나의 프로그램에 하나의 메시지 큐 할당

윈도우에 사건이 발생하면

운영 체제는 이를 메시지로

만들어

발생 순서대로 메시지 큐에 넣는다

메시지 큐

Page 9: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

9 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

메시지 큐에 있는 메시지를 읽어서 윈도우 프로시저로 전송하는 루프

MSG msg;

while(GetMessage(&msg,NULL,0,0))

{

TransLateMessage(&msg); // 키보드 메시지 변환

DispatchMessage(&msg); //윈도우 프로시져로 메시지 전달

}

메시지 루프

Page 10: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

10 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

메시지에 응답하여 처리하는 함수

LONG APIENTRY WinProc // 윈도우 프로시저 이름

(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)

{

case 메시지1 : .....

break;

case 메시지2 : .....

break;

......

default :

return DefWindowProc(hWnd, message, wParam, lParam);

}

return 0;

}

윈도우 프로시져

Page 11: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

11 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

운영체제 응용 프로그램

while(GetMessage(…))

{

….메시지를 큐에서 읽음

DispatchMessage(…)

...메시지를 처리할 프로시저에 전달

}

switch(msg) switch(msg)

{ {

… ….

} }

메시지

윈도우 프로그램 동작원리

Page 12: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

12 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

기존의 타입들을 typedef문으로 재정의

BOOL: Boolean value.

LPSTR: 32-bit pointer to a character string.

UINT: 16-bit unsigned integer on Windows versions 3.0 and 3.1; a 32-

bit unsigned integer on Win32.

WORD: 16-bit unsigned integer.

DWORD: 32-bit unsigned integer or the address of a segment and its

associated offset.

LONG: 32-bit signed integer.

WPARAM: value passed as a parameter to a window procedure or

callback function: 16 bits on Windows versions 3.0 and 3.1; 32 bits on

Win32.

LPARAM: 32-bit value passed as a parameter to a window procedure

or callback function.

HANDLE: Handle of an object, UINT.

HWND: Handle of a window., UINT.

HINSTANCE: Handle of an instance, UINT

데이터 타입

Page 13: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

13 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

디바이스에 비의존적(device-independent)

출력하고자 하는 장치의 종류에 따라 출력 방법이 달라지지 않음

GDI(Graphic Device Interface)

어떤 디바이스에든지 출력하는데 사용되는 함수들

윈도우에서의 출력 방법

Page 14: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

14 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

응용 프로그램

G D I

디바이스 드라이브

실제 출력 장치

Win32 API

윈도우 운영체제에서의 출력

윈도우에서의 출력 방법

Page 15: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

15 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

윈도우 운영체제에서 출력 대상을 지칭

논리적으로 볼 때 붓과 펜, 팔레트, 종이(여기서는 비트맵에 해당), 폰

트 등의 그림을 그리기 위한 도구의 집합

윈도우 화면 출력절차

화면에 대한 디바이스 컨텍스트 핸들을 구함

BeginPaint(), GetDC() 함수

GDI를 이용해 원하는 출력

모든 출력장치에 동일

디바이스 컨텍스트 제거

EndPaint()(BeginPaint()로 디바이스 컨텍스트를 얻은 경우)

ReleaseDC()(GetDC() 로 디바이스 컨텍스트를 얻은 경우)

디바이스 컨텍스트

Page 16: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

16 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

출력할 장치의 디바이스 컨텍스트 핸들을 얻는다.

HDC hDC = GetDC(hWnd);

GDI로 앞에서 얻은 핸들에 출력을 행한다.

TextOut(hDC,0,0,”Hello”,5);

LineTo(hDC,100,100);

:

디바이스 컨텍스트를 제거한다.

ReLeaseDC(hWnd,hDC);

윈도우에서의 출력 절차

Page 17: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

17 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

예제 - Hello, everybody

case WM_PAINT:

HDC hDC;

PAINTSTRUCT ps;

hDC = BeginPaint(hWnd, &ps);

TextOut(hDC, 10, 10, "Hello, everybody", 16);

EndPaint(hWnd, &ps);

break;

WM_PAINT 메시지 처리

// 비교: C언어였다면, printf(“Hello, everybody”);

Page 18: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

18 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

HDC BeginPaint( HWND hwnd, LPPAINTSTRUCT lpPaint );

PAINTSTRUCT structure

typedef struct tagPAINTSTRUCT { // ps

HDC hdc; // Identifies the display DC to be used for painting.

BOOL fErase; // Specifies whether the background must be erased.

RECT rcPaint; // specifies the upper left and lower right corners of the

rectangle in which the painting is requested.

BOOL fRestore; // Reserved; used internally by Windows.

BOOL fIncUpdate; // Reserved; used internally by Windows.

BYTE rgbReserved[32]; // Reserved; used internally by Windows.

} PAINTSTRUCT;

BeginPaint()

Page 19: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

19 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

BOOL TextOut(

HDC hdc, // handle of device context

int nXStart, // x-coordinate of starting position

int nYStart, // y-coordinate of starting position

LPCTSTR lpString, // address of string

int cbString // number of characters in string

);

BOOL EndPaint(

HWND hWnd, // handle to window

CONST PAINTSTRUCT *lpPaint

// pointer to structure for paint data

);

TextOut(), EndPaint()

Page 20: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

20 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

어떤 디바이스에든지 출력하는데 사용되는 함수들

첫 번째 인자는 디바이스 컨텍스트에 대한 핸들

GDI의 출력 영역은 어디까지나 사용자 영역 안

디바이스 컨텍스트의 속성 관련 함수

도형 관련 출력 함수

텍스트 관련 출력 함수

맵핑 모드 관련 함수

그림 도구 변경 함수

GDI(Graphic Device Interface)

Page 21: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

21 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

윈도우 운영체제에서 화면복구시 사용

1

2

1

2

1

2

1번 윈도우에 의해 가

려졌던 2번 윈도우가

클릭된다.

가려졌던 영역이 일단

WM_ERASEBKGND

메시지에 의해 지워진

다.

WM_PAINT 메시지에

의해 가려졌던 영역이

복구된다.

윈도우에서의 화면 복구

WM_PAINT 메시지

Page 22: Windows Programming 1 - KOCWcontents.kocw.net/KOCW/document/2015/korea_sejong/... · 2016-09-09 · Windows Programming 1 최종 업데이트: 2012. 08. 14 . ... 윈도우 프로그램의

22 COMPUTER & INFORMATION SCIENCE, KOREA UNIVERSITY

KOREA

UNIVERSITY

WM_PAINT 메시지에서 화면 복구

디바이스 컨텍스트는 BeginPaint 함수 사용하여 구함

그 이외에는 GetDC 사용

BeginPaint 함수의 두 번째 인자에 복구되어야 할 영역을 지정

case WM_PAINT:

HDC hDC;

PAINTSTRUCT ps;

hDC = BeginPaint(hWnd, &ps);

// ps.rcPaint에 복구 영역의 좌표가 들어있다.

// 이 부분만 딱 그려줄 수 있다면 가장 이상적이다.

EndPaint(hWnd, &ps);

WM_PAINT 메시지