×

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

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

Include Guards in C++

In C++ programming, we frequently utilize a class more than once, so it is necessary to create a header file and include it in the main program. Now, occasionally a...

3 minutes read.

How to create a stack in C++

A stack is a data structure, which is of linear type. A specific order has to be followed while inserting and deleting the elements from the stack. Generally, stack follows...

5 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

SET Data Structure in C++

Generally, in our home, we store food items in a fridge or kitchen efficiently to find them easily and use them. Similarly, in programming, we need to store the data...

6 minutes read.

C++ Call by Value

In this article, we will discuss C++ Call by Value with their syntax, examples, use cases, advantages, and disadvantages. C++ Function A function is a set of statements that executes a task....

5 minutes read.

Sum of all elements between k1’th and k2’th Smallest Elements

In this tutorial, we will look at how to determine sum of all given elements between two given indexes’ smallest elements. Assuming an array of integers and two numbers, k1...

2 minutes read.

Hybrid Inheritance

C++ Hybrid Inheritance When more than one type of inheritance is combined in single inheritance is called as hybrid inheritance. C++ Hybrid Inheritance Example #include<iostream>   using namespace std;   class Student{       protected:           int rollno;       public:           void getRoll(int a){               rollno=a;           }           void putRoll(void){               cout <<"Roll No: "<< rollno<<endl;           }   };   class Test : public Student{       protected:           float subject1, subject2;       public:           void getMarks(float x, float y){               subject1=x;               subject2=y;           }           void putMarks(void){               cout<< "Marks gain: "<<endl <<"Subject1 =  "<<subject1<<endl<<"Subject2 = "<<subject2 <<endl;           }   };   class Sport{       protected:           float score;       public:           void getScore(float s){               score=s;           }           void putScore(void){               cout<<"Sports score: "<<score<<endl;           }   };   class Result : public Test, public Sport{       float total;       public:           void display(void);   };   void Result:: display(void){       total=subject1+subject2+score;       putRoll();       putMarks();       putScore();       cout<<"Total Score: "<<total<<endl;   }   int main(){       Result stu;       stu.getRoll(10);       stu.getMarks(40,50);       stu.getScore(60);       stu.display();       return 0;   } Output: Roll No: 10 Marks gain: Subject1 = 40 Subject2 = 50 Sports score: 60 Total...

1 minute read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 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.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.

C++ Program to move all zeros to the end of the array

Write a program to move all the zeros in the arr[] to the end. The order of the non-zero elements should not be altered and all the zeros should be...

3 minutes read.

C++ Inline function

A C++ function that extends in line when called is known as an inline function. It reduces function call overhead by having the compiler use the function code rather than...

4 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.