How to Create a Button
To create buttons dynamically, you use the CreateWindow or CreateWindowEx function. This topic demonstrates how to use the CreateWindow function to create a default push button.
What you need to know
Technologies
Prerequisites
- C/C++
- Windows User Interface Programming
Instructions
Use the CreateWindow function to create a button control.
In the following C++ example, the m_hwnd parameter is the handle to the parent window. The BS_DEFPUSHBUTTON style specifies that a default push button should be created. Note that the size and position values must be specified because using CW_USEDEFAULT for a button sets the values to zero.
HWND hwndButton = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"OK", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
10, // x position
10, // y position
100, // Button width
100, // Button height
m_hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLongPtr(m_hwnd, GWLP_HINSTANCE),
NULL); // Pointer not needed.
Related topics