Updated April 4, 2023
Definition of Windows Programming
Windows programming is synonymously called as C/C++ programming language as the Operating system present within it is written in C. Most developers try to learn C/C++ language despite pure object-oriented language because it creates the base for learning and implementation of Windows programming. All major embedded software is written in Windows programming only like the CLR of the .NET framework is also written in a windows programming language. It makes file and folder saved with an extension where .cpp is used for saving and need to include “stdafx.h” for retrieving the file and message.
Syntax:
There is a proper syntax flow for Windows programming with C++ which is represented as follows:
# include "stdafx.h"
#include<Windows.h>
Int WINAPI Winmain(Hinstance hinstnc, Hinstance hPrevInstnc, Pstr szcmd, int icmdshw)
{
MessageBox(NULL, Text("welcome_.."), Text("welcome"), Mb_ok);
Return 0;
}
Each has a significance where,
- Windows.h: It signifies the header inclusion for the windows program.
- WINAPI: It stands for the Windows application programming interface which lets the compiler understand that the program contains graphical user components than console components.
- WinMain: This acts as the main program for the windows programs which considers four functions as parameters.
- HINSTANCE: This acts as one of the handles to instances as it means the object-oriented programming is being handled for some of the other reasons and gets blend with other new instance programmers for implementation as well.
- PSTR: This stands for the pointer to string which means it acts as a char* pointer only but with little difference.
- Message Box: This is a method that is used synonymously as a print statement for printing messages relevant to it.
- MB_Ok: It stands for a # define a function to acknowledge the print or display of text is proper.
How to Create Windows programming with C++?
There are certain pre-requisites that need to be taken care of while creating Windows programming with C++ like:
- Install Visual Studio code (version) as per requirement.
- Go to the main menu and then select File > New > Project to open and create a new project in the dialog box all this will happen in Visual Studio Code.
- At top of the dialog box just set language to C++ and then set the platform as Windows, then set the project type as Desktop.
- Further, select the project type as Windows Desktop Wizard then select next for further manipulation.
- Choose to create the desktop-related button for the project.
- Then start to create the code for editing and manipulating.
Let’s deep dive into the creation of code for which Windows Desktop Application is needed:
- Just as C and C++ program has the main function to start the program flow same in case of windows programming it consists of WinMain function to start the program flow which is represented as follows:
int WINAPI WinMain (
_ip_ HINSTANCE hInstnce,
_Ip_op_t_ HINSTANCE hPrvInstnc,
_Ip_ LPSTR lp_cmd_line,
_Ip_ int k_CmdShow_0
);
- To indicate that it is getting run using Windows desktop program that it is important to understand the headers that need to be included like <windows>, <tchar.h>. If there is something where Unicode is already declared as part of code, then it is not much needed to tchar exclusively if enabled only wtchar can directly be used. Represented as follows:
#include<windows.h>
#include<wtchar.h>
- WinMain() function is used for every windows desktop application where it can be called and used whenever there are some events occurring over the windows for transition. Suppose the developer try to hit the Ok button over the window this event will take place in the presence of WinMain() function where the call-back function includes parameters having definite significance:
Lrslt_Call Windw_Proc (
Ip_Hd hwndow,
Ip_Uint Msg,
Ip_Wprm wprm,
Ip_Lprm lprm)
Examples
Let us discuss examples of windows programming.
Example #1
This code snippet is an example to demonstrate how to register within a window class as represented below.
const wchar_t CLASS_NAME [] = L"Welcome_Demo_Class";
WINDW_CLASS wc_0 = { };
wc_0.lpfnWndw_Proc = WindowProc_0;
wc_0.hInstance_1 = hInstance_c;
wc_0.lpszClassName_0 = CLASS_NAME_1;
Explanation:
Once the Window is associated with the window class It will have all its class name and hinstance parameters to be registered with the main program. This will give WINDW_CLASS wc_0 as a pointer to the window prompt for reference. All the events will get associated with the main class once attached to it. CLASS_NAME_1 identifies the window class with reference to the main window. Then this WINDW_CLASS gets registered with the register class. This makes the window class register with the structure which is defined in the remaining class variables and members.
Example #2
There is a way to create a new instance of the window by calling CreateWindowExp function which further has some parameters for reference.
HWND hwnd_0 = CreateWindowExp (
0,
CLASS_NAME,
L"New to windows programming",
WS_OVERLAPPEDWINDOW,
CW_USE_DEFAULT, CW_USE_DEFAULT, CW_USE_DEFAULT, CW_USE_DEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd_0 == NULL)
{
return 0;
}
Explanation:
Here this function CreateWindowExp is used for creating a new instance of a window class for many manipulations where the parameters have different levels of significance starting from the class name, windows text, default variables for positioning and sizing giving NULL values and instances then making createWindowexp for registering the instances if it becomes Null then it will return the value.
Example #3
This program demonstrates the code snippet for message display properly for any windows programming so that the understanding and visibility get enhanced.
MSG msg_0 = { };
While (getMessage (&msg_0, NULL, 0, 0) > 0)
{
TranslateMessage (&msg_0);
DispatchMessage (&msg_0);
}
Explanation:
Here the message found as part of get message will translate the message into an understandable format using TranslateMessage function and then it will dispatch the message with DispatchMessage making the program more enhanced.
Conclusion
Windows Programming is a very versatile form of programming language although needs a basic understanding of C/C++ to develop an application for it. All the .NET framework-related codes and compilations make use of Windows Programming which makes the overall experience enhanced and robust for both developer and end-user. Windows Programming in C++ has evolved a lot and is still evolving with the incorporation of multiple technologies.
Recommended Articles
This is a guide to Windows programming with C++. Here we discuss the definition, syntax, How to create Windows programming with C++? examples with code implementation. You may also have a look at the following articles to learn more –