Hello, I am an Engineering Manager at Facebook with 13+ years in Ad Technology, Natural Language Processing and Data mining. (Learn More)
by Pravin Paratey

Lesson 1 - Your first windows application

Updates

  1. November 20, 2010 - Updated code to Code::Blocks 10.05
  2. July 18, 2008 - Moved to Code::Blocks
  3. April 18, 2003 - Original version for Dev-C++

Welcome to your first lesson. We will begin first by familiarizing ourselves with Code::Blocks, a C++ IDE that I will be using throughout this series of lessons. That done, you will create your first windows application which will display ‘Hello World!’ in a message box.

Setting up your development environment

I have chosen Code::Blocks as the IDE to base this tutorial on. Simply because it’s free and therefore accessible to all of you. All screenshots and references in this tutorial are with respect to Code::Blocks. However, you should be able to use any IDE and compiler you like. A few other free C++ IDEs are listed at the end of this section.

If you haven’t done so, download and install Code::Blocks. That done, fire up Code::Blocks. I have set up my editor options (Settings > Editor) to show me line numbers.

Diving In

We are going to start with a blank project. Click File > New Project. Select ‘Win32 GUI Project’ and hit Go. Call the project DrawLite and save it.

![Create new Project](/img/win32/1-1.png)

A default main.cpp file will be created. Open it and replace the code to the one below:

/* DrawLite - Windows Programming Tutorial
 * by Pravin Paratey (March 08, 2007)
 * http://pravin.insanitybegins.com/win32
 *
 * Source released under Creative Commons Attribution 2.5 Licence
 * http://creativecommons.org/licenses/by/2.5/
 */

#include <windows.h>

int WINAPI
    WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, "Hello World! This is my first win32 program!",
        "Lesson1", MB_OK);

    return 0;
}

Press F9 to build and run the project. You should see:

![Your first windows program](/img/win32/1-4.png)

Congratulations! You have just written your first windows application.

Whoa! What just happened there?

Lets break down the code.

#include <windows.h>

All Windows programs must include the header file windows.h. This file has the function declarations of Windows system calls or the WinAPI. The WinAPI has everything necessary for programming under windows.

WinMain (..)

This is the entry point of a windows application. This is like the main() of a console based application. WinMain is declared as,

int WINAPI WinMain(
HINSTANCE hInst,        /* Handle to the current instance */
HINSTANCE hPrevInstance,/* Handle to the previous instance */
LPSTR lpCmdLine,        /* pointer to command line arguments */
int nCmdShow);          /* show state of the window */

The parameters of WinMain are self-explainatory, except perhaps nCmdShow. nCmdShow tells you in what manner you are expected to display this window (Maximized, Minimized, etc). We haven’t used any of these parameters in this lesson, but we’ll learn more about them in the coming lessons.

MessageBox(..)

This is a Windows function which displays a messagebox. The MessageBox function is declared as,

int MessageBox(
HWND hWnd,        /* Handle of owner window */
LPCTSTR lpText,   /* Address of text in message box */
LPCTSTR lpCaption,/* Address of title of message box */
UINT uType);      /* Style of message box */

return 0

This is the return value to the system.

This brings us to the end of the first lesson. Go to Lesson 2.

List of a few free C++ IDEs

Dev-C++
Pros: Winning reason - devpaks.org, opensource, supports multiple compilers
Cons: Debugger is occasionally iffy
Microsoft Visual C++ Express Edition
Pros: The only reason would be its awesome debugger that lets you do almost anything.
Cons: The free version does not allow you to make stand-alone exe’s. This document will help those who wish to circumvent that.