csammisrun

A rare situation

Chapter 2

without comments

What’s different about a GUI program?

For the most part, a Win32 GUI program is just like any other program. It has a start, a middle, and an end. Chapter Two outlines the minor differences between a console (text-only) C or C++ program and a Win32 GUI program.

The Start

A journey of a thousand miles begins with a single step…so, naturally, a program of a thousand lines of code begins with a single entry point. If you’ve done any C or C++ before, you’re already familiar with the entry point for those programs:

int main(int argc, char** argv);

The function is where execution of your program starts. You can’t have a console program without one. But what about GUI programs? Is there a function for that too? As a matter of fact, there is:

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);

As you can see, the differences are subtle (but many) between this and the standard console-style entry point. The function is the location in the code where execution of your GUI program will start. The specific arguments to the function will be explained when we create our first window in chapter four.

The Middle

In the function, a programmer will generally write initialization, declare some variables, and call some functions. What then does a programmer put in the function?

Any program that creates a window must have some initialization done to it; this is either done in itself or in a seperate function that is called from (details in chapter four). After initialization, enters the program into a message loop. The message loop is how Windows communicates with your program, and it is the heart and soul of the GUI program. The reason for the existance of the message loop will be explained in detail when we get to chapter three.

The Ending

To exit a console program, one simply puts

return 0;

at the end of .
This is also true of a GUI program (except the is at the end of ), but additionally, a specific message must be sent to the application when it comes time to break out of the message loop (i.e., when your program is ready to quit). Messages in general will be covered in the next chapter, and this message in particular will be shown in chapter four when we create our first window.

Does all this make sense? If not, read it again, I can wait. If yes, or if reading it again would only make you frustrated and cranky, go on to chapter three

Back to contents

Written by Chris

May 29th, 2007 at 6:26 pm

Posted in General

Leave a Reply