WinUI 3 C++ コントロール解説 – 浮動ポップアップ(Popup通知ウィンドウ)

まず Popup のデモを行います

XAML にボタンを追加し、クリックすると Popup を開くようにします。Popup 要素も記述し、初期状態は非表示、ボタンクリック後に表示させます。

    <StackPanel Spacing="24" VerticalAlignment="Top">

        <!-- 生のPopupルートコンテナ -->
        <Popup x:Name="MainPopup" Width="320" Height="180">
            <Border Background="White" BorderBrush="Gray" BorderThickness="1" CornerRadius="8" Padding="16">
                <StackPanel Spacing="12">
                    <TextBlock Text="カスタムポップアップコンテンツ" FontSize="18"/>
                    <TextBlock Text="これは標準Popup基底コンテナです"/>
                    <Button Content="ポップアップを閉じる" Click="BtnClosePopup_Click"/>
                </StackPanel>
            </Border>
        </Popup>

        <Button Content="標準Popupコンテナを開く" Click="BtnOpenPopup_Click" HorizontalAlignment="Left"
        ToolTipService.ToolTip="基底ポップアップレイヤーを開く"/>

    </StackPanel>Code language: HTML, XML (xml)

次にボタンクリックイベント BtnOpenPopup_Click を作成し、内部でPopupを表示します。<Popup x:Name="MainPopup" Width="320" Height="180"> ...</Popup> は浮動ウィンドウを定義し初期非表示。内部のボタン <Button Content="ポップアップを閉じる" Click="BtnClosePopup_Click"/> をクリックすると親Popupが閉じます。

F12キーを押し、2つのボタンイベントメソッドを自動生成します

.H ヘッダーファイル

#pragma once

#include "MainWindow.g.h"

namespace winrt::AppWinui::implementation
{
    struct MainWindow : MainWindowT<MainWindow>
    {
    public:
        MainWindow();

        int32_t MyProperty();
        void MyProperty(int32_t value);

        void BtnOpenPopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e);
        void BtnClosePopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e);
    };
}

namespace winrt::AppWinui::factory_implementation
{
    struct MainWindow : MainWindowT<MainWindow, implementation::MainWindow>
    {
    };
}
Code language: C++ (cpp)
#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Globalization.h>

#include <winrt/Windows.UI.h> //Windows::UI::Colors


// グローバル名前空間エイリアス、クラス未検出・RoutedEventArgs未定義エラー解消
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Controls::Primitives;


// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace winrt::AppWinui::implementation
{
    int32_t MainWindow::MyProperty()
    {
        throw hresult_not_implemented();
    }

    void MainWindow::MyProperty(int32_t /* value */)
    {
        throw hresult_not_implemented();
    }

    MainWindow::MainWindow()
    {
        InitializeComponent();
    }
}


void winrt::AppWinui::implementation::MainWindow::BtnOpenPopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    MainPopup().IsOpen(true);
}

void winrt::AppWinui::implementation::MainWindow::BtnClosePopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    MainPopup().IsOpen(false);
}
Code language: C++ (cpp)

WinUI 3 C++ コントロール解説 – 浮動ポップアップ(Popup通知ウィンドウ)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です