Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

C++ Win32 API Tutorial 1: First Window

Joined
Aug 4, 2010
Messages
572
Reaction score
177
Hello,

Today you will be learning the simple code to start Win32 for creating your very first window if this is your first time.

WinMain function

Every Windows application must have at least two functions. The WinMain function and the window procedure. The WinMain funtion is the entry point to the Windows application.

The WinMain function initializes the application, shows the application window on the screen and enters the main loop.

Code:
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow );

hInstance is handle of instance. It is a 32-bit number identifying the instance of our program within the OS environment. This number is given by Windows, when the program starts executing. hPrevInstance parameter is always NULL. It is a legacy from 16-bit Windows. Windows programs can still be started from the command line. The parameters given are stored in lpCmdLine. The nCmdShow value specifies, how the window will be displayed. Minimized, maximized or hidden.

The WinMain function terminates, when it receives the WM_QUIT message.

Registerig window

Before we can create a window, we must register it within the Windows. All windows must be registered. Later on we will see, that we do not register a window, when we create a button, static text etc. This is because these controls are predefined. They have already been registered. Programming in C/winapi means a lot of working with structures. To register a window, we must create and fill an WNDCLASS structure. We set the window style, extra allocation bytes, window class name, handle of the program instance, background brush, optional menu name, window procedure, handle of the cursor and icon. Finally, we call the RegisterClass() function.

Code:
Creating a window

The window is created by calling the CreateWindow() function.

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

The lpClassName uniquely identifies the window. It is the name, under we registered the window. The lpWindowName is a window name. It's effect depends on context. It can be title of the window in parent windows or a label in child windows like button or static text. Windows can be created using several styles. For this, we have the dwStyle parameter. The x, y specify the initial horizontal and vertical position of the window. The nWidth and nHeight specify the window width and height. The hWndParent is a handle to the parent window. For windows that do not have parents, we use NULL. For a parent window the hMenu is an optional handle to the menu, for a child window, it is a control identifier. The hInstance is a handle to the program instance. The lpParam is the last parameter, it is an optional value passed to the window during the WM_CREATE message. The CreateWindow() function returns handle to the newly created window.

Messages

The WinMain function creates a message loop. It is an endless cycle, which runs during the life of the application. Message loop is a programming construct that waits for and dispatches events or messages in a program. Windows communicate using messages. A message is an integer value that identifies a specific event. May it be a button click, resizing of the window or closing of an application. There can be multiple messages created in a moment. The messages cannot be processed all at the same time. Therefore there is a message queue. The message enters the message queue and waits until it is processed. The GetMessage() function retreives the message from the message queue. The DispatchMessage() function dispatches a message to a window procedure. If the application obtains character input, we include the TranslateMessage() function in the loop.

Code:
Window procedure

 LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);


Every window must have a window procedure. It is a function, that receives messages. The hwnd is a handle to the window, that received the message. The uMsg is the message. The wParam and lParam parameters provide additional message information. The messages come from the user or from the Windows OS. We react to a message or we call the default window procedure to provide default processing. Most messages are sent to the default window procedure. The default window procedure is called DefWindowProc(). It is called with the same parameters as the normal window procedure.

Here's your example you can eaither copy paste this or actually study it.

Code:
#include <windows.h>

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
    MSG msg;
    HWND hwnd;
    WNDCLASS wc;
    
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.lpszClassName = TEXT("Era");
    wc.hInstance = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpszMenuName = NULL;
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    
    RegisterClass(&wc);
    hwnd = CreateWindow( wc.lpszClassName, TEXT("Era"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                500, 500, 500, 500, NULL, NULL, hInstance, NULL);
    
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    
    while (GetMessage(&msg, NULL, 0,0)){
        DispatchMessage(&msg);
    }
    return (int) msg.wParam;
}

    LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
        switch(msg)
        {
            case WM_DESTROY:
                {
                    PostQuitMessage(0);
                    return 0;
                }
            }
            return DefWindowProc( hwnd, msg, wParam, lParam );
        }

Follows a step by step explanation of the code example.

Code:
 #include <windows.h>

<windows.h> is a header file for the C programming language. It contains all function declarations in the API, all common macros and all the data types. The Win32 API is added to the C programming project by linking the necessary libraries, kernel32.lib, user32.lib, gdi32.lib and by including the <windows.h> header file.

Code:
 wc.style = CS_HREDRAW | CS_VREDRAW;
We set the window style here. The CS_HREDRAW and CS_VREDRAW flags mean that whenever there is a movement or size adjustement of the height or width of the window, the entire window is redrawn.

Code:
 wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
In our example, we do not use the additional bytes. So we set the members to zero.

Code:
wc.lpszClassName = TEXT( "Window" );
Window is a class name for this particular window type. We will use this class name when creating the window.

Code:
 wc.hInstance = hInstance ;
We set the instance of our program.

Code:
 wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
Here we set the background brush. It is the color that is used to paint the client area of the window.

Code:
 wc.lpszMenuName  = NULL;
In our example, we do not create a menu.

Code:
 wc.lpfnWndProc = WndProc;
We provide the window procedure for the window class.

Code:
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hIcon   = LoadIcon(NULL, IDI_APPLICATION);
We set the cursor and the icon for our application. We load them from system resources.

Code:
 RegisterClass(&wc);
We register the window class with the system.

Code:
 ShowWindow(hwnd, nCmdShow);
  UpdateWindow(hwnd);
These two lines show the window on the screen. The nCmdShow specifies, how we display the window on the screen.

Code:
 while( GetMessage(&msg, NULL, 0, 0)) {
  DispatchMessage(&msg);
 }
This is the message loop. We receive messages from the message queue using the GetMessage() funcion and dispatch them to the window procedure using the DispatchMessage() call.

Code:
 switch(msg)  
  {
    case WM_DESTROY:
      {
        PostQuitMessage(0);
        return 0;
      }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
Here we react to the WM_DESTROY message. The PostQuitMessage() sends WM_QUIT message to the message queue. All other messages are sent to the default processing using the DefWindowProc() function call.

Credits to : Wizcoder
Credits to : http://zetcode.com/tutorials/winapi/window/

Give thanks if I helped you! :<:
 
Junior Spellweaver
Joined
Apr 12, 2006
Messages
121
Reaction score
26
I don't see what's the point of copy/pasting tutorials from somewhere else into here.

Edit: Gotta like people having "XML" as a programming language in their sig :lol:

@WizCoder
Aren't you lacking ASM, Delphi, Fortran, COBOL, ALGOL58, B, AWK, Objective-C, Python, Perl in your sig? Seriously how far have you got to the languages listed in there?
 
Last edited:
Back
Top