Win32 Programming – How to create a simple GUI based application?

By adding Graphical User Interface (GUI) to the applications we can change the whole look and feel of the applications and users will feel more comfortable accessing the GUI-based applications than the text-based or console-based applications.

Win32 API provides a vast number of API functions to create and deal with Graphical User Interface (GUI) applications. In this article, I am going to create a simple GUI application using Win32 API in “Visual C++”.

Usually, GUI-based applications contain windows, icons, and controls on windows to accept user input and display the output. All these are part of the user interface; hence we call them user-interface elements. User-interface elements can be able to display text, graphics, or image(s).

Let’s create our simple GUI application.

Step 1. The first step in creating our GUI application is, to create a window. We use CreateWindow Win32 API to create our application window. The syntax of CreateWindow API looks like the below:

HWND WINAPI CreateWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);

Where lpClassName is the name of the class, lpWindowName is the name of the window. dwStyle is the window style and x, y are the coordinates to position the window. nWidth, nHeight are the window width and height respectively. hwndParent is the parent window handle, hMenu is the menu handle of the window. lpParam is a pointer to one of the variations of CREATESTRUCT structure variable. Most of the parameters in this function are optional.

Upon success, the CreateWindow function returns the window handle. Otherwise, it returns NULL as the return value.

The function call looks like the below:

HWND hWnd = CreateWindow("", "My first GUI window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 200, 200, NULL, NULL, NULL, NULL);

From the above code, we are trying to create a 200×200 window with the window title “My first GUI window”. We are trying to create a WS_OVERLAPPEDWINDOW and make it visible (WS_VISIBLE) when we run the application.

The code looks like the below:

// sample.cpp
#include <windows.h>
#include <stdio.h>

int main()
    HWND hWnd = CreateWindow("", "My first GUI window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 200, 200, NULL, NULL, NULL, NULL);
    if (hWnd == NULL)
       printf("0x%x\n", GetLastError());

   return 0;

Observe that in the code we used the main() function instead of WinMain(); just for simplicity. If we use the main() function, the console window is automatically attached to the application; so, we can use printf like functions to display the results on the console window.

Step 2. Now we need to compile and run our program. Let’s compile it, to generate an executable file. Below is the command to compile our “sample.cpp” file.

c:\Visual Studio Projects 2012>cl sample.cpp /link User32.lib

Because we have used the “CreateWindow” function in our code and it is referenced in the “User32.lib” file, we need to link the “User32.lib” library file when compiling our “sample.cpp” file. Otherwise, we will get the below error message when compiling our “sample.cpp” file:

sample.obj : error LNK2019: unresolved external symbol __imp__CreateWindowExA@48
referenced in function _WinMain@16

After successful compilation and linking the “User32.lib” library file, the above command generates the “sample.exe” file. Now it’s time to run our application “sample.exe”.

Let’s run it; and observe that, our application will not display any window. It simply displays the error code “0x57f” on the console window.

Is there anything wrong we did in our code? Why our application didn’t display the GUI window?

Step 3. To figure out what went wrong, first, we need to analyze the error code “0x57f” which is displayed on the screen when we run our application.

To analyze this, we will use the “Visual Studio 2012” tool “Error Lookup”. “Error Lookup” tool is used to look up the error message which belongs to the error code; for example: in our case, the error code is “0x57f”.

Once we type this error number “0x57f” in the “Error Lookup” window, and click on the “Lookup” button; the “Error Lookup” tool will display the message “Cannot find window class.”. That means our application returns a “Cannot find window class.” error. But why our application returns this error message? To understand this, first, we need to understand what is window class and how it works.

A Window class is nothing but a structure containing attributes and a window procedure to process window messages to control window behavior and appearance. We can use the same window class for multiple windows. We can use pre-defined window classes (eg: BUTTON, STATIC, etc.,.) or we can create our own window classes. Before using a window class to create GUI windows, first, the window class has to be registered with Windows Operating System.

Remember that the first argument to our “CreateWindow” function is, the name of the window class. In our program, we have passed an empty string. Actually, we have to pass a valid window class name; either pre-registered or our own window class name. Because we passed an empty string; we got a “Cannot find window class.” error when we run the application; that means we need to pass a valid window class name when calling the “CreateWindow” Win32 API function.

I want to create our window with our own class instead of pre-defined window class names.

So, how to create our own window class? Let’s discuss creating our own window class and registering for the class, in our next article.


Win32 Programming – How to create a simple GUI based application?

3 thoughts on “Win32 Programming – How to create a simple GUI based application?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top