Post tutorial Report RSS WinAPI introduction

A short introduction to programming Windows applications using C++.

Posted by on - Basic Client Side Coding

First, you need to obtain Visual C++ Express if you haven't, here:
Microsoft.com
Click Visual C++ 2010 Express to expand options/menu. BTW, it's free for 30 days, then you need to obtain a free key to continue to use it, so basically free. Only Visual Studio 2010 Professional is paid.

Then, inside the program:
File -> New -> Project... -> Empty Project.

Add a new file to the project, name it as you want. Below two simple examples of WinAPIs.

#include <Windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int CmdShow)
{
	MessageBox(NULL, TEXT("Text inside the window"), TEXT("Window title/caption"), MB_OK);
	return 0;
}

And the second one:

#define WIN32_LEAN_AND_MEAN

#include <Windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MessageBox(NULL, "The content of the window", "Title", 0);
	return 0;
}

This one isn't included in the archive containing examples (you'll find it below).

Let's break these examples down.

The above WinMain() function is the backbone, the main part of the Windows program.

The following MessageBox() displays simple dialog box. As you can see, the first example contains instead of 0 number a constant MB_OK. MB_OK is an OK button. So what's that 0 number? It's an OK button too. In fact, it defines what default button the window possess.

Check what happens if you use number 1 instead of 0.

Also, you can write MB_, then press CTRL + SPACEBAR to bring up IntelliSense and check out different types of buttons. For example, instead of MB_OK, you can write MB_COMPOSITE. ;-)

The second example also includes a definition:
WIN32_LEAN_AND_MEAN
It makes the application lightweight / "lean", excluding any rarely used stuff, allowing your programs to compile faster.

You can find more info and more tutorials here:
Zetcode.com

Already coded & compiled examples from Zetcode.com you can find here on ModDB:
Moddb.com

These examples are somewhat changed. E.g. where the header CommCtrl.h is included, these two lines were added:
#pragma comment(lib, "comctl32.lib")
<- a pragma comment, that is a linker option
#define _WIN32_WINNT_ 0x0500
<- macro

Without that linker option you may get linker errors (even if everything is well coded), and without that macro your application can have less functionality... than it should. ;-) At least I experienced such problems, you may not need these.

Also objects such as holdPen, holdBrush were defined as HPEN and HBRUSH types while they should be of HGDIOBJ type to work correctly. That was modified, too.

Oh, and yes, to compile any of those examples, you first need to remove comments from one file at a time:
/*
at the beginning and
*/
at the end. :-)

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: