Getting Started With Windows Programming In C/C++

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. 
  1. #include"stdafx.h"  
  2. #include<Windows.h>  
  3. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)  
  4. {  
  5.     MessageBox(NULL, TEXT("Hello World\nThis is Windows Programming"), TEXT("Hello"), MB_OK);  
  6.   
  7.     return 0;  
  8. }   
Description:
  • Windows.h : This is the header file for windows program.

  • WINAPI:

    This is Windows Application Programming Interface(WINAPI) to tell the compiler that this program contain graphical components than console components.

  • WinMain: 

    This is the main function of all windows programs.This function takes four parameters.It is same as main() function in simple C/C++ programs.

  • HINSTANCE:

    This is Handle to Instance.As you know what is the meaning of instance in Object-Oriented programming. It is the number used as object.In this program the object of program is handled by itself.There may be many instances of one program.This handles only new comer instance. Next parameter hPrevInstance handles previous instance.

  • PSTR: 

    This is Pointer to String parameter.It is same as char* but meaning is different.This parameter is same as command line argument(args) in Object-Oriented programming language.

  • MessageBox : This method display a message box.

  • TEXT : This function is same as char* in C/C++ but in windows it is PCWSTR(Pointer to Character Wide String).

  • MB_OK : This is #define function for adding only OK button to messagebox. 
Now let's create a window.

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

Copy & Paste following program into your project & Debug it. 
  1. #include"stdafx.h"  
  2. #include<Windows.h>  
  3.   
  4. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lparam);  
  5.   
  6. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)  
  7. {  
  8.     TCHAR appname[] = TEXT("Windows Programming");  
  9.     WNDCLASS wndclass;  
  10.     MSG msg;  
  11.     HWND hwnd;  
  12.   
  13.     wndclass.cbClsExtra = 0;  
  14.     wndclass.cbWndExtra = 0;  
  15.     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);  
  16.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);  
  17.     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);  
  18.     wndclass.hInstance = hInstance;  
  19.     wndclass.lpfnWndProc = WndProc;  
  20.     wndclass.lpszClassName = appname;  
  21.     wndclass.lpszMenuName = NULL;  
  22.     wndclass.style = CS_HREDRAW | CS_VREDRAW;  
  23.   
  24.     // check this window class is registered or not  
  25.     if (!RegisterClass(&wndclass))  
  26.     {  
  27.         MessageBox(NULL, TEXT("Window class is not registered"), TEXT("Error...."), MB_ICONERROR);  
  28.         return 0;  
  29.     }  
  30.   
  31.     hwnd = CreateWindow(appname,  //window name  
  32.                             appname,   // window text  
  33.                             WS_OVERLAPPEDWINDOW, // window style  
  34.                             CW_USEDEFAULT,   //default window position x  
  35.                             CW_USEDEFAULT,   //default window position y  
  36.                             600,   //width  
  37.                             500,    // height  
  38.                             NULL,  
  39.                             NULL,  
  40.                             hInstance,  
  41.                             NULL  
  42.                        );  
  43.   
  44.     // show & update created window  
  45.     ShowWindow(hwnd, iCmdShow);  
  46.     UpdateWindow(hwnd);  
  47.   
  48.     // get message from queue  
  49.     while (GetMessage(&msg, NULL, 0, 0))  
  50.     {  
  51.         TranslateMessage(&msg);  
  52.         DispatchMessage(&msg);  
  53.     }  
  54.   
  55.     return msg.wParam;  
  56. }  
  57.   
  58. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)  
  59. {  
  60.     HDC hdc;  
  61.     PAINTSTRUCT ps;  
  62.   
  63.     switch (message)  
  64.     {  
  65.     case WM_CREATE :  
  66.         MessageBox(NULL, TEXT("Window is started !"), TEXT("Welcome"), MB_ICONINFORMATION);  
  67.         return 0;  
  68.         break;  
  69.   
  70.     case WM_PAINT :  
  71.         hdc = BeginPaint(hwnd, &ps);  
  72.   
  73.         SetTextColor(hdc, RGB(0, 0, 240));  
  74.         RECT rect;   
  75.         GetClientRect(hwnd, &rect);  
  76.   
  77.         DrawText(hdc, TEXT("This is Windows Programming"), -1, &rect, DT_VCENTER | DT_CENTER | DT_SINGLELINE);  
  78.   
  79.         EndPaint(hwnd, &ps);  
  80.         break;  
  81.   
  82.     case WM_DESTROY :  
  83.         PostQuitMessage(EXIT_SUCCESS);  
  84.         return 0;  
  85.     }  
  86.   
  87.     return DefWindowProc(hwnd, message, wParam, lParam);  
  88. }  
Description :
  • WNDCLASS : Structure to create window class.

  • MSG : Message structure

  • HWND : Handle to Window.This structure handles the created window.

  • HBRUSH : Handle to Brush.

  • LoadCursor : This function load cursor for created window.IDC stands for ID number for Cursor.

  • LoadIcon : This function load icon for created window.IDI stands for ID number for Icon.

  • GetStockObject : Obtains a graphic object, in this case a brush used for painting the window's background.

  • RegisterClass : This function takes one parameter of WNDCLASS structure variable.

  • CreateWindow : This function create the window.

  • Parameters : CreateWindow(window_name,window_text,window_style,x,y,width,height,parent,menu,hInstance,lparam);

  • ShowWindow : This function shows the created window.

  • UpdateWindow: 

    This function update the created window. Everything in windows programs are handled using messages.Messages are the numbers stored in a queue. Each number define the specific task which will be performed by program.
  • GetMessage : This function get the message from the queue.

  • LRESULT : It means the generated result is Long type.

  • CALLBACK : Defining this keyword,it specify the function to call it back.

  • WPARAM : PARAM means parameter & W stands for WORD which is 16 bit unsigned short integer.

  • LPARAM : L stands for Long.

  • HDC : Stands for Handle to Device Context.It is a structure that handles to a devices such as graphics.

  • PAINTSTRUCT : It is a structure for paint components.

  • WM_CREATE : This function is same as Form_Load() function in C#.It means that window is created.

  • WM_PAINT : This function paints the created window.In C# it is same as OnPaint() method.

  • WM_DESTROY : This is same as window closing event. 
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 ?
  • cb : counts of bytes
  • hbr : handle to brush
  • h : handle
  • lpfn : long pointer to function
  • lpsz : long pointer to string terminated with zero.
Other important prefixes
  • CS : Class Style
  • CW : Create Window
  • DT : Draw Text
  • IDI : ID number for Icon
  • IDC : ID number for Cursor
  • MB : Message Box
  • SND : Sound
  • WM : Window Message
  • WS : Window Style
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++: