GameProgrammer.org
tutorials for dummies bug source let me google it for you my blog all about me
Beginning Win32 SDK Programming
In this article you will be introduced to the rudiments of Win32 SDK programming and you will create your first Win32 application. You can use virtual any C++ compiler to compile the sample code but I prefer to use Visual C++ 6.0 or later.

The first difference between a windows application and a console one is that windows applications are message-based and in fact everything that a windows application does is getting user messages, processing messages and reacting appropriately. So, you might be wondering what actually a 'message' is? Messages in windows include mouse movement, mouse clicking, keystrokes, etc. Every windows application contains a WinMain() function that is the equivalent of main() in a console application and usually WinMain() contains an infinite loop that gets user messages and sends it to the message processor function, continually. The Message processor function that is written by the programmer is where all messages will be processed.

Let's start programming:

If you are using Visual C++, Create an empty Win32 Application Project and add a new C++ file to the project, now enter the following code:

#include <windows.h>

int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance ,LPSTR lpCmdLine ,int nCmdShow){

    MessageBox( NULL , "Hello World!", "Win32 Tutorial" , MB_OK );

    return 0;
}

This program is simplest windows application, If you compile and run this, you will see a message box with a "Hello World" title. Now let's explain about the code:

First, we've included "windows.h" header file that contains base windows functions and then we've defined the WinMain() function. WinMain() takes four arguments, First one is the application instance that is in fact a unique integer which is given by Windows. Second parameter is not used any more and is just for compatibility which had been an instance of the same program that was already running. Next parameter lpCmdLine is a pointer to a null terminated string that specifies command line arguments and last parameter, nCmdShow specifies initial window state (Minimized, Maximized,...).

MessageBox() function as its name says, creates a message box. It takes four arguments too. First parameter is a handle to message box owner window, setting NULL indicates that this dialog hasn't any owner. Second parameter is the message text and the next parameter is the caption of dialog box. The last parameter specifies dialog properties. Pass these values instead to see some different message boxes: MB_HELP, MB_OKCANCEL.

And at last the program exits simply returning 0.

Of course this was not a complete Win32 application because a complete Win32 application has a main window and a message loop. In the next tutorial we will show you how to write a complete Win32 application. Please feel free to contact me if you have any questions or comments.

Posted on : 13 Apr 2004
Vahid Kazemi

Copyright © 2003-2010 Vahid Kazemi, GameProgrammer.org

iPhone Games