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.
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.
- #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;
-
-
- if (!RegisterClass(&wndclass))
- {
- MessageBox(NULL, TEXT("Window class is not registered"), TEXT("Error...."), MB_ICONERROR);
- return 0;
- }
-
- hwnd = CreateWindow(appname,
- appname,
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 600,
- 500,
- NULL,
- NULL,
- hInstance,
- NULL
- );
-
-
- ShowWindow(hwnd, iCmdShow);
- UpdateWindow(hwnd);
-
-
- 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 :
- 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.
Read more articles on
C++: