C++: How to create and use control items in GUI
This article explains how to create controls items like button, progressbar, edit box., combobox etc in a window using cpp.
Creating and using window controls
In windows, there are various predefined class names which are used to create controls in a window.
Using these classes, controls can be created by calling the function CreateWindow() or CreateWindowEx().
However some controls require additional things to be known. So i'm here to explain how to create them.
The syntax of CreateWindowEx() function is:
HWND CreateWindowEx(DWORD ExStyle,
int X,int Y,int Width,int Height,
There are 7 parameters out of 12 which are important to us. Remaining can be set to NULL as shown in following examples.
How to create a Button
HWND hBtn = CreateWindowEx(0,"Button","Button Caption",WS_CHILD|WS_VISIBLE,x,y,Width,Height,hWnd,NULL,NULL,NULL);
How to create Static(Label) control
hLabel = CreateWindowEx(0,"Static","Static Caption",WS_CHILD|WS_VISIBLE,x,y,Width,Height,hWnd,NULL,NULL,NULL);
How to Create Edit control
HWND hEdit = CreateWindowEx(0,"EDIT",NULL,WS_CHILD|ES_MULTILINE|ES_LEFT|WS_VISIBLE|WS_BORDER,x,y,Width,Height,hWnd,NULL,NULL,NULL);
Similarly you can create other controls by giving thier predefined class names as second parameter.
As I told first, there are some expections in creating some controls like ProgressBar and ComboBox.
To Create a progress bar you must add "ComCtl32.Lib" to your project. If you are using VC++, you can do it by adding following line:
After adding ComCtl32.Lib, you should Initiate it like this:
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_PROGRESS_CLASS;
Now you can create ProgressBar using CreateWindowEx() function.
When creating ComboBox, note that height must be greater than 50. Else it won't show dropdown list.
Handling Events, Setting States, and Reading Values.
As we know that WndProc function recieves different messages, these include Events from control items like button,
Editbox, combobox, etc. the WndProc function recieves WM_COMMAND message, when control item events are occured.
the information about these events can be obtained from WPARAM and LPARAM parameters, by performing bit shifting operations like
HIWORD() and LOWORD(). extracting information about events is not same for all events. therefore you have to refer MSDN to know about them.
We can set States of control items by using SendMessage() function. This is reciprocal to the method of handling events.
There we get messages and process them, here we are sending messages. The Syntax of SendMessage() function is:
First Parameter(HWND) is the handle to the control to which we are sending message.
Second Parameter(UINT) is the message to be sent. and WPARAM and LPARAM should contain Additional Informations.
An Example of Setting ProgressBar's Progress is shown below:
SendMessage(hProgress, PBM_SETPOS, 50, 0);
This will set it's progress to 50% complete state. You can find out more messages in commctrl.h header file, or at MSDN
To read values from control items like Edit, Combo, etc. you can use SendMessage(), GetDlgItemText(), GetDlgItemInt()
etc functions. commonly the Message to be sent is WM_GETTEXT. Here is an example:
char* buff = new char;
In the above example Third parameter of SendMessage() indicates the size of data to be read,
and last param is the variable that holds the data.