|
Prev | Up |
Next
Lesson 1 - Your First Windows Program
Click here to download the source files to this tutorial.
Welcome to a journey in Windows Programming. In this lesson, you'll create
your first windows application which will display "Hello World" in a message
box.
- If you haven't started Dev-C++ already, do so now.
- Select File->New Project. In the dialog box that appears, choose the
project type as an Empty Project and name the project Lesson1. Also choose
the C language.

- You land up with an empty project. Now we will set the project type to
Win32 GUI. This tells the compiler that the project is to be compiled as a
windows GUI application. Press Alt+P to get the project options. Select
Win32 GUI under Type.

- Right click Lesson1 on the Project Panel and click New Unit. A new file
is created. Save it as lesson1.c
- Now enter the code as given below:
/* Windows Programming Tutorial Series
* Lesson 1 - Your first windows program
* Pravin Paratey (October 08, 2002)
**/
#include <windows.h>
int WINAPI
WinMain(HINSTANCE hInst,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MessageBox (NULL, "Hello World! This is my first WIN32 program",
"Lesson 1", MB_OK);
return 0;
}
- Press F9 to compile and run the project. You should see the following,

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 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. Click the link below to go
to the next lesson.
Next Lesson4
|