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:
- 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;
// 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);
}

Mohammed IbrahimPosted May 4, 2016, 11:01 AM
nice
NitinPosted May 4, 2016, 7:40 AM
Thanks for sharing
Ammar ShaukatPosted May 4, 2016, 7:13 AM
this is cool
Kuppurasu NagarajPosted May 3, 2016, 9:48 PM
Nice Sharing..
Thiruppathi RPosted May 3, 2016, 5:20 PM
NIce One..