In this article, we are going to learn about Button control in C++/UWP.
Button control allows the user to perform actions. They possess mainly two properties, content and click event. Content properties are used to set the caption of the Button and the click event is used to trigger the event.
Content property is used to set the caption of the button. We can’t directly assign the text (In VB or C#, we can directly assign the text value), instead we use the Inspectable interface to assign the text.
IInspectable
IInspectable is the base interface to all Windows runtime classes. This interface is used for projecting COM features to the other languages, like C++, C#, JavaScript etc. To convert the string to IInspectable, we have to use the PropertyValue class.
PropertyValue
PropertyValue is used to convert the datatype (like string, int, float) to IInspectable.
Here is the sample code assigning the Content text.
Button Click Event
Button click is handled in the RoutedEventHandler event, passing the RoutedEventHandler function as argument (callback function). RoutedEventHandler callback has two input parameters, one is Sender, sender parameter IInspectable interface, then RoutedEventHandler as a 2nd argument.
Ex
In the below sample application, the user clicks the button, and a message box will be shown.
Precompiled Headers (pch.h) file
Precompiled header files use a faster process for the compiler, allowing us to see in detail. Based on the API we have to include header files in the .cpp file, lets suppose two .cpp files used the same header files, when you compile the application, the header files get compiled twice. To avoid that, add all the header files into one pch.h header file and include pch.h header file in .cpp files. Pch.h header file will get compiled only one time. How does this file get compiled only one time? Answer: #pragma once.
#pragma once
Predefine macro, it uses the current source file and includes it only once to a compiler. Ex: Pch.h header file mostly used all the application file, complication time, compiler try to include all the pch.h header main times, to avoid that, we are using #pragma once and inform to have the complier add the pch.h file only once.
#pragma comment
#pragma comment (lib,"windowsapp”): This comment is used to link the library file into the application, no need to add into the linker dependency list (VC++ project settings)
Main Function
int __stdcall wWinMain (HINSTANCE, HINSTANCE,PWSTR,int)
WinMain
It’s a traditional entry point function for windows programming, it has four main properties:
HINSTANCE
Instance handle of the Application. HINSTANCE: It’s not used, it was used in the 16-bit applications. PWSTR: It contains the command line arguments. INT: It is used windows style, whether it is minimized or Maximized or Normal.
__stdcall
Standard calling conversation (__stdcall) is used to inform the compiler how the function arguments should be added into the stack.
Application::Start
Running the standard application message loop of the current thread. This function takes the callback function as an argument.
Application
Application class is used to encapsulated complete application life cycle, such as app entry point, particularly for various activation contracts, app lifetime management, app-scoped resources, and unhandled exception detection.
OnLaunched
This function gets invoked when the application is launched. We can override this function to do some default initialization or overwrite the default behaviour. In this sample we have added the TextBlock control to the window, to display some data.
Namespace
Namespace is a collection of objects. To organize into one name you should use namespace. In C++/UWP programming, all the WinRT APIs go under one namespace (i.e.) “winrt”.
Declare namespace in C++/UWP programming like below.
using namespace winrt;
Conclusion
I hope you understood how to use the button control.