For all programmers who are skilled in C-style languages, and beginners who fish for new experience with these!

Report article RSS Feed WinAPI introduction

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

Posted by feillyne on Nov 18th, 2010
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.

cpp code:
#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:

cpp code:
#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
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Established
Jul 20, 2010
Privacy
Public
Subscription
Open to all members
Contact
Send Message
Email
Members Only
Membership
Join this group
Group Watch
Track this group
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Groups
Curly Bracket Programming Realm
Curly Bracket Programming Realm Hobbies & Interests group with 74 members
Indie Devs
Indie Devs Hobbies & Interests group with 1,129 members