Introduction

Windows Programming is the C programming language in which the Microsoft Windows Operating System is written. In the world, most of the developers use C windows programming to develop software than Object-Oriented languages. To learn this language,you must have complete knowledge about basics of C/C++ programming language. Even the CLR of .NET framework is written in windows programming.

Procedure

Step 1. Start Visual Studio & Go to File, New, then Project,

Step 2. Drop Visual C++ list and Select Win32 Project.,

Step 3. Enter project name & click ok.

Step 4. Win32 Application Wizard dialog box appeared then click finish.

Once your project is created then you can see already added code to editor in Visual Studio. Remember your file is saved as .cpp extension so you must include "stdafx.h" header file. Now, remove all the contents from the editor of Visual Studio of your created project. All right,Let's start. Here's the windows program that display message box. Copy & Paste following program into your project & Debug it.

#include"stdafx.h"  
#include<Windows.h>  
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)  
{  
    MessageBox(NULL, TEXT("Hello World\nThis is Windows Programming"), TEXT("Hello"), MB_OK);  
  
    return 0;  
}

Description:

Now let's create a window.

Here's the program to create window in C++.

Copy & Paste following program into your project & Debug it.

#include"stdafx.h"  
#include<Windows.h>  
  
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lparam);  
  
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)  
{  
    TCHAR appname[] = TEXT("Windows Programming");  
    WNDCLASS wndclass;  
    MSG msg;  
    HWND hwnd;  
  
    wndclass.cbClsExtra = 0;  
    wndclass.cbWndExtra = 0;  
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);  
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);  
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);  
    wndclass.hInstance = hInstance;  
    wndclass.lpfnWndProc = WndProc;  
    wndclass.lpszClassName = appname;  
    wndclass.lpszMenuName = NULL;  
    wndclass.style = CS_HREDRAW | CS_VREDRAW;  
  
    // check this window class is registered or not  
    if (!RegisterClass(&wndclass))  
    {  
        MessageBox(NULL, TEXT("Window class is not registered"), TEXT("Error...."), MB_ICONERROR);  
        return 0;  
    }  
  
    hwnd = CreateWindow(appname,  //window name  
                            appname,   // window text  
                            WS_OVERLAPPEDWINDOW, // window style  
                            CW_USEDEFAULT,   //default window position x  
                            CW_USEDEFAULT,   //default window position y  
                            600,   //width  
                            500,    // height  
                            NULL,  
                            NULL,  
                            hInstance,  
                            NULL  
                       );  
  
    // show & update created window  
    ShowWindow(hwnd, iCmdShow);  
    UpdateWindow(hwnd);  
  
    // get message from queue  
    while (GetMessage(&msg, NULL, 0, 0))  
    {  
        TranslateMessage(&msg);  
        DispatchMessage(&msg);  
    }  
  
    return msg.wParam;  
}  
  
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)  
{  
    HDC hdc;  
    PAINTSTRUCT ps;  
  
    switch (message)  
    {  
    case WM_CREATE :  
        MessageBox(NULL, TEXT("Window is started !"), TEXT("Welcome"), MB_ICONINFORMATION);  
        return 0;  
        break;  
  
    case WM_PAINT :  
        hdc = BeginPaint(hwnd, &ps);  
  
        SetTextColor(hdc, RGB(0, 0, 240));  
        RECT rect;   
        GetClientRect(hwnd, &rect);  
  
        DrawText(hdc, TEXT("This is Windows Programming"), -1, &rect, DT_VCENTER | DT_CENTER | DT_SINGLELINE);  
  
        EndPaint(hwnd, &ps);  
        break;  
  
    case WM_DESTROY :  
        PostQuitMessage(EXIT_SUCCESS);  
        return 0;  
    }  
  
    return DefWindowProc(hwnd, message, wParam, lParam);  
}

Description

DefWindowProc

This function returns the generated result message to the WinMain function. WNDCLASS structure indexes In above program you can see the structure components with prefixes like cbClsExtra,hbrBackground,lpfnWndProc etc.

So what are they?

Other important prefixes

Above program starts its execution from WinMain function.

That takes the hwnd created window & message,show & update the window.

But what should the created window do?

This is provided by WndProc function.

WndProc is assign to the function of structure variable wndclass - lpfnWndProc.

Here's the output of above program.

output

Read more articles on C++: