Skip to content

Commit

Permalink
feat: 在原位重新创建主窗口
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Feb 15, 2024
1 parent 20e12bf commit 9c2a053
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
25 changes: 21 additions & 4 deletions src/XamlIslandsCpp/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ bool MainWindow::Create(
HINSTANCE hInstance,
winrt::AppTheme theme,
winrt::WindowBackdrop backdrop,
bool isCustomTitleBarEnabled
bool isCustomTitleBarEnabled,
const WINDOWPLACEMENT* wp
) noexcept {
static const int _ = [](HINSTANCE hInstance) {
WNDCLASSEXW wcex{
Expand All @@ -37,6 +38,9 @@ bool MainWindow::Create(
}(hInstance);

_SetInitialTheme(theme, backdrop, isCustomTitleBarEnabled);
if (wp && wp->showCmd == SW_SHOWMAXIMIZED) {
_SetInitialMaximized();
}

CreateWindowEx(
Win32Helper::GetOSVersion().Is22H2OrNewer() && backdrop != winrt::WindowBackdrop::SolidColor ? WS_EX_NOREDIRECTIONBITMAP : 0,
Expand Down Expand Up @@ -64,9 +68,22 @@ bool MainWindow::Create(
SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOCOPYBITS);

// Xaml 控件加载完成后显示主窗口
Content().Loaded([this](winrt::IInspectable const&, winrt::RoutedEventArgs const&) {
ShowWindow(Handle(), SW_SHOWNORMAL);
});
if (wp) {
Content().Loaded([this, wp(*wp)](winrt::IInspectable const&, winrt::RoutedEventArgs const&) {
// 禁用显示窗口的动画
BOOL value = TRUE;
DwmSetWindowAttribute(Handle(), DWMWA_TRANSITIONS_FORCEDISABLED, &value, sizeof(value));

SetWindowPlacement(Handle(), &wp);

value = FALSE;
DwmSetWindowAttribute(Handle(), DWMWA_TRANSITIONS_FORCEDISABLED, &value, sizeof(value));
});
} else {
Content().Loaded([this](winrt::IInspectable const&, winrt::RoutedEventArgs const&) {
ShowWindow(Handle(), SW_SHOWNORMAL);
});
}

// 创建标题栏窗口,它是主窗口的子窗口。我们将它置于 XAML Islands 窗口之上以防止鼠标事件被吞掉
//
Expand Down
3 changes: 2 additions & 1 deletion src/XamlIslandsCpp/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class MainWindow : public XamlWindowT<MainWindow, winrt::XamlIslandsCpp::App::Ro
HINSTANCE hInstance,
winrt::XamlIslandsCpp::App::AppTheme theme,
winrt::XamlIslandsCpp::App::WindowBackdrop backdrop,
bool isCustomTitleBarEnabled
bool isCustomTitleBarEnabled,
const WINDOWPLACEMENT* wp = nullptr
) noexcept;

bool SetTheme(winrt::XamlIslandsCpp::App::AppTheme theme, winrt::XamlIslandsCpp::App::WindowBackdrop backdrop) noexcept;
Expand Down
19 changes: 15 additions & 4 deletions src/XamlIslandsCpp/XamlApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ int XamlApp::Run() {
return _mainWindow.MessageLoop();
}

bool XamlApp::_CreateMainWindow() noexcept {
bool XamlApp::_CreateMainWindow(const WINDOWPLACEMENT* wp) noexcept {
winrt::Settings settings = _uwpApp.Settings();

if (!_mainWindow.Create(
_hInstance,
settings.Theme(),
settings.Backdrop(),
settings.IsCustomTitleBarEnabled()
settings.IsCustomTitleBarEnabled(),
wp
)) {
return false;
}
Expand All @@ -66,12 +67,22 @@ bool XamlApp::_CreateMainWindow() noexcept {
[this](winrt::IInspectable const& sender, winrt::WindowBackdrop backdrop) {
winrt::Settings settings = sender.as<winrt::Settings>();
if (_mainWindow.SetTheme(settings.Theme(), backdrop)) {
// 由于无法更改 WS_EX_NOREDIRECTIONBITMAP 样式,必须重新创建主窗口
shouldQuit = false;

WINDOWPLACEMENT wp{ .length = sizeof(wp) };
GetWindowPlacement(_mainWindow.Handle(), &wp);

// 禁用关闭窗口的动画
BOOL value = TRUE;
DwmSetWindowAttribute(
_mainWindow.Handle(), DWMWA_TRANSITIONS_FORCEDISABLED, &value, sizeof(value));

winrt::CoreDispatcher dispatcher = _mainWindow.Content().Dispatcher();
_mainWindow.Destroy();
dispatcher.TryRunAsync(winrt::CoreDispatcherPriority::Normal, [this]() {
dispatcher.TryRunAsync(winrt::CoreDispatcherPriority::Normal, [this, wp]() {
shouldQuit = true;
_CreateMainWindow();
_CreateMainWindow(&wp);
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/XamlIslandsCpp/XamlApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class XamlApp {
private:
XamlApp() = default;

bool _CreateMainWindow() noexcept;
bool _CreateMainWindow(const WINDOWPLACEMENT* wp = nullptr) noexcept;

void _MainWindow_Destoryed();

Expand All @@ -33,6 +33,7 @@ class XamlApp {
winrt::XamlIslandsCpp::App::Settings::IsCustomTitleBarEnabledChanged_revoker _isCustomTitleBarEnabledChangedRevoker;
winrt::XamlIslandsCpp::App::Settings::BackdropChanged_revoker _backdropChangedRevoker;

// 防止重新主窗口时退出
bool shouldQuit = true;
};

Expand Down
6 changes: 6 additions & 0 deletions src/XamlIslandsCpp/XamlWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ class XamlWindowT {
);
}

// 窗口尚未显示无法最大化,通过这个方法设置 _isMaximized 使 _UpdateIslandPosition 估计 XAML Islands 窗口尺寸。
// 否则在显示窗口时可能会看到 NavigationView 的导航栏的展开动画。
void _SetInitialMaximized() noexcept {
_isMaximized = true;
}

void _SetInitialTheme(
winrt::XamlIslandsCpp::App::AppTheme theme,
winrt::XamlIslandsCpp::App::WindowBackdrop backdrop,
Expand Down

0 comments on commit 9c2a053

Please sign in to comment.