The thing i dont quite understand in this is the msg variable which is of the MSG type. I have googled this and i cant find nothing. Why am i predefining msg? what is it? and where do i use it in WinMain()?
You need MSG in windows programming the way you need int's and char's and string's in console programming
what is it?
"Windows sends some messages directly to your callback function but others are placed in a queue. These tend to be keyboard messages or other input..."
source: http://www.toymaker.info/Games/html/winmain.html
where do i use it in WinMain()?
If you make a window more complex than a simple message box, you will need to process MSG's
from the code you posted:
//snipped
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT(“Skeleton”);
WNDCLASSEX wndclass;
HWND hWindow;
MSG msg;
//fill up the window structure
//..and icons and cursors
//...and colors
//then, register your 'class'
//...and create your window
//...don't forget to 'show' it
//and, finally we're at the message loop, where 'msg' is processed:
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&msg, NULL, 0, 0) > 0)
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&msg);
/* Send message to WindowProcedure */
DispatchMessage(&msg);
}
//return something... but we're not there, yet ;)
}
- the message loop (and comments) came from Code::Blocks (with a bit of modification -credit theForger's winAPI tutorial)
As in a previous post [1] I refer you to the MSDN tutorial: Learn to Program for Windows in C++
http://msdn.microsoft.com/en-us/library/ff381399(v=VS.85).aspx
Thanks, when we use the GetMessage(&msg, NULL, 0, 0); function why do we fill &msg with the information about the message, because by doing that are we not putting information into msg's address in memory?
... why do we fill &msg with the information about the message, because by doing that are we not putting information into msg's address in memory?
That's what Windows (the os) expects. Anytime a window (your app) is created, the os creates a message queue for that thread. GetMessage() will retrieve a message from the queue, and returns a '0' or '1' (actually nonzero) or '-1' (for errors).
We fill &msg not only for GetMessage() but for TranslateMessage() and DispatchMessage too.
GetMessage(msg, NULL, 0, 0);// assume earlier we declared: MSG msg;
GetMessage() expects a pointer to a message structure, so we pass an alias of its instance i.e &msg. Anything other than that gives a compile error. Also remember that passing a copy of your structure to a function is inefficient.
Do we fill &msg and not msg because msg is a structure, so if we tried to fill it in a variable, msg, we would not be filling the structure, thats why we fill &msg because we are telling it where the attributes are located?
I would guess we are passing the address (&msg) as opposed to the actual variable (msg) because we want the function to modify the variable we are passing so we can use the information later.
hmmm, im still a little confused, would you be able to answer my previous question in a different way please, i mean, you said "want the function to modify the variable we are passing so we can use the information later", im abit confused about that part :)
Also, a question on the message which GetMessage() recieves...
I understand that GetMessage() pulls the first message of the head of the queue, my question is what if the message doesnt apply to the application, does it just reject it?
I understand that GetMessage() pulls the first message of the head of the queue, my question is what if the message doesnt apply to the application, does it just reject it?
Easier to think that GetMessage() only return -1/0/nonzero -that's all it does.
If you refer to the GetMessage() loop I posted earlier you'd see that all nonzero messages (paint, keystroke, mouse clicks, etc) will go into the loop; if needed, TranslateMessage() will process it first
then everything will passed on to DispatchMessage() which passes it to the window procedure (there should be one in every nontrivial win32 app).
There'd be a switch-case block in the window procedure that handles all the messages passed to it by DispatchMessage() -the ones a programmer wants/needs to handle will be processed by the app, while the ones that "doesnt apply to the application" will be handled by the DefWindoProc() function.
p/s
1. all of these are of course in the links posted in this very thread
2. I'm not sure about this, but has anybody recommended you Petzold's (holy) book?
3. how much have you coded win32 apps? any tutorial you're currently following?
Ahh ok, thanks :) Now back to the other question of...
"Do we fill &msg and not msg because msg is a structure, so if we tried to fill it in a variable, msg, we would not be filling the structure, thats why we fill &msg because we are telling it where the attributes are located?"
Am i on the right track with that, or am i completely wrong in saying that :)
I think so. Your wording is a bit awkward. If you were to pass simply msg, you would pass a copy and any changes made to it in the function would be lost after it returns. By passing the address, the function can modify the variable you have (putting in message information) so you can use it after the function ends. Consider this example: