×

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the difference between the buttons. We use different buttons for a proper structure for an application and a suitable design. Any user can select a button in three different ways they are:

  • By clicking it with a mouse.
  • By using a specific tab to enter
  • By using arrow keys to move within a group

The working of a button is like this. First, the system provides a button for the keyboard focus. Then the button sends a message to the parent window asking to notify it of the selection. After this, the system sends a message to the button to change its state, and finally, the button will output its new state.

In this article, we got a little idea of what a button is, so now we will learn how to create the button in C++ programming language and some other features and uses of basic buttons on a web application.

Creating a button in C++ programming language:

We use one primary function to create a button: CreateWindow or CreateWindowEx dynamically. To work with this function, we must first learn about all windows controls and have a proper windows user interface programming with C/C++ language pre-installed. Now let us look at an example code below to create a button using the CreateWindow function.

Code:

HWND hwndButton = CreateWindow( 
    L"BUTTON",  // a predefined class (Unicode)
    L"OK",      // text of button
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
    10,         // x-axis position 
    10,         // y axis position 
    100,        // width of button
    100,        // height of button
    m_hwnd,     // main window
    NULL,       // No extrenall links
    (HINSTANCE)GetWindowLongPtr(m_hwnd, GWLP_HINSTANCE), 
    NULL);      // no pointers required

In the above C++ example code, we used a new parameter (m_hwnd). This is used to handle the main window. And the word BS_DEFPUSHBUTTON refers to the style type of the button, where it specifies this button is a default push button. And then, we assigned the size and position values. In this code, we specified the dimensions and position values because we used CW_USEDEFAULT, so this function automatically sets all parameters to zero as default. And with this piece of code, we can create a button on any application required.

Applications of buttons:

  • A button can send messages to the main window, and the main window returns a message back to the button. So the main or parent window will send the message to the button through an overlapped window using a function called SendMessage. The application will use the BM_GETCHECK message to retrieve the check status of a radio button.
  • Similarly, notifications from a button are sent to the main window as WM_COMMAND or WM_NOTIFY messages. These will reach information about which message is used and the reference for every notification.
  • Button color messages are a default color provided by the system, where an application can retrieve default values by identifying the color. It can identify by using a function called GetSysColor, or we set the values by using a function called SetSysColors. And this SetSysColors will affect the whole application, so we should not use this function for the customized buttons in an application.
  • There is a default messaging process for the button where the system procedure for the predefined button control window class is. This is a default processing procedure for all messaging buttons, and this will state any Boolean statements given to a button.
  • The predefined window procedure will pass all other messages to the function DefWindoeProc for the default message processing.

Conclusion:

In this article, we have covered the basics of a button and its purpose of the button. Then we have learned how to create a button in C++ programming language. Then we have discussed on the messaging concepts and protocols of the button, like sending messages to the button, notification messages from the buttons, button color messages, and a few related topics. And with all these concepts, we will get a clear idea of how to use buttons. Where to use buttons? And why should we use the button? 


Related Topics

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

How to Reverse a String in C++ using For Loop

For Loop: We may loop through a certain section of C++ code repeatedly using the for loop. A for loop is carried out if the test expression yields a true result. The...

4 minutes read.

Object in C++

In this article, we will learn about Object in C++. In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to...

3 minutes read.

C++ Reading file

In file handling, read() function is used to read data from the file into the program. The read() uses ifstream library to read data from a file. Syntax file-stream-class   file-stream-object;   file-stream-object.read((char *)&var , sizeof (var)); <h3">Example ofstream  outfile;         outfile . read((char*)&emp,sizeof(emp)); C++ File Handling read() Function Example Reading the content of existing...

2 minutes read.

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

Differences between #define & const in C/C++

 Differences between #define & const in C/C++ A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation. In this chapter, we'll learn about the member, variable,...

3 minutes read.

Print Table Using While Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.

Lexicographically Next Permutation in C++

In this tutorial, we'll look at how to use C++ to generate the lexicographically next permutation of a string. The lexicographically next permutation is the larger permutation. "ACB," for example,...

3 minutes read.

Swap numbers in C++

Swap numbers Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming...

4 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.