WinAPI 0002 - 윈도우 클래스의 등록
- 공유 링크 만들기
- X
- 이메일
- 기타 앱
지난번에 이어 wWinMain 함수를 좀 더 다듬어 보자.
우선, RegisterClass 함수를 통해 윈도우 클래스를 등록해 보자. WinAPI에서는 틀에 해당하는 윈도우 클래스를 먼저 등록하고, 이걸 통해 윈도우를 생성하는 방식을 사용한다.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
BOOL InitApplication(HINSTANCE hinstance)
{
WNDCLASSEXW wcx = { };
wcx.cbSize = sizeof(wcx); // size of structure
wcx.style = CS_HREDRAW | CS_VREDRAW; // the class styles
// https://learn.microsoft.com/en-us/windows/win32/winmsg/window-class-styles
wcx.lpfnWndProc = DefWindowProcW; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
// The number of extra bytes
// to allocate following the window-class structure.
// The system initializes the bytes to zero.
wcx.cbWndExtra = 0; // no extra window memory
// The number of extra bytes to allocate following the window instance.
// The system initializes the bytes to zero.
// If an application uses WNDCLASSEX
// to register a dialog box
// created by using the CLASS directive in the resource file,
// it must set this member to DLGWINDOWEXTRA.
wcx.hInstance = hinstance; // handle to instance
wcx.hIcon = LoadIcon(NULL,
IDI_APPLICATION); // predefined app. icon
wcx.hCursor = LoadCursor(NULL,
IDC_ARROW); // predefined arrow
wcx.hbrBackground = GetStockObject(
WHITE_BRUSH); // white background brush
wcx.lpszMenuName = L"MainMenu"; // name of menu resource
wcx.lpszClassName = L"MainWClass"; // name of window class
wcx.hIconSm = LoadImage(hinstance, // small class icon
MAKEINTRESOURCE(5),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
// Register the window class.
return RegisterClassEx(&wcx);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
if ( !InitApplication(hInstance) )
return FALSE;
return 0;
}
주석을 제거한 소스코드는 다음과 같다.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
BOOL InitApplication(HINSTANCE hinstance)
{
WNDCLASSEXW wcxw = { };
wcxw.cbSize = sizeof(wcxw);
wcxw.style = CS_HREDRAW | CS_VREDRAW;
wcxw.lpfnWndProc = DefWindowProcW;
wcxw.cbClsExtra = 0;
wcxw.cbWndExtra = 0;
wcxw.hInstance = hinstance;
wcxw.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcxw.hCursor = LoadCursor(NULL, IDC_ARROW);
wcxw.hbrBackground = GetStockObject(WHITE_BRUSH);
wcxw.lpszMenuName = L"MainMenu";
wcxw.lpszClassName = L"MainWClass";
wcxw.hIconSm = LoadImage(hinstance, MAKEINTRESOURCE(5), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
return RegisterClassExW(&wcxw);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
if ( !InitApplication(hInstance) )
{
return FALSE;
}
return 0;
}
컴파일 시에는 gdi32 라이브러리를 링크해 줘야 한다. 소스 코드 파일이름이 ex2.c 일 경우에 다음과 같이 입력한다.
gcc ex2.c -o ex2 -municode -lgdi32
실행시켜도 아직은 아무것도 뜨지 않는다. 다음에는 윈도우를 하나 띄우도록 해 볼 것이다.
관련 문서는 다음과 같다.
RegisterClassExW 함수(winuser.h)
GetStockObject function (wingdi.h)
LoadImageW function (winuser.h)
MAKEINTRESOURCEW macro (winuser.h)
GetSystemMetrics function (winuser.h)
Conventions for Function Prototypes
- 공유 링크 만들기
- X
- 이메일
- 기타 앱
댓글
댓글 쓰기
서로간에 존중해주시고 예의를 지켜주시길 부탁드립니다.