-
Learning win32API
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);
}
-
-
I dont understand the WindProc function, when is it called, i dont see it called in the program, and how does the msg, created in WinMain (as a MSG type) get filled with queues from WindProc, if there is nothing using the definition how does it work???
-
nvm i went searching for a long time online, took me less time to find it then it did to get an answer, the callback function is called everytime something happens in the window and then the message loop uses the messages made by the callback function.and puts them onto the window.
-
And what you have there is a program that should be compiled as a GUI application. If you want a console application then use the old main function instead of winamin.
-
well ya but i just said that i was writing the code, and i quickly added the cout functions so i could see when the callback was used etc...
-
isn't it sort of logical when it is used?
-
i'm currently studying the same book. just finished chapter #13 on using the printer. chapter #14 discusses bitmaps and bitblt.
fun book.
if you still want to use the dos console whilst in winmain, this tutorial will show you how to get a handle to the dos console.
-
ya it really is, lots of history too. never thought a little history could be some fun lol. im on chapter 4 now WHOOP! :)