- What I get from m_mainWindow.MyBut().ActualOffset() or m_mainWindow.MyBut().TransformToVisual() is 0. The value of the coordinate attribute will only change when the window changes.
- I tried the link that has properties that may provide coordinates https://learn.microsoft.com/zh-cn/uwp/api/windows.ui.xaml.controls.button?view=winrt-22621
- How do I get the coordinates of this element? I saw an example in C# that can get the coordinates like this. I don’t know if it’s a problem with C++/WinRT WinUI.
#include "pch.h"
#include "TitlebarPage.xaml.h"
#if __has_include("TitlebarPage.g.cpp")
#include "TitlebarPage.g.cpp"
#endif
using namespace winrt;
using namespace Windows::UI;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Navigation;
using namespace Microsoft::UI::Input;
namespace winrt::SampleApp::implementation
{
winrt::hstring defaultTitle = L"WinUI C++ Desktop sample App";
TitlebarPage::TitlebarPage()
{
InitializeComponent();
TitlebarCustomBtn().PointerPressed({this,
&TitlebarPage::TitlebarCustomBtn_Click});
TitlebarCustomBtn().PointerPressed({ this,
&TitlebarPage::TitlebarCustomBtn_Click });
}
void TitlebarPage::OnNavigatedTo(NavigationEventArgs const& e)
{
m_mainWindow = e.Parameter().as<MainWindow>();
AppWindow appWindow = m_mainWindow.MyAppWindow();
m_appWindow = appWindow;
m_changedToken = m_appWindow.Changed({ this,
&TitlebarPage::AppWindowChangedHandler });
}
void TitlebarPage::AppWindowChangedHandler(winrt::Windows::Foundation::IInspectable
const& sender, winrt::Microsoft::UI::Windowing::AppWindowChangedEventArgs const& e)
{
if (e.DidSizeChange()
&& sender.as<AppWindow>().TitleBar().ExtendsContentIntoTitleBar())
{
SetCustomTitleBarDragRegion();
}
}
void TitlebarPage::TitlebarBrandingBtn_Click(
winrt::Windows::Foundation::IInspectable const& /*sender*/,
winrt::Microsoft::UI::Xaml::RoutedEventArgs const& /*e*/)
{
m_appWindow.TitleBar().ResetToDefault();
m_mainWindow.MyTitleBar().
Visibility(winrt::Microsoft::UI::Xaml::Visibility::Collapsed);
m_brandTitleBar = !m_brandTitleBar;
if (AppWindowTitleBar::IsCustomizationSupported() && m_brandTitleBar)
{
m_appWindow.Title(L"Default titlebar with custom color customization");
m_appWindow.TitleBar().ForegroundColor(Colors::Transparent());
m_appWindow.TitleBar().BackgroundColor(Colors::Transparent());
m_appWindow.TitleBar().InactiveBackgroundColor(Colors::Transparent());
m_appWindow.TitleBar().InactiveForegroundColor(Colors::Transparent());
//Buttons
m_appWindow.TitleBar().ButtonBackgroundColor(Colors::Transparent());
m_appWindow.TitleBar().ButtonForegroundColor(Colors::Transparent());
m_appWindow.TitleBar().
ButtonInactiveBackgroundColor(Colors::Transparent());
m_appWindow.TitleBar().
ButtonInactiveForegroundColor(Colors::Transparent());
m_appWindow.TitleBar().ButtonHoverBackgroundColor(Colors::Transparent());
m_appWindow.TitleBar().ButtonHoverForegroundColor(Colors::Transparent());
m_appWindow.TitleBar().ButtonPressedBackgroundColor(Colors::Transparent());
m_appWindow.TitleBar().ButtonPressedForegroundColor(Colors::Transparent());
}
else
{
m_appWindow.TitleBar().ResetToDefault();
m_appWindow.Title(defaultTitle);
}
}
void TitlebarPage::TitlebarCustomBtn_Click(winrt::Windows::Foundation::IInspectable
const& /*sender*/, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& /*e*/)
{
if (AppWindowTitleBar::IsCustomizationSupported() && !m_customTitleBar) {
m_customTitleBar = true;
m_appWindow.TitleBar().ExtendsContentIntoTitleBar(true);
// Show the custom titlebar
m_mainWindow.MyTitleBar().
Visibility(winrt::Microsoft::UI::Xaml::Visibility::Visible);
// Set Button colors to match the custom titlebar
m_appWindow.TitleBar().ButtonBackgroundColor(Colors::Transparent());
m_appWindow.TitleBar().ButtonForegroundColor(Colors::Gray());
m_appWindow.TitleBar().
ButtonInactiveBackgroundColor(Colors::Transparent());
m_appWindow.TitleBar().ButtonInactiveForegroundColor(Colors::Gray());
m_appWindow.TitleBar().ButtonHoverBackgroundColor(Colors::Transparent());
m_appWindow.TitleBar().ButtonHoverForegroundColor(Colors::Transparent());
m_appWindow.TitleBar().ButtonPressedBackgroundColor(Colors::Transparent());
m_appWindow.TitleBar().ButtonPressedForegroundColor(Colors::Gray());
m_appWindow.TitleBar().
IconShowOptions(IconShowOptions::HideIconAndSystemMenu);
//Set the drag region for the custom titlebar
SetCustomTitleBarDragRegion();
}
else
{
// Bring back the default titlebar
m_customTitleBar = false;
m_mainWindow.MyTitleBar().
Visibility(winrt::Microsoft::UI::Xaml::Visibility::Collapsed);
m_appWindow.TitleBar().ResetToDefault();
}
}
void TitlebarPage::ResetTitlebarBtn_Click(winrt::Windows::Foundation::IInspectable
const& /*sender*/, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& /*e*/)
{
m_mainWindow.MyTitleBar().Visibility(
winrt::Microsoft::UI::Xaml::Visibility::Collapsed);
m_appWindow.TitleBar().ResetToDefault();
m_appWindow.Title(defaultTitle);
}
void TitlebarPage::SetCustomTitleBarDragRegion()
{
double scaleAdjustment =
m_mainWindow.MyTitleBar().XamlRoot().RasterizationScale();
double scaleAdjustment2 =m_mainWindow.MyBut().XamlRoot().RasterizationScale();
GeneralTransform transform = m_mainWindow.MyBut().TransformToVisual(nullptr);
static Rect bunds;
bunds = transform.TransformBounds(Rect(0, 0,
m_mainWindow.MyBut().ActualWidth(),
m_mainWindow.MyBut().ActualHeight()));
std::vector<Windows::Graphics::RectInt32> dragRects;
Windows::Graphics::RectInt32 MyButRect;
MyButRect.X = static_cast<int>(bunds.X * scaleAdjustment);
MyButRect.Y = static_cast<int>(bunds.Y * scaleAdjustment);
MyButRect.Height = static_cast<int>(bunds.Height * scaleAdjustment);
MyButRect.Width = static_cast<int>(bunds.Width * scaleAdjustment);
dragRects.push_back(MyButRect);
InputNonClientPointerSource nonClientInputSrc =
InputNonClientPointerSource::GetForWindowId(m_appWindow.Id());
nonClientInputSrc.SetRegionRects(NonClientRegionKind::Passthrough, dragRects);
}
}