rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 learn more   get in touch 

Logo - I Build Search
Jul 18
2008

Lesson 1 – Your first windows application digg

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.

Create new Project

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.

Adding source file Adding source file

Now enter the following code in main.cpp:

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:

Your first windows program

Congratulations! You have just written your first windows application.

Whoa! What just happened there?

Lets break down the code.

  1. #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.
  2. 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.

  3. 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 */
  4. 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)

#1

Josh

September 14th, 2008 at 3:23 am

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.

#2

Johan

February 23rd, 2009 at 2:31 am

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

#3

Will

April 27th, 2009 at 6:53 pm

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.

#4

mike

June 15th, 2009 at 4:18 am

OMG,I just need it!
Thanks!

#5

larry

January 9th, 2010 at 12:17 am

This is is freakin great

#6

lol

February 16th, 2010 at 4:55 pm

I just started this tutorial. thank you for your all work.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Latest Articles

Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it! [Read More]
Jan
21

Writing a spider in 10 mins using Scrapy

I came across Scrapy a few days back and have grown to really love it. This tutorial will illustrate how you can write a simple spider using Scrapy to scrape data off Paul Smith. All this in 10 minutes. [Read More]

Featured Projects

NLP classes for PHP

NLP classes for PHP

This is an ongoing project to develop a set of classes for Natural Language Processing. Some code would be ported from the NLTK project.

[Read More]

Yahoo Messenger Client for *nix

Yahoo Messenger Client for *nix

Yux is an alternative Yahoo Messenger client for *nix systems that attempts to match the look and feel of the original Windows client.

[Read More]

This page and its contents are copyright © 2010, Pravin Paratey.