WinAPI - 윈도 프로시저 콜백
- 공유 링크 만들기
- X
- 이메일
- 기타 앱
https://learn.microsoft.com/ko-kr/windows/win32/learnwin32/your-first-windows-program
https://learn.microsoft.com/ko-kr/windows/win32/learnwin32/writing-the-window-procedure
WinAPI의 가장 기본적인 단위 중 하나는 윈도우입니다. 다음 예제는 윈도우를 생성하고 처리하는 방법을 보여줍니다.
#ifndef UNICODE #define UNICODE #endif #include <windows.h> LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { // Register the window class. const wchar_t CLASS_NAME[] = L"Sample Window Class"; WNDCLASS wc = { }; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // Create the window. HWND hwnd = CreateWindowEx( 0, // Optional window styles. CLASS_NAME, // Window class L"Learn to Program Windows", // Window text WS_OVERLAPPEDWINDOW, // Window style // Size and position CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, // Parent window NULL, // Menu hInstance, // Instance handle NULL // Additional application data ); if (hwnd == NULL) { return 0; } ShowWindow(hwnd, nCmdShow); // Run the message loop. MSG msg = { }; while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } 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); }
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
다음과 같은 네 개의 매개 변수가 있습니다.
hwnd는 창에 대한 핸들입니다.
uMsg는 메시지 코드입니다. 예를 들어 WM_SIZE 메시지는 창의 크기가 조정되었음을 나타냅니다.
wParam 및 lParam에는 메시지와 관련된 추가 데이터가 포함되어 있습니다. 정확한 의미는 메시지 코드에 따라 달라집니다.
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_SIZE: { int width = LOWORD(lParam); // Macro to get the low-order word. int height = HIWORD(lParam); // Macro to get the high-order word. // Respond to the message: OnSize(hwnd, (UINT)wParam, width, height); } break; } } void OnSize(HWND hwnd, UINT flag, int width, int height) { // Handle resizing }
LOWORD 및 HIWORD 매크로는 lParam에서 16비트 너비 및 높이 값을 가져옵니다. 창 프로시저는 너비와 높이를 추출한 다음 이러한 값을 함수에 OnSize 전달합니다.
창 프로시저가 실행되는 동안 동일한 스레드에서 만든 창에 대한 다른 모든 메시지를 차단합니다. 따라서 창 프로시저 내에서는 긴 처리를 피합니다. 예를 들어 프로그램이 TCP 연결을 열고 서버가 응답할 때까지 무기한 대기한다고 가정합니다. 창 프로시저 내에서 이 작업을 수행하면 요청이 완료될 때까지 UI가 응답하지 않습니다. 이 시간 동안 창은 마우스 또는 키보드 입력을 처리하거나, 자체적으로 다시 그리거나, 닫을 수 없습니다.
대신 Windows에 기본 제공되는 멀티태스킹 기능 중 하나를 사용하여 작업을 다른 스레드로 이동해야 합니다.
새 스레드를 만듭니다.
스레드 풀을 사용합니다.
비동기 I/O 호출을 사용합니다.
비동기 프로시저 호출을 사용합니다.
- 공유 링크 만들기
- X
- 이메일
- 기타 앱
댓글
댓글 쓰기
서로간에 존중해주시고 예의를 지켜주시길 부탁드립니다.