Learn Universal Windows Programming Via Modern C++ (ProgressRing)

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

In this article, I am going to explain how to implement ProgressRing Control in Modern C++/WinRT.

ProgressRing control

ProgressRing is similar to the ProgressBar control. It indicates that some operation is going on in the background. ProgressBar contains the value indicator to show the completed percentage of the task while the ProgressRing just shows the indeterminate progress by displaying animated ring.

This control is mostly used in the background loading of the content on the web or in the database. The following UI shows the ProgressRing control ( just represents that the data's loading).
 
 
 
Property - IsActive

It has only one main property - set IsActive as true and the animation is started; set IsActive as false and the animation is stopped.
 
Note
  • true - Control is animated, and visible to the user
  • false - Control is hidden and the animation has stopped. 
Let's see one simple example
 
Concept - Add two buttons - Start and Stop. If the user presses the Start button, the animation gets started. And, when the Stop button is pressed, it stops the animation. Here is the code for the same.
  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::Navigation;  
  13. using namespace Windows::UI::Xaml::Media;  
  14. using namespace Windows::Media;  
  15. using namespace Windows::Media::Core;  
  16. using namespace Windows::Storage;  
  17. using namespace Windows::Storage::Pickers;  
  18.   
  19.   
  20. struct App :ApplicationT<App>  
  21. {  
  22.     TextBlock txtStatus;  
  23.       
  24.     ProgressRing proRing;  
  25.       
  26.     IInspectable CreateInspectable(hstring strCaption)  
  27.     {  
  28.         return PropertyValue::CreateString(strCaption);  
  29.     }  
  30.   
  31.     void BtnClick(IInspectable const & sender, const RoutedEventArgs &args)  
  32.     {  
  33.   
  34.         Button btnTag = sender.as<Button>();  
  35.   
  36.         auto demo = btnTag.Tag();  
  37.   
  38.         IPropertyValue tagId = btnTag.Tag().as<IPropertyValue>();  
  39.   
  40.         if (tagId.GetInt16() == 1)  
  41.         {  
  42.             proRing.IsActive(true);  
  43.         }  
  44.         else if (tagId.GetInt16() == 2)  
  45.         {  
  46.             proRing.IsActive(false);  
  47.         }  
  48.     }  
  49.   
  50.     TextBlock CreateTextBlock(hstring text)  
  51.     {  
  52.         TextBlock txtBlock;  
  53.         txtBlock.Text(text);  
  54.         txtBlock.FontFamily(FontFamily(L"Segoe UI Semibold"));  
  55.         txtBlock.TextAlignment(TextAlignment::Left);  
  56.         txtBlock.FontSize(35);  
  57.         txtBlock.Foreground(SolidColorBrush(Colors::Brown()));  
  58.         txtBlock.Margin(CreateThickness(10, 10, 0, 0));  
  59.         return txtBlock;  
  60.     }  
  61.   
  62.     Thickness CreateThickness(int left, int top, int right, int bottom)  
  63.     {  
  64.         Thickness thick;  
  65.         thick.Left = left;  
  66.         thick.Top = top;  
  67.         thick.Right = right;  
  68.         thick.Bottom = bottom;  
  69.         return thick;  
  70.     }  
  71.   
  72.   
  73.     void OnLaunched(LaunchActivatedEventArgs const&)  
  74.     {  
  75.   
  76.         Button BtnIncrease, BtnDecrease, BtnError;  
  77.         auto captionText = PropertyValue::CreateString(L"Start");  
  78.         BtnIncrease.Content(captionText);  
  79.         BtnIncrease.Click({ this,&App::BtnClick });  
  80.         BtnIncrease.Margin(CreateThickness(20, 10, 0, 0));  
  81.         BtnIncrease.Tag(PropertyValue::CreateInt32(1));  
  82.   
  83.         captionText = PropertyValue::CreateString(L"Stop");  
  84.         BtnDecrease.Content(captionText);  
  85.         BtnDecrease.Click({ this,&App::BtnClick });  
  86.         BtnDecrease.Margin(CreateThickness(20, 10, 0, 0));  
  87.         BtnDecrease.Tag(PropertyValue::CreateInt32(2));  
  88.   
  89.         auto txtheader = CreateTextBlock(L"ProgressRing Sample(Modern C++/WinRT)");  
  90.         txtheader.FontSize(25);  
  91.   
  92.         StackPanel btnPanel;  
  93.         btnPanel.Orientation(Orientation::Horizontal);  
  94.         btnPanel.Children().Append(BtnIncrease);  
  95.         btnPanel.Children().Append(BtnDecrease);  
  96.           
  97.   
  98.   
  99.         StackPanel sContentPanel;  
  100.         sContentPanel.Children().Append(btnPanel);  
  101.         sContentPanel.Orientation(Orientation::Vertical);  
  102.         sContentPanel.VerticalAlignment(VerticalAlignment::Top);  
  103.   
  104.         proRing.IsActive(true);  
  105.         proRing.Width(100);  
  106.         proRing.Height(100);  
  107.         proRing.Margin(CreateThickness(20, 10, 0, 0));  
  108.   
  109.         proRing.Background(SolidColorBrush(Colors::Yellow()));  
  110.         proRing.VerticalAlignment(VerticalAlignment::Center);  
  111.         proRing.HorizontalAlignment(HorizontalAlignment::Left);  
  112.   
  113.         StackPanel mainPanel;  
  114.         mainPanel.Children().Append(sContentPanel);  
  115.         mainPanel.Children().Append(txtheader);  
  116.         mainPanel.Children().Append(proRing);  
  117.   
  118.   
  119.   
  120.         Window window = Window::Current();  
  121.         window.Content(mainPanel);  
  122.         window.Activate();  
  123.   
  124.   
  125.     }  
  126. };  
  127.   
  128. int __stdcall wWinMain(HINSTANCEHINSTANCEPWSTRint)  
  129. {  
  130.     Application::Start([](auto &&) {make<App>(); });  
  131.     return 0;  
  132. }  
output
 

The sample code is given here.

Conclusion

I hope you understood how to use ProgressRing control.


Similar Articles