C++: How to create and use control items in GUI

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, LPCSTR ClassName, LPCSTR WindowName, DWORD Style, int X,int Y,int Width,int Height, HWND hParentWindow, HMENU hMenu, HINSTANCE hInstance, LPVOID Param);

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


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:

#pragma comment(lib,"ComCtl32.Lib")

After adding ComCtl32.Lib, you should Initiate it like this.

INITCOMMONCONTROLSEX icc; icc.dwSize = sizeof(icc); icc.dwICC = ICC_PROGRESS_CLASS; InitCommonControlsEx(&icc);

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; SendMessage(hEdit,WM_GETTEXT,(WPARAM)10,(LPARAM)buff); MessageBox(0,buff,"Item:Edit",MB_OK);

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.

1 comment:

  1. this is the one which i was searching for.. Thanks a lot dude..


Thank you for commenting. Please keep visiting.