WinAPI 0003 - CreateWindowExW 함수
- 공유 링크 만들기
- X
- 이메일
- 기타 앱
CreateWindow 함수는 CreateWindowEx 함수의 매크로로 구성되며, 이때 dwExStyle 의 값은 0L이 된다. 따라서 CreateWindowEx 함수만을 알고 있으면 된다.
HWND CreateWindowExW(
[in] DWORD dwExStyle,
[in, optional] LPCWSTR lpClassName,
[in, optional] LPCWSTR lpWindowName,
[in] DWORD dwStyle,
[in] int X,
[in] int Y,
[in] int nWidth,
[in] int nHeight,
[in, optional] HWND hWndParent,
[in, optional] HMENU hMenu,
[in, optional] HINSTANCE hInstance,
[in, optional] LPVOID lpParam
);
소스 코드는 다음과 같다.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
BOOL InitApplication(HINSTANCE hinstance)
{
WNDCLASSEXW wcxw = { };
wcxw.cbSize = sizeof(wcxw);
wcxw.style = CS_HREDRAW | CS_VREDRAW;
wcxw.lpfnWndProc = WindowProc;
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);
}
HWND InitInstance(HINSTANCE hinstance, int nCmdShow)
{
HWND hwnd;
hwnd = CreateWindowExW(
0,
L"MainWClass", // name of window class
L"Sample", // title-bar string
WS_OVERLAPPEDWINDOW, // top-level window
CW_USEDEFAULT, // default horizontal position
CW_USEDEFAULT, // default vertical position
CW_USEDEFAULT, // default width
CW_USEDEFAULT, // default height
(HWND) NULL, // Parent window. no owner window
(HMENU) NULL, // Menu. use class menu
hinstance, // handle to application instance
(LPVOID) NULL); // no window-creation data
if (!hwnd) { return hwnd; }
// Show the window and send a WM_PAINT message to the window
// procedure.
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return hwnd;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
MSG msg;
HWND hwnd;
if ( !InitApplication(hInstance) )
{
return FALSE;
}
if ( !(hwnd = InitInstance(hInstance, nCmdShow)) )
{
return FALSE;
}
BOOL fGotMessage;
while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 &&
fGotMessage != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
UNREFERENCED_PARAMETER(pCmdLine);
}
주석을 없앤 코드는 다음과 같다.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
BOOL InitApplication(HINSTANCE hinstance)
{
WNDCLASSEXW wcxw = { };
wcxw.cbSize = sizeof(wcxw);
wcxw.style = CS_HREDRAW | CS_VREDRAW;
wcxw.lpfnWndProc = WindowProc;
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);
}
HWND InitInstance(HINSTANCE hinstance, int nCmdShow)
{
HWND hwnd;
hwnd = CreateWindowExW(
0,
L"MainWClass", // name of window class
L"Sample", // title-bar string
WS_OVERLAPPEDWINDOW, // top-level window
CW_USEDEFAULT, // default horizontal position
CW_USEDEFAULT, // default vertical position
CW_USEDEFAULT, // default width
CW_USEDEFAULT, // default height
(HWND) NULL, // Parent window. no owner window
(HMENU) NULL, // Menu. use class menu
hinstance, // handle to application instance
(LPVOID) NULL); // no window-creation data
if (!hwnd) { return hwnd; }
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return hwnd;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
MSG msg = { };
HWND hwnd;
if ( !InitApplication(hInstance) ) { return FALSE; }
if ( !(hwnd = InitInstance(hInstance, nCmdShow)) ) { return FALSE; }
BOOL fGotMessage;
while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 &&
fGotMessage != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
UNREFERENCED_PARAMETER(pCmdLine);
}
관련 문서는 다음과 같다.
- https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexw
- https://learn.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles
- https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindoww
- https://learn.microsoft.com/en-us/windows/win32/winmsg/using-window-classes
- https://learn.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program
- https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types
- https://learn.microsoft.com/en-us/windows/win32/winmsg/windows
- https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
- https://learn.microsoft.com/en-us/windows/win32/winmsg/using-windows
- https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessage
- https://learn.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
- 공유 링크 만들기
- X
- 이메일
- 기타 앱
댓글
댓글 쓰기
서로간에 존중해주시고 예의를 지켜주시길 부탁드립니다.