1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
Dismiss Notice
Welcome to our Education website, plz like our page facebook to support us. Thank You and wish you good navigation

Sample code to implement RelativePanel in Modern C++ / WinRT

abdelouafiNov 21, 2017

    1. abdelouafi

      abdelouafi Administrator Staff Member

      Messages:
      402
      Likes Received:
      9
      Trophy Points:
      18
      Joined
      Sep 13, 2016
      Code:
      #include "pch.h" 
       
      using namespace winrt; 
      using namespace Windows::ApplicationModel; 
      using namespace Windows::ApplicationModel::Activation; 
      using namespace Windows::Foundation; 
      using namespace Windows::UI; 
      using namespace Windows::UI::Xaml; 
      using namespace Windows::UI::Xaml::Controls; 
      using namespace Windows::UI::Xaml::Controls::Primitives; 
      using namespace Windows::UI::Xaml::Interop; 
      using namespace Windows::UI::Xaml::Navigation; 
      using namespace Windows::UI::Xaml::Media; 
      using namespace Windows::Media; 
      using namespace Windows::Storage; 
       
      struct App :ApplicationT<App> 
      { 
      public: 
          virtual ~App() = default; 
          static TextBlock CreateTextBlock(hstring); 
          static void OnLaunched(LaunchActivatedEventArgs const&); 
          static Thickness CreateThickness(int bottom, int left, int right, int top); 
      }; 
      Thickness App::CreateThickness(int bottom, int left, int right, int top) 
      { 
          Thickness think; 
          think.Bottom = bottom; 
          think.Left = left; 
          think.Right = right; 
          think.Top = top; 
          return think; 
      } 
      TextBlock App::CreateTextBlock(hstring textCaption) 
      { 
          TextBlock text; 
          text.Text(textCaption); 
          text.TextAlignment(TextAlignment::Center); 
          text.Margin(CreateThickness(10, 10, 0, 10)); 
          return text; 
      } 
       
      void App::OnLaunched(LaunchActivatedEventArgs const&) 
      { 
           
          auto txtTop = CreateTextBlock(L"AlignTopWithPanel"); 
          txtTop.Margin(CreateThickness(0, 10, 0, 0)); 
          auto txtBottom = CreateTextBlock(L"AlignBottomWithPanel"); 
          txtBottom.Margin(CreateThickness(0, 10, 0, 0)); 
          auto txtLeft = CreateTextBlock(L"AlignLeftWithPanel"); 
          txtLeft.Margin(CreateThickness(10, 0, 5, 50)); 
          auto txtRight = CreateTextBlock(L"AlignRightWithPanel"); 
       
          auto txtHorCenter = CreateTextBlock(L"AlignHorizontalCenterWithPanel"); 
          auto txtVertical = CreateTextBlock(L"AlignVerticalCenterWithPanel"); 
          auto txtRightOf = CreateTextBlock(L"RightOfControl"); 
          txtRightOf.Foreground(SolidColorBrush(Colors::Red())); 
       
          auto txtLeftOf = CreateTextBlock(L"LeftOfControl"); 
          txtLeftOf.Foreground(SolidColorBrush(Colors::Blue())); 
       
          auto txtAbove = CreateTextBlock(L"AboveOfControl"); 
          txtAbove.Foreground(SolidColorBrush(Colors::Blue())); 
           
          auto txtBelow = CreateTextBlock(L"BelowOfControl"); 
          txtBelow.Foreground(SolidColorBrush(Colors::Green())); 
       
          auto txtHeader = CreateTextBlock(L"Modern C++/WinRT"); 
          txtHeader.Foreground(SolidColorBrush(Colors::Red())); 
          txtHeader.HorizontalAlignment(HorizontalAlignment::Center); 
          txtHeader.VerticalAlignment(VerticalAlignment::Center); 
          txtHeader.Margin(CreateThickness(0, 10, 5, 100)); 
          txtHeader.FontSize(25); 
           
          RelativePanel RPanel; 
          RPanel.Children().Append(txtTop); 
          RPanel.Children().Append(txtBottom); 
          RPanel.Children().Append(txtLeft); 
          RPanel.Children().Append(txtRight); 
          RPanel.Children().Append(txtHorCenter); 
          RPanel.Children().Append(txtVertical); 
          RPanel.Children().Append(txtRightOf); 
          RPanel.Children().Append(txtLeftOf); 
          RPanel.Children().Append(txtAbove); 
          RPanel.Children().Append(txtBelow); 
          RPanel.Children().Append(txtHeader); 
       
       
          RPanel.SetAlignTopWithPanel(txtTop,true); 
          RPanel.SetAlignBottomWithPanel(txtBottom, true); 
          RPanel.SetAlignLeftWithPanel(txtLeft, true); 
          RPanel.SetAlignRightWithPanel(txtRight, true); 
       
          RPanel.SetAlignVerticalCenterWithPanel(txtVertical, true); 
          RPanel.SetAlignHorizontalCenterWithPanel(txtHorCenter,true); 
          RPanel.SetRightOf(txtRightOf, PropertyValue::CreateInspectable(txtHorCenter)); 
          RPanel.SetLeftOf(txtLeftOf, PropertyValue::CreateInspectable(txtHorCenter)); 
          RPanel.SetAbove(txtAbove, PropertyValue::CreateInspectable(txtVertical)); 
          RPanel.SetBelow(txtBelow, PropertyValue::CreateInspectable(txtVertical)); 
       
          RPanel.SetRightOf(txtHeader, PropertyValue::CreateInspectable(txtVertical)); 
           
          RPanel.Margin(CreateThickness(10, 10, 10, 10)); 
          RPanel.BorderThickness(CreateThickness(1, 1, 1, 1)); 
          RPanel.BorderBrush(SolidColorBrush(Colors::Blue())); 
       
          Window window = Window::Current(); 
          window.Content(RPanel); 
          window.Activate(); 
      } 
      int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) 
      { 
          Application::Start([](auto &&) {make<App>(); }); 
          return 0; 
      } 
      Panel3.png
       
      Loading...

Share This Page

Share