Broad Network


Your first Window

Getting to know Windows – Part 7

Volume - Windows User Interface

Forward: In this part of the series, you create your first window.

By: Chrysanthus Date Published: 28 Aug 2012

Introduction

This is part 7 of my series, Getting to know Windows. I assume you have read all the previous tutorials before this one. You should be reading the tutorials in the order given. In this part of the series, you create your first window.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Entry Point
The following is a C++ program for the console:

#include <iostream>
using namespace std;

int main()
    {
        cout << "Hello World!";

        return 0;
    }

The beginning of the main function is the entry point for the application; that is where the application starts. To compile this application with the g++ compiler, you would type something like:

    g++ hello.cpp -o hello.exe

A windows application uses a different entry point function and not main. A windows application uses the entry function called, WinMain. The prototype is:

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

The operating system normally supplies all the arguments for this WinMain function. hInstance is the window class instance handle we talked about in the previous parts of the series. The operating system supplies the actual value for you. The last statement you type in the WinMain function block is not “return 0”. It is,

    return msg.wParam;

where msg is the identifier of the message struct. Do not worry about this return statement for now.

Assume that the name of your windows application in C++ is, firstwin.cpp. For the g++ compiler, you would type the following command to compile the windows application:

    g++ firstwin.cpp -mwindows -o firstwin.exe

Note the use and position of the switch, -mwindows. This switch prevents the command prompt window from appearing when the application window appears, during execution.

Your First Window Application
I will give you the code for your first window application. You will try it. I will explain the code in general terms below. You will have the detail explanation in other series (divisions). Here is the code:

#include <windows.h>
using namespace std;


LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

    switch (uMsg)
    {

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return 0;
}


int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wcx;

    wcx.cbSize = sizeof(wcx);
    wcx.style = CS_HREDRAW | CS_VREDRAW;                  
    wcx.lpfnWndProc = MainWndProc;   
    wcx.cbClsExtra = 0;              
    wcx.cbWndExtra = 0;              
    wcx.hInstance = hinstance;       
    wcx.hIcon = NULL;
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);                
    wcx.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);              
    wcx.lpszMenuName =  NULL;
    wcx.lpszClassName = "MainWClass";
    wcx.hIconSm = NULL;


    RegisterClassEx(&wcx);


    HWND hwndMain;

    hwndMain = CreateWindowEx(0, "MainWClass", "Main Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,

CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinstance, NULL);                  

    if (!hwndMain)
        return FALSE;


    ShowWindow(hwndMain, SW_SHOW);
    UpdateWindow(hwndMain);


    MSG msg;
    BOOL bRet;


    while( (bRet = GetMessage( &msg, hwndMain, 0, 0 )) != 0)
        {
            if (bRet == -1)
            {
                // handle the error and possibly exit the application
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }


    return msg.wParam;
}

Type the application code in a text editor and save it as a file called, firstwin.cpp, in the MinGW directory. Open your command prompt window and go to the MinGW directory. Execute the following command:

    g++ firstwin.cpp -mwindows -o firstwin.exe

The compiled file should be saved as, firstwin.exe, in the MinGW directory. Open the MinGW directory now with Windows. Double-click the file, firstwin.exe. You should see a window (whose client area is dark blue).

Generalized Code Explanation
You need to include the header file, windows.h. This file contains the headers for your windows. In the code you have the window class procedure, whose name has been given by me (you can choose your own name). The block of this procedure does nothing other than call the default procedure. You then have the main function. It begins with the declaration of an identifier for the window class struct. The name of the procedure is assigned to one of the members of the class struct. The name of the class has been given by me as "MainWClass" (you can choose your own name).

The value of the class instance handle is hinstance, got from the first parameter of the WinMain function. As I said, the operating system supplies the actual value through the WinMain function when it calls the WinMain function. It is the operating system that calls the WinMain function. Do not worry what the other members of the window class WNDCLASSEX struct are doing for now.

Next, the window is created, shown and updated. Then you have the while loop to remove messages from the queue and send to the window class procedure. You will see examples of messages in other series.

The WinMain has the duty to register the class, create the main window and house the message WHILE Loop. Also note the presence of the MSG struct in the WinMain function.

After reading this tutorial, you can begin the next series called, Window Classes. Just search my blog with the title, Window Classes, to arrive at the series.

Well, I hope at this point you have general knowledge of what a Microsoft Window is, from a technical point of view. Let us stop here and continue in the next series, titled, “Window Classes”

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem

Comments

Become the Writer's Fan
Send the Writer a Message