2008
Lesson 1 – Your first windows application

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 Application’ and check the ‘Do not create any files’ checkbox. Then click Create. Call the project DrawLite and save it.

An empty project will be created. Click File » New File to add a new file to this project. We are going to save all source files (*.cpp, .h) in a folder called src under your project root folder. Create a folder called src and save this new file as main.cpp.

Now enter the following code in main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* DrawLite - Windows Programming Tutorial * by Pravin Paratey (March 08, 2007) * http://www.dustyant.com/wintut * * 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:

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 definitions 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 see their uses 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.
6 Responses (rss) (trackback)
Johan
Hello,
I am a beginner in Win32 programming and I just started by trying to follow this tutorial. I have downloaded the Code::Blocks v2.08 IDE and entered the code of lesson 1 (the lesson 1 message box). But CodeBlocks won’t compile it. By default I use the GNU GCC compiler, but I get the following message when I try to compile:
“DrawLite – Debug” uses an invalid compiler. Skipping…
Nothing to be done.”
I have tried a few other compilers (by changing build options) with the same result. The Microsoft Visual C++ 2005/2008 compiler did not produce the same message, though, but instead it failed for another reason, producing the message “LINK fatal error LNK1181: cannot open input file ‘obj\Debug\main.obj’”
Which compiler should I use?
Anything else (e.g. apply some setting) that I should do to make it work?
I’d be greatful for any tips!
Johan
Will
Johan, make sure you download the Code::Blocks installer that COMES WITH the GNU C compiler. It’s the larger of the two downloads on their website.
lol
I just started this tutorial. thank you for your all work.



Josh
Whoa.
Not only is this tutorial awesome, but your entire website and all the tutorials.
True I prefered the old tutorials on Dev-C++ because I have tried them both and prefer it, but I can’t have everything and shouldn’t be complaining.
Keep up the great work.
Beginners will find this very helpful.