WinAPI
WinAPI 기본 구조
GGym_
2020. 5. 7. 10:11
#include<windows.h>
#define START_X 0
#define START_Y 0
#define WINSIZE_X 800
#define WINSIZE_Y 600
HINSTANCE g_hInst; // 인스턴스의 핸들 선언
LPCTSTR g_ClassName = TEXT("PARK"); // 클래스 이름
LPCTSTR g_WinName = TEXT("JUNE"); // 윈도우 이름
HWND g_hWnd; // 윈도우의 핸들
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPRevInstance, LPSTR lpszCmdParam, int nCmdShow) {
MSG Message;
WNDCLASS WndClass;
g_hInst = hInstance;
int nWidth, nHeight;
// 프레임의 사이즈를 더해줌.(상하좌우+캡션)
nWidth = WINSIZE_X + GetSystemMetrics(SM_CXFRAME) * 2;
nHeight = WINSIZE_Y + GetSystemMetrics(SM_CXFRAME) * 2 + GetSystemMetrics(SM_CYCAPTION);
// 1 윈도우 클래스 초기화
WndClass.cbClsExtra = 0; // 여분 메모리
WndClass.cbWndExtra = 0; // 여분 메모리
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // 배경 색상
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); // 커서설정
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // 아이콘 읽기
WndClass.hInstance = hInstance; // 윈도우 클래스 인스턴스
WndClass.lpfnWndProc = (WNDPROC)WndProc; // 윈도우 프로시저 이름
WndClass.lpszClassName = g_ClassName; // 윈도우 클래스 이름
WndClass.lpszMenuName = NULL; // 메뉴의 이름
WndClass.style = CS_HREDRAW | CS_VREDRAW; // 윈도우의 스타일
// 2 클래스 등록
RegisterClass(&WndClass);
// 3 윈도우 생성
g_hWnd = CreateWindow(
g_ClassName, // 윈도우 클래스 문자열
g_WinName, // 타이틀 바에 나타날 문자열
WS_OVERLAPPEDWINDOW, // 윈도우의 형태 옵션 (OR 연산자 사용)
START_X, START_Y, // 윈도우 시작위치
nWidth, nHeight, // 윈도우 크기
NULL, // 부모 윈도우의 핸들
(HMENU)NULL, // 메뉴의 핸들
hInstance, // 인스턴스
NULL); // createStruct라는 구조체의 주소
// 4 윈도우를 화면에 보여줌
ShowWindow(g_hWnd, nCmdShow);
// 5 메세지 루프
while (GetMessage(&Message, 0, 0, 0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) {
HDC hdc;
PAINTSTRUCT ps;
switch (iMessage) {
case WM_TIMER: {
}break;
case WM_PAINT: {
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}break;
case WM_LBUTTONDOWN: {
}break;
case WM_RBUTTONDOWN: {
}break;
case WM_KEYDOWN: {
switch (wParam) {
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
}break;
case WM_DESTROY: {
PostQuitMessage(0);
return 0;
}
}
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}