Win32 API: Controls
This section provides documentation for the Windows API functions related to common user interface controls. These functions allow you to create, manage, and interact with standard Windows controls such as buttons, edit boxes, list boxes, and more.
Overview
Windows controls are fundamental building blocks for creating graphical user interfaces (GUIs). The Win32 API provides a rich set of functions for manipulating these controls. Controls are typically child windows that receive input and display output.
Common Control Categories
- Buttons: Push buttons, radio buttons, check boxes.
- Edit Controls: Single-line and multi-line text input fields.
- List Boxes: Displaying lists of items from which the user can select one or more.
- Combo Boxes: A combination of an edit control and a list box.
- Scroll Bars: Horizontal and vertical scroll bars.
- Static Controls: Used for displaying text, rectangles, or bitmaps.
- Dialog Boxes: Modal and modeless dialogs for user input and interaction.
Key Control Functions
Creating Controls
Controls are typically created using the CreateWindowEx function, specifying a class name that corresponds to the desired control type. For example:
HWND hButton = CreateWindowEx(0, L"BUTTON", L"Click Me", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, 10, 100, 30, hwndParent, NULL, hInstance, NULL);
Sending Messages to Controls
Interaction with controls is primarily done by sending messages to their window handles using the SendMessage or PostMessage functions. Each control type has a specific set of messages it understands.
Example: Getting Text from an Edit Control
To retrieve text from an edit control, you can send the EM_GETTEXT message:
TCHAR buffer[256];
int length = SendMessage(hEditControl, EM_GETTEXT, sizeof(buffer)/sizeof(TCHAR), (LPARAM)buffer);
if (length > 0) {
// Process the text in 'buffer'
}
Common Control Messages
BM_CLICK: Simulates a button click.EM_SETSEL: Selects text in an edit control.EM_GETLINE: Retrieves a line of text from an edit control.LB_ADDSTRING: Adds a string to a list box.CB_GETCURSEL: Gets the index of the currently selected item in a combo box.WM_SETTEXT: Sets the text of most controls.WM_GETTEXT: Retrieves the text of most controls.
CreateWindowEx
Function: CreateWindowEx(DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
Creates an extended window.
- Parameters: Explain common parameters like
lpClassName(e.g., "BUTTON", "EDIT", "LISTBOX", "COMBOBOX"),dwStyle,hWndParent. - Return Value: A handle to the new window, or
NULLif the function fails.
SendMessage
Function: LRESULT SendMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
Sends a message to a specified window or windows. The current window process waits until the message has been processed.
- Parameters:
hWnd,Msg(the message to send),wParam,lParam. - Return Value: The message-dependent return value of the window procedure.
PostMessage
Function: BOOL PostMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
Places a message in the message queue of the specified thread and returns without waiting for the thread to process the message.
- Parameters:
hWnd,Msg,wParam,lParam. - Return Value: Nonzero if the message was placed in the message queue; otherwise, zero.
For detailed information on specific control types and their associated messages, please refer to the relevant documentation sections.