Howto's for newbie hardcore programmers - Article 1

Posted by: SilentRage

Howto's for newbie hardcore programmers - Article 1 - 03/13/02 10:03 AM

Maybe my subject needs a little explaining. I think it'll be best to explain it by describing my own hard luck in getting anywhere in my C programming. I don't use VC++, I don't use borland, I use something called LCC to compile my C programs, and it doesn't automate anything unless you tell it to - which I don't. So that makes things very difficult cause the things you previously took for granted suddenly becomes a real chore to do. And lots of people out there can't help ya cause they do things the easy way. If you're a hardcore programmer, but also a newbie at C - then this is for you:

One of the first problems I faced was the fact that my program would terminate as soon as the main function returned. It didn't matter how many timers or windows I created, it wouldn't give them a chance. At first I tried putting stuff in loops and putting the Sleep() function in there to keep my program from being a processor hog... but then I discovered the RIGHT way to do it, and really, it's the only way to make your windows work.

Since I don't have any menu's with accelerators, this is what I have:

int main (int argc, char *argv[])
{
     //call your functions, make your windows, initialize your vars, blah dilay la
     //wait for messages bound to our window
     while (GetMessage(&msg, NULL, 0, 0)) {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
}
}

GetMessage will block until it recieves a message sent to your program via PostMessage or SendMessage. It is far more efficient than a loop cause it doesn't require any processing at all. if you have a form displayed, and somebody clicks on it for instance, a message is sent to your program and GetMessage will return. TranslateMessage will translate the message to other messages if need be (like a WM_KEYDOWN to a WM_CHAR) and DispatchMessage will actually send the message to the proper window's message queue.

Well, that's that! Next time I'll tell you how to add an icon to your EXE, and otherwise explain the process.