Learn Universal Windows Programming Via Modern C++ (Button Control)

Before reading this article, I highly recommend reading the introduction part:

In this article, we are going to learn about Button control in C++/UWP.

Button Control

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

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

  1. BtnClick.Click(&App::BtnClick);  
  2. void App::BtnClick(IInspectable const & sender,const RoutedEventArgs &args)  
  3. {  
  4. }  
In the below sample application, the user clicks the button, and a message box will be shown.
  1. #include "pch.h"  
  2.   
  3. using namespace winrt;  
  4. using namespace Windows::ApplicationModel;  
  5. using namespace Windows::ApplicationModel::Activation;  
  6. using namespace Windows::Foundation;  
  7. using namespace Windows::UI;  
  8. using namespace Windows::UI::Xaml;  
  9. using namespace Windows::UI::Xaml::Controls;  
  10. using namespace Windows::UI::Xaml::Controls::Primitives;  
  11. using namespace Windows::UI::Xaml::Interop;  
  12. using namespace Windows::UI::Xaml::Media;  
  13. using namespace Windows::UI::Xaml::Navigation;  
  14. using namespace Windows::UI::Popups;  
  15. using namespace Windows::Storage;  
  16.   
  17. struct App:ApplicationT<App>  
  18. {  
  19. public:  
  20.     virtual ~App() = default;  
  21.     static void OnLaunched(LaunchActivatedEventArgs const&);  
  22.     static void BtnClick(IInspectable const & sender, const RoutedEventArgs &args);  
  23. };  
  24.   
  25. void App::OnLaunched(LaunchActivatedEventArgs const&)  
  26. {  
  27.   
  28.     Button BtnClick;  
  29.     IInspectable captionText = PropertyValue::CreateString(L"Click Me");  
  30.     BtnClick.Content(captionText);  
  31.   
  32.     //To change background and Foreground of the color  
  33.     BtnClick.Background(SolidColorBrush(Colors::Red()));  
  34.     BtnClick.Foreground(SolidColorBrush((Colors::Yellow())));  
  35.       
  36.     BtnClick.Click(&App::BtnClick);  
  37.       
  38.   
  39.     Window window = Window::Current();  
  40.     window.Content(BtnClick);  
  41.   
  42.     window.Activate();  
  43. }  
  44.   
  45. void App::BtnClick(IInspectable const & sender,const RoutedEventArgs &args)  
  46. {  
  47.       
  48.     MessageDialog msgDlg(L"Button Click Event",L"C++/UWP");  
  49.     msgDlg.ShowAsync();  
  50. }  
  51.   
  52. int __stdcall wWinMain(HINSTANCE,HINSTANCE,PWSTR,int)  
  53. {  
  54.     Application::Start([](auto &&) {make<App>(); });  
  55.     return 0;  
  56. }  
output
 
 

So far we have learned two sample applications, the below topic explains an overview of the program like pch.h file, WinMain, lets see what the role of that is.

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.


Similar Articles