Tutorial 1 – Creating A Window

So hopefully by now you’ve got to grips with some of the basics of C/C++. There’s a lot you can do with applications that output to a console but after a while you want to try something different. In this series of tutorials I’ll be talking about how to create a window using the Win32 API and then having a look at what can be done with that window.

Some of my later C++ tutorials started to get a bit wordy and unfortunately using the Win32 API can be even more verbose. For that reason I’ll only be focusing on really important lines of code in these tutorials, but I will add a link to a demo project at the end of each page so that you can investigate more of the code if you want to.

Lets start by creating a new empty Win32 project in Visual studio:

File -> New Project -> Win32 Project

In this project add one source file called Main.cpp (or whatever you like, it doesn’t make too much of a difference!).

Winmain is the new Main!

As with all C++ executables on Windows, your program will need an entry point. So add this to your code:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmd, int show)
    return 0;

You can probably already see how lengthy some simple sections of Win32 code can be. There’s a couple of new things happening here, the first one is the keyword WINAPI. This goes between the return type of the function and the function name and is known as a calling convention. All this does is lets C++ know how this function will receive parameters and how it will return a result. There are also several parameters being sent into this function which we will discuss as they are used. For now it is enough to know that this is the standard entry point for a Win32 application.

Window Classes

At the moment this program starts and then exits with the return code ‘0’. Let’s add some code to make it a bit more interesting. We’re going to be creating a new window, but first we need to create a ‘Window Class’ structure that will be associated with that window:

WNDCLASSEX windowClass;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.hInstance = hInstance;
windowClass.hIcon = NULL;
windowClass.hbrBackground = (HBRUSH) COLOR_ACTIVEBORDER;
windowClass.lpszClassName = "Win32 Tutorial 1 Class";

This structure describes certain aspects of the window that we will create. First we need to let the structure know how big it is, then we set a value indicating that the window will be redrawn if it’s horizontal or vertical size changes. Next we store a handle to the instance of this application. Currently we’ll be using the default application icon so we set hIcon to NULL and let the program figure out which image to use. The hbrBackground field is a brush used to paint the background of the window, in this case it will be the “Active border” colour. We then give our class a name.


There’s one more field that we need to fill in. Win32 applications run on a system of messages; when you click the window, that is a message being sent to it, if you minimise the window, that is a message! A window needs to handle these messages appropriately and so we need to inform it which function does this handling.

windowClass.lpfnWndProc = WndProc;

The above line stores a pointer to the required function. The function named will then be used to handle any messages sent to the window.

Of course if we’ve named a function to use, we need it to actually exist! Therefore above the Winmain entry point we’ll add this function:

      case WM_CLOSE:
      case WM_DESTROY:

    return (DefWindowProc(hwnd, m, wp, lp));

As with the Winmain function there are a few new things to this code. LRESULT is a microsoft defined type that is returned by this function. CALLBACK is yet another one of those calling conventions that lets the function know how to take the parameters and return values. For the moment we don’t need to do anything to interesting with the messages. We just check if the message is a “close” or “destroy” message, and if that is the case, we request that the program exits. After that we call the default message handler and return that function’s result.

There we go, that’s all there is to creating a simple Win32 application. What you should end up with is something that looks like this

There is one minor caveat; chances are visual studio has used unicode settings by default in your application. This will throw some errors on the lines involving strings. To fix this either change the project’s settings to use multibyte strings or simply place an ‘L’ in front of the strings e.g:

L"this is a string";

As promised here’s the download link for the full solution for those of you want to look at the fine detail of the code.

Win32 Tutorial 1

  1. No comments yet.
  1. No trackbacks yet.

Tell me what you think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: