Home | API | MFC | C++ | C | Previous | Next

Programming Windows API

Adding Controls - Child Windows

A child window or control is a prepacked window that exists inside and is dependent on a parent window.  Controls cut down repetitive development tasks and promote a consistent and recognisable application interface. The child window processes mouse and keyboard messages and notifies the parent window, by way of command messages when the child window’s state has changed. Child controls are created like any other window using the CreatWindowEx() or CreateWindow() function.  Although it’s possible to create user-defined child window controls, Windows includes several predefined standard child window controls using predefined classes created within Windows. This predefined name is used as a class parameter in the CreateWindow call. Windows offers more than 20 types of controls; six, are known as classic controls and are implemented as standard.

Edit Control

An edit or textbox control is a rectangular control window that allows the user to enter and edit text. The text in the edit controls can be left-justified, right-justified, or centered. Edit controls are set by default to a single line but can be created as multiline. An edit control is limited to about 60 KB of text. If there is a requirement to handle large amounts of text, an enhanced version of the standard edit control known as the rich edit control is available as part of the common control library. The predefined class name for a textbox in the CreatWindow function is “EDIT”

In the following edit control example clicking the button title ‘set title’ changes the contents of the static box to the value of the textbox. The Windows API function GetWindowText() and SetWindowText() are used to get and set the contents of both the editbox and staticbox.

Display Code
Download Code


Static Control

Static controls are commonly used as labels for other controls.  The static control cannot be selected, accept input from the keyboard or mouse and does do not send WM_COMMAND messages back to the parent window. A static control can be used to display text, shapes, or a picture such as a icons and bitmap. A static window is created by using “STATIC” as the window class in the CreateWindow function. For an overview of its configuration and use see the combobox control example (below)


Buttons

A Button is a simple control used to trigger an action. When a button is clicked, it sends a WM_COMMAND message to the parent window. 

Push Button- A push or command button is a rectangular clickable window typically containing a text label describing its function. The predefined class name used in the CreateWindow function is “BUTTON”.

Check Boxes - A checkbox is a small square box and associated label that describes the purpose of the checkbox. The specified text within the label will usually appear to the right of the checkbox. Checkboxes function as a toggle switch allowing the user to select and de-select options. Clicking the box once causes a checkmark to appear; clicking again toggles the checkmark off. Both of these actions send a message to the parent window.  The predefined class name used for a checkbox in the createwindow function is "BUTTON" with the dwStyle being set to "BS_CHECKBOX"

In the checkbox example below, clicking any of the checkboxes changes the window's background to a mixture of the corresponding checkbox colour.

Display Code
Download Code

Radiobutton - A radio button is a control element that usually allows the user to choose only one of a predefined set of mutually exclusive options.  The control consists of a small circle and a descriptive label. Radio buttons are arranged in groups of two or and are mutually exclusive. This means that when the user selects a particular radio button, any previously selected radio button in the same group becomes deselected. Unlike checkboxes, radio buttons do not work as toggles. The predefined class name for a radio button in the CreateWindow() function is “BUTTON” with the dwStyle being set to “BS_AUTORADIOBUTTON”

In the radiobutton example below, clicking any of the buttons changes the window's background to the corresponding colour.

Display Code
Download Code


The Scrollbar

A scroll bar is an object that allows the user to adjust a particular value, a section of the window or view, by navigating either left and right or up and down. A scroll bar appears as a long bar with a small button at each end. Between these buttons, there is a moveable bar called a thumb. Scrollbars exist in two forms: the standard scroll bar and the scroll bar control. The standard scroll bar is an integral part of a window, whereas the scroll bar control exists as a separate control.

To scroll, the user can click one of the buttons or grab the thumb and drag it.  Unlike the button controls, scroll bar controls do not send WM_COMMAND messages to the parent window. Instead, they send WM_VSCROLL and WM_HSCROLL messages.

The predefined class for the scrollbar is SCROLLBAR. To add a scrollbar to an existing window add the WS_HSCROLL and WS_VSCROLL parameters to the dwStyle parameter when creating the window


The Listbox Class

A listbox displays a list of items displayed as a scrollable list within a rectangle. Users can select or deselect one or more of these items by clicking the appropriate line of text.  The predefined class for the listbox is “LISTBOX”

The following short program creates a listbox and then adds a limited number of selectable items. Clicking any list item results in the selected listview value being copied to the static box. The following messages are sent to both the listbox and staticbox as a SendMessage API function parameter.

LB_ADDSTRING- add items to the listbox
LB_GETCURSEL – get index of currently selected item in listbox
LB_GETTEXT – retrieves listbox item text
WM_SETTEXT – sets value of static box display value

Display Code
Download Code


A Combo Box

A combo box or drop-down list is a combination of both a listbox and editbox. This allows the user to either type a value directly or select a value from the list.  Combo boxes come in three varieties: simple, drop-down, and drop-down list. The predefined class for the scrollbar is “COMBOBOX”

The following short program displays a dropdown list or combo box. Selecting (clicking) any item will result in the value being displayed in the static box. The following messages are sent to both populate the combo box and detect changes –

CB_ADDSTRING – Adds a string to the list box of a combo box. 
CBN_SELCHANGE – Sent when the user changes the current selection in the list box of a combo box. 
CB_GETCURSEL – sends to the combobox to retrieve the index of the currently selected item.
CB_GETLBTEXT – retrieves a string from the list of a combo box.

Display Code
Download Code


Home | API | MFC | C++ | C | Previous | Next
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions

Last Updated: 17 September 2022