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

Before reading this article, I highly recommend reading the previous parts of the series.

In this article, we are going to learn about Border control in Modern C++

Border

Border is a container control; this control is used to draw the border of another control.

Properties

BorderThickness: This property is used to set the thickness of the border,.BorderBrush is used to the set the color of the BorderThickness, and background property is used to change the background color of the border.

A border can contain only one child object,. This property is used the make the border of another control

Here is sample code that shows you how to use the Border control 
  1.         Border border;    
  2.         border.Margin(CreateThickness(15, 15, 15, 15));    
  3.         border.BorderThickness(CreateThickness(1,1, 1, 1));    
  4.         border.BorderBrush(SolidColorBrush(Colors::Red()));    
  5.         border.Background(SolidColorBrush(Colors::Brown()));       
  6.             
  7.         border.Child(panel1);  
ex: stackpanel is added into the border control 
 
 
 

CornerRadius

CornerRadius property is used to the make the corners rounded. BottomLeft , BottomRight , TopLeft , TopRight properties are used to set the CornerRadius of the Border control.
 
Sample code 
  1. CornerRadius cornerRadius;    
  2.         cornerRadius.BottomLeft = 20;    
  3.         cornerRadius.BottomRight = 20;    
  4.         cornerRadius.TopLeft = 15;    
  5.         cornerRadius.TopRight = 15;    
  6.         borderLeft.CornerRadius(cornerRadius);    
  7.      
 
 
The below sample explains how to add the stackpanel into the Border control.
  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.     StackPanel sPanel;  
  22.   
  23.     IInspectable MakeString(hstring captionText)  
  24.     {  
  25.         return PropertyValue::CreateString(captionText);  
  26.     }  
  27.   
  28.     Thickness CreateThickness(int top,int bottom,int left,int right)  
  29.     {  
  30.         Thickness margin;  
  31.         margin.Bottom = bottom;  
  32.         margin.Left = left;  
  33.         margin.Right = right;  
  34.         margin.Top = top;  
  35.         return margin;  
  36.     }  
  37.   
  38.     TextBlock CreateTextBlock(hstring textCaption)  
  39.     {  
  40.         TextBlock text;  
  41.         text.Text(textCaption);  
  42.         text.TextAlignment(TextAlignment::Center);  
  43.         text.Margin(CreateThickness(10, 10, 0, 10));  
  44.         return text;  
  45.     }  
  46.   
  47.     StackPanel CreateStackPanel(hstring panelName, Orientation oriPosition)  
  48.     {  
  49.         StackPanel stPanel;  
  50.         stPanel.Children().Append(CreateTextBlock(panelName));  
  51.         stPanel.Children().Append(CreateTextBlock(L"Panel 1"));  
  52.         stPanel.Children().Append(CreateTextBlock(L"Panel 2"));  
  53.         stPanel.Children().Append(CreateTextBlock(L"Panel 3"));  
  54.         stPanel.Children().Append(CreateTextBlock(L"Panel 4"));  
  55.         stPanel.Children().InsertAt(4, CreateTextBlock(L"Panel 5"));  
  56.         stPanel.Children().InsertAt(5, CreateTextBlock(L"Panel 6"));  
  57.         stPanel.Orientation(oriPosition);  
  58.         return stPanel;  
  59.     }  
  60.   
  61.   
  62.     void OnLaunched(LaunchActivatedEventArgs const&)  
  63.     {  
  64.   
  65.         auto panel1 = CreateStackPanel(L"StackPanel 1", Orientation::Horizontal);  
  66.         auto panel2 = CreateStackPanel(L"StackPanel 2", Orientation::Vertical);  
  67.   
  68.         Border border;  
  69.         border.Margin(CreateThickness(15, 15, 15, 15));  
  70.         border.BorderThickness(CreateThickness(1,1, 1, 1));  
  71.         border.BorderBrush(SolidColorBrush(Colors::Red()));  
  72.         border.Background(SolidColorBrush(Colors::Brown()));  
  73.   
  74.           
  75.         border.Child(panel1);  
  76.   
  77.         Border borderLeft;  
  78.         borderLeft.Margin(CreateThickness(15, 15, 15, 15));  
  79.         borderLeft.BorderThickness(CreateThickness(10, 10, 10, 10));  
  80.         borderLeft.BorderBrush(SolidColorBrush(Colors::Red()));  
  81.         borderLeft.Child(panel2);  
  82.         CornerRadius cornerRadius;  
  83.         cornerRadius.BottomLeft = 20;  
  84.         cornerRadius.BottomRight = 20;  
  85.         cornerRadius.TopLeft = 15;  
  86.         cornerRadius.TopRight = 15;  
  87.         borderLeft.CornerRadius(cornerRadius);  
  88.   
  89.         StackPanel panel;  
  90.         panel.Children().Append(border);  
  91.         panel.Children().Append(borderLeft);  
  92.         panel.BorderThickness(CreateThickness(5, 5, 5, 5));  
  93.         panel.Background(SolidColorBrush(Colors::Yellow()));  
  94.           
  95.         auto window = Window::Current();  
  96.         window.Content(panel);  
  97.   
  98.         window.Activate();  
  99.     }  
  100. };  
  101.   
  102.   
  103. int __stdcall wWinMain(HINSTANCEHINSTANCEPWSTRint)  
  104. {  
  105.     Application::Start([](auto &&) {make<App>(); });  
  106.     return 0;  
  107. }  
output

Conclusion

I hope you understood how to use the Border control.

Up Next
    Ebook Download
    View all
    Learn
    View all