==========================================================================================
API
==========================================================================================
<API>
=2-3-가. 배경색 바꾸기
WndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
![](https://t1.daumcdn.net/cfile/tistory/26028F4256483B9833)
=2-3-나. 커서 바꾸기
값 | 설명 |
IDC_ARROW | 화살표 모양 |
IDC_CROSS | 십자 모양 |
IDC_IBEAM | I자 모양 |
IDC_NO | 원안에 빗금이 쳐진 모양 |
IDC-WAIT | 모래시계 모양 |
=2-3-다. 윈도우의 타이틀 바꾸기
hWnd=CreateWindow(lpszClass, L"Change Title",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,(HMENU)NULL,hInstance,NULL); |
![](https://t1.daumcdn.net/cfile/tistory/2418974256483B9A1F)
2-3-라. 윈도우의 크기 바꾸기
hWnd=CreateWindow(lpszClass,"My First Program",WS_OVERLAPPEDWINDOW, 100,100,300,100, NULL,(HMENU)NULL,hInstance,NULL); |
![](https://t1.daumcdn.net/cfile/tistory/2437D64256483B9D02)
2-3-마. 윈도우 스타일
*WS_CAPTION, WS_SYSMENU => 기본으로 넣어줌.
WS_OVERLAPPEDWINDOW : 평범한 형태의 윈도우
값 | 설명 |
WS_CAPTION | 타이틀 바를 가진다. |
WS_SYSMENU | 시스템 메뉴를 가진다. |
WS_HSCROLL | 수평 스크롤 바를 가진다. |
WS_VSCROLL | 수직 스크롤 바를 가진다. |
WS_MAXIMIZEBOX | 최대화 버튼을 가진다. |
WS_MINIMIZEBOX | 최소화 버튼을 가진다. |
WS_THICKFRAME | 크기를 조절할 수 있는 경계선을 가진다. |
3-1-가. DC의 필요성
1. 많은 양의 정보를 모두 모아 사용
2. 멀티 태스킹 시스템 => 화면에서 그려야 할 부분과 아닌 부분 구별 필요
3-1-나. 문자열 출력
3-2-가. TextOut
값 | 설명 |
TA_TOP | 지정한 좌표가 상단좌표가 된다. |
TA_BOTTOM | 지정한 좌표가 하단 좌표가 된다. |
TA_CENTER | 지정한 좌표가 수평 중앙 좌표가 된다. |
TA_LEFT | 지정한 좌표가 수평 왼쪽 좌표가 된다. |
TA_RIGHT | 지정한 좌표가 수평 오른쪽 좌표가 된다. |
TA_UPDATECP | 지정한 좌표대신 CP를 사용하며 문자열 출력후에 CP를 변경한다. |
TA_NOUPDATACP | CP를 사용하지 않고 지정한 좌표를 사용하며 CP를 변경하지 않는다. |
3-1-다. WM_PAINT 메시지
HDC hdc; PAINTSTRUCT ps; switch (iMessage) { // 메시지 번호가 인자
case WM_DESTROY: // WM : Window Message PostQuitMessage(0); return 0; case WM_LBUTTONDOWN: hdc = GetDC(hWnd); SetTextAlign(hdc, TA_LEFT); TextOut(hdc, 10, 10, L"Beautiful Korea", 15); ReleaseDC(hWnd, hdc); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); SetTextAlign(hdc, TA_CENTER); TextOut(hdc, 100, 60, L"Beautiful Korea", 15);
return 0; } |
![](https://t1.daumcdn.net/cfile/tistory/2537DD4256483B9E02)
=>LBUTTON CLICK
![](https://t1.daumcdn.net/cfile/tistory/27121E4256483BA025)
3-1-라. DC를 얻는 방법
HDC GetDC(HWND hWnd); int ReleaseDC(HWND hWnd,HDC hDC);
case WM_PAINT: HDC BeginPaint(HWND hwnd, LPPAINTSTRUCT lpPaint); BOOL EndPaint(HWND hWnd,CONST PAINTSTRUCT *lpPaint);
typedef struct tagPAINTSTRUCT { HDC hdc; BOOL fErase; RECT rcPaint; BOOL fRestore; BOOL fIncUpdate; BYTE rgbReserved[16]; } PAINTSTRUCT; |
앞의 세 멤버는 사용자가 사용
나머지는 윈도우즈 내부에서 사용
=3-2-나. DrawText
typedef struct _RECT { // rc LONG left; LONG top; LONG right; LONG bottom; } RECT; |
값 | 설명 |
DT_LEFT | 수평 왼쪽 정렬한다. |
DT_RIGHT | 수평 오른쪽 정렬한다. |
DT_CENTER | 수평 중앙 정렬한다. |
DT_BOTTOM | 사각 영역의 바닥에 문자열을 출력한다. |
DT_VCENTER | 사각 영역의 수직 중앙에 문자열을 출력한다. |
DT_WORDBREAK | 사각영역의 오른쪽 끝에서 자동 개행되도록 한다. |
DT_SINGLELINE | 한줄로 출력한다. |
DT_NOCLIP | 사각 영역의 경계를 벗어나도 문자열을 자르지 않고 그대로 출력한다. |
HDC hdc; PAINTSTRUCT ps; RECT rt = { 100, 100, 400, 300 }; char *tstr= { TEXT("님은 갔습니다. 아아 사랑하는 나의 님은 갔습니다. 푸른 산빛을 ") TEXT("깨치고 단풍나무 숲을 향하여 난 작은 길을 걸어서 차마 떨치고 갔습니다. ") TEXT("황금의 꽃같이 굳고 빛나던 옛맹세는 차디찬 티끌이 되어 한숨의 미풍에") TEXT("날아갔습니다.") }; switch (iMessage) { case WM_DESTROY: // WM : Window Message PostQuitMessage(0); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); DrawText(hdc, tstr, -1, &rt, DT_CENTER | DT_WORDBREAK); EndPaint(hWnd, &ps); return 0; } |
![](https://t1.daumcdn.net/cfile/tistory/2129234256483BA210)
3-3-가. 그래픽 출력
HDC hdc; PAINTSTRUCT ps; RECT rt = { 100, 100, 400, 300 }; int i; int ie; int j; int k; int rgb;
switch (iMessage) { case WM_DESTROY: // WM : Window Message PostQuitMessage(0); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); j = i = 20; rgb = 0xFF0000; SetPixel(hdc, i, j, rgb); for (k = 0; k < 4; ++k) { switch (k) { case 0: i = 20; rgb = 0xFF0000; break; case 1: i = 30; rgb = 0x00FF00; break; case 2: i = 40; rgb = 0x0000FF; break; case 3: i = 50; rgb = 0x000000; break; } ie = i + 2; for (i = i; i < ie; ++i) { for (j = 20; j < 22; ++j) { SetPixel(hdc, i, j, rgb); } } }
MoveToEx(hdc, 30, 30, NULL); LineTo(hdc, 50, 50); Rectangle(hdc, 100, 100, 150, 160); Ellipse(hdc, 100, 150, 150, 210);
EndPaint(hWnd, &ps); return 0; } |
![](https://t1.daumcdn.net/cfile/tistory/2131C64256483BA407)
=도스 콘솔
#include <windows.h>
#pragma comment(lib, "user32.lib") int main() { int iRet; int i; iRet = MessageBox(NULL, "123","345",MB_OK|MB_ICONWARNING); printf("MB_OK : %d\n" ,iRet); for(i = 0; i<3; ++i) { iRet = MessageBox(NULL, "123","345",MB_ABORTRETRYIGNORE|MB_ICONINFORMATION); printf("MB_ABORTRETRYIGNORE : %d\n" ,iRet); } for(i=0; i<2;++i) { iRet = MessageBox(NULL, "123","345",MB_OKCANCEL|MB_ICONSTOP); printf("MB_OKCANCEL : %d\n" ,iRet); } for(i=0; i<2;++i) { iRet = MessageBox(NULL, "123","345",MB_YESNO|MB_ICONQUESTION); printf("MB_YESNO : %d\n" ,iRet); } for(i=0; i<2;++i) { iRet = MessageBox(NULL, "123","345",MB_RETRYCANCEL); printf("MB_RETRYCANCEL : %d\n" ,iRet); } for(i=0; i<3;++i) { iRet = MessageBox(NULL, "123","345",MB_YESNOCANCEL); printf("MB_YESNOCANCEL : %d\n" ,iRet); } return 0; } |
![](https://t1.daumcdn.net/cfile/tistory/2544514E56483E8111)
![](https://t1.daumcdn.net/cfile/tistory/2434844E56483E7F1A)
![](https://t1.daumcdn.net/cfile/tistory/250C1A4356483BA60E)
![](https://t1.daumcdn.net/cfile/tistory/25189E4356483BA803)
![](https://t1.daumcdn.net/cfile/tistory/25744E4356483BA923)
![](https://t1.daumcdn.net/cfile/tistory/21720B4356483BAB26)
=WIN32API
MessageBox(hWnd, L"마우스 왼쪽 버튼을 눌렀습니다", L"메시지 박스", MB_OK); |
![](https://t1.daumcdn.net/cfile/tistory/27083C4356483BAD11)
=
HDC hdc; PAINTSTRUCT ps; int len; //static char str[256]; static WCHAR str[256]; //UNICODE로 완전히 넘어가서 문제생김
switch (iMessage) { case WM_CHAR: //len = strlen(str); len = lstrlen(str); // WCHAR str[len] = (WCHAR)wParam; str[len + 1] = 0; InvalidateRect(hWnd, NULL, FALSE); // window에게 한번더 그리라는 메시지를 요청 => WM_PAINT메시지 생성 => 글자 그려줌 return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); TextOut(hdc, 100, 100, str, lstrlen(str)); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } |
![](https://t1.daumcdn.net/cfile/tistory/2108334356483BB012)
![](https://t1.daumcdn.net/cfile/tistory/2303064356483BB217)
=키보드
오른쪽 Alt => 한/영
4-1-나. 무효화 영역 => 중요
InvalidateRect
![](https://t1.daumcdn.net/cfile/tistory/227F004456483BB31C)
*마우스 커서는 WM_PAINT메시지 발생되지는 않는다.
창 잡고 흔들면 아래의 다른 창들은 WM_PAINT를 받느라 바빠짐 => CPU사용률 올라감( => 요즘은 다른 창들이 내려감 )
HDC hdc; PAINTSTRUCT ps; int len; //static char str[256]; static WCHAR str[256]; //UNICODE로 완전히 넘어가서 문제생김
switch (iMessage) { case WM_CHAR: if ((WCHAR)wParam == 32) { str[0] = 0; } else { len = lstrlen(str); str[len] = (WCHAR)wParam; str[len + 1] = 0; } InvalidateRect(hWnd, NULL, TRUE); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); TextOut(hdc, 100, 100, str, lstrlen(str)); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } |
![](https://t1.daumcdn.net/cfile/tistory/277A144456483BB520)
![](https://t1.daumcdn.net/cfile/tistory/2765B44456483BB737)
![](https://t1.daumcdn.net/cfile/tistory/2706664456483BBA17)
InvalidateRect(hWnd, NULL, FALSE); |
4-1-다. WM_KEYDOWN
= 문자 이외 키보드 입력
HDC hdc; PAINTSTRUCT ps; static int x = 100; static int y = 100;
switch (iMessage) { case WM_KEYDOWN: switch (wParam) { case VK_LEFT: x = x - 8; break; case VK_RIGHT: x = x + 8; break; case VK_UP: y = y - 8; break; case VK_DOWN: y = y + 8; break; } InvalidateRect(hWnd, NULL, TRUE); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); TextOut(hdc, x, y, L"A", 1); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } |
![](https://t1.daumcdn.net/cfile/tistory/23107D4456483BBC0D)
InvalidateRect(hWnd, NULL, FALSE); |
![](https://t1.daumcdn.net/cfile/tistory/2505074456483BBE17)