Well im pretty excited, I just picked up Programming Windows Fifth Edition by Petzold a couple days ago and have been reading it since. But theres one consept I just cant seem to grasp, I tried adding some cout functions to a console to see what was going on, fyi because I compiled in a console, I couldnt use GetStockObject so there is no background. Can someone please help me understand how the message loop works. Or just explain this code for me. Thanks!
Code:#include <windows.h> #include <iostream> using namespace std; LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[ ] = "TestProgram"; HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); wndclass.hbrBackground = NULL; wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("ERROR: Window could not be registered!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow (szAppName, TEXT ("Josh's Test Program"), WS_OVERLAPPEDWINDOW, 400, 400, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); cout << "MEssage has been looooped\n"; } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; cout << "Call to CALLBACK FUNCTION!\n"; switch (message) { case WM_CREATE: cout << "CALL TO CREATE!\n"; return MessageBox (hwnd, TEXT ("WINDOW CREATED!"), TEXT ("TEST"), MB_ICONEXCLAMATION); case WM_PAINT: hdc = BeginPaint (hwnd, &ps); GetClientRect (hwnd, &rect); DrawText (hdc, TEXT ("Hello!!!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint (hwnd, &ps); cout << "CALL TO PAINT!\n"; return 0; case WM_DESTROY: PostQuitMessage(0); cout << "CALL TO DESTROY\n"; system("pause"); return 0; } return DefWindowProc (hwnd, message, wParam, lParam); }



LinkBack URL
About LinkBacks


