Skip to content

Commit

Permalink
Fix win32 classic menu theme display bug
Browse files Browse the repository at this point in the history
Fix win32 classic menu theme display bug
  • Loading branch information
Gaoyifei1011 committed Jun 3, 2024
1 parent 03d1c70 commit 67a7306
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 86 deletions.
Binary file modified GetStoreApp/GetStoreApp.res
Binary file not shown.
6 changes: 3 additions & 3 deletions GetStoreApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
[assembly: AssemblyCompany("高怡飞")]
[assembly: AssemblyCopyright("Copyright ©2022-2024 高怡飞, All Rights Reserved.")]
[assembly: AssemblyDescription("获取商店应用")]
[assembly: AssemblyFileVersion("4.0.602.0")]
[assembly: AssemblyInformationalVersion("4.0.602.0")]
[assembly: AssemblyFileVersion("4.0.603.0")]
[assembly: AssemblyInformationalVersion("4.0.603.0")]
[assembly: AssemblyProduct("获取商店应用")]
[assembly: AssemblyTitle("获取商店应用")]
[assembly: AssemblyVersion("4.0.602.0")]
[assembly: AssemblyVersion("4.0.603.0")]

// 应用程序默认区域性的资源控制器设置
[assembly: NeutralResourcesLanguage("en-us")]
Expand Down
31 changes: 26 additions & 5 deletions GetStoreApp/Views/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using GetStoreApp.Views.Pages;
using GetStoreApp.WindowsAPI.PInvoke.Comctl32;
using GetStoreApp.WindowsAPI.PInvoke.User32;
using GetStoreApp.WindowsAPI.PInvoke.Uxtheme;
using Microsoft.UI;
using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Content;
Expand All @@ -37,6 +38,7 @@
using Windows.System;
using Windows.UI;
using Windows.UI.StartScreen;

using WinRT.Interop;

// 抑制 IDE0060 警告
Expand Down Expand Up @@ -150,9 +152,10 @@ public MainWindow()
IsWindowMaximized = overlappedPresenter.State is OverlappedPresenterState.Maximized;
contentCoordinateConverter = ContentCoordinateConverter.CreateForWindowId(AppWindow.Id);

// 标题栏设置
// 标题栏和右键菜单设置
SetTitleBar(AppTitlebar);
SetTitleBarColor((Content as FrameworkElement).ActualTheme);
SetTitleBarTheme((Content as FrameworkElement).ActualTheme);
SetClassicMenuTheme((Content as FrameworkElement).ActualTheme);

// 挂载相应的事件
AppWindow.Changed += OnAppWindowChanged;
Expand Down Expand Up @@ -392,7 +395,8 @@ private void OnCloseClicked(object sender, RoutedEventArgs args)
/// </summary>
private void OnActualThemeChanged(FrameworkElement sender, object args)
{
SetTitleBarColor(sender.ActualTheme);
SetTitleBarTheme(sender.ActualTheme);
SetClassicMenuTheme(sender.ActualTheme);
}

/// <summary>
Expand Down Expand Up @@ -683,9 +687,9 @@ private void SetSystemBackdrop()
}

/// <summary>
/// 设置标题栏按钮的颜色
/// 设置标题栏按钮的主题色
/// </summary>
private void SetTitleBarColor(ElementTheme theme)
private void SetTitleBarTheme(ElementTheme theme)
{
AppWindowTitleBar titleBar = AppWindow.TitleBar;

Expand Down Expand Up @@ -716,6 +720,23 @@ private void SetTitleBarColor(ElementTheme theme)
}
}

/// <summary>
/// 设置传统菜单标题栏按钮的主题色
/// </summary>
private static void SetClassicMenuTheme(ElementTheme theme)
{
if (theme is ElementTheme.Light)
{
UxthemeLibrary.SetPreferredAppMode(PreferredAppMode.ForceLight);
}
else
{
UxthemeLibrary.SetPreferredAppMode(PreferredAppMode.ForceDark);
}

UxthemeLibrary.FlushMenuThemes();
}

/// <summary>
/// 显示窗口
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions GetStoreApp/WindowsAPI/PInvoke/Uxtheme/PreferredAppMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace GetStoreApp.WindowsAPI.PInvoke.Uxtheme
{
/// <summary>
/// 菜单样式
/// </summary>
public enum PreferredAppMode
{
Default,
AllowDark,
ForceDark,
ForceLight,
Max
};
}
25 changes: 25 additions & 0 deletions GetStoreApp/WindowsAPI/PInvoke/Uxtheme/UxthemeLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Runtime.InteropServices;

namespace GetStoreApp.WindowsAPI.PInvoke.Uxtheme
{
/// <summary>
/// Uxtheme.dll 函数库
/// </summary>
public static partial class UxthemeLibrary
{
private const string Uxtheme = "uxtheme.dll";

/// <summary>
/// 设置 win32 右键菜单的样式
/// </summary>
/// <param name="preferredAppMode">菜单样式</param>
[LibraryImport(Uxtheme, EntryPoint = "#135", SetLastError = false)]
public static partial void SetPreferredAppMode(PreferredAppMode preferredAppMode);

/// <summary>
/// 刷新右键菜单样式
/// </summary>
[LibraryImport(Uxtheme, EntryPoint = "#136", SetLastError = false)]
public static partial void FlushMenuThemes();
}
}
134 changes: 67 additions & 67 deletions GetStoreAppPackage/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,78 @@
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="com uap uap5 rescap">

<Identity
Name="Gaoyifei1011.GetStoreApp"
Publisher="CN=高怡飞"
Version="4.0.602.0" />
<Identity
Name="Gaoyifei1011.GetStoreApp"
Publisher="CN=高怡飞"
Version="4.0.603.0" />

<Properties>
<DisplayName>ms-resource:PackageDisplayName</DisplayName>
<PublisherDisplayName>高怡飞</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Properties>
<DisplayName>ms-resource:PackageDisplayName</DisplayName>
<PublisherDisplayName>高怡飞</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>

<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.19041.0" MaxVersionTested="10.0.26100.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.19041.0" MaxVersionTested="10.0.26100.0" />
</Dependencies>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.19041.0" MaxVersionTested="10.0.26100.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.19041.0" MaxVersionTested="10.0.26100.0" />
</Dependencies>

<Resources>
<Resource Language="EN-US" />
<Resource Language="ZH-HANS-CN" />
</Resources>
<Resources>
<Resource Language="EN-US" />
<Resource Language="ZH-HANS-CN" />
</Resources>

<Applications>
<Application Id="GetStoreApp"
Executable="GetStoreApp.exe"
EntryPoint="Windows.FullTrustApplication">
<uap:VisualElements
DisplayName="ms-resource:AppDisplayName"
Description="ms-resource:AppDescription"
BackgroundColor="transparent"
Square150x150Logo="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png">
<uap:DefaultTile ShortName="ms-resource:AppShortDisplayName" Wide310x150Logo="Assets\Wide310x150Logo.png" Square71x71Logo="Assets\SmallTile.png" Square310x310Logo="Assets\LargeTile.png">
<uap:ShowNameOnTiles>
<uap:ShowOn Tile="wide310x150Logo" />
<uap:ShowOn Tile="square310x310Logo" />
<uap:ShowOn Tile="square150x150Logo" />
</uap:ShowNameOnTiles>
</uap:DefaultTile>
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
<Extensions>
<uap:Extension Category="windows.protocol" Executable="GetStoreApp.exe" EntryPoint="GetStoreApp.App">
<uap:Protocol Name="taskbarpinner">
<uap:DisplayName>ms-resource:TaskbarProtocolName</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
<uap:Extension Category="windows.protocol" Executable="GetStoreAppWebView.exe" EntryPoint="Windows.FullTrustApplication">
<uap:Protocol Name="webbrowser">
<uap:DisplayName>ms-resource:WebBrowserProtocolName</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
<Applications>
<Application Id="GetStoreApp"
Executable="GetStoreApp.exe"
EntryPoint="Windows.FullTrustApplication">
<uap:VisualElements
DisplayName="ms-resource:AppDisplayName"
Description="ms-resource:AppDescription"
BackgroundColor="transparent"
Square150x150Logo="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png">
<uap:DefaultTile ShortName="ms-resource:AppShortDisplayName" Wide310x150Logo="Assets\Wide310x150Logo.png" Square71x71Logo="Assets\SmallTile.png" Square310x310Logo="Assets\LargeTile.png">
<uap:ShowNameOnTiles>
<uap:ShowOn Tile="wide310x150Logo" />
<uap:ShowOn Tile="square310x310Logo" />
<uap:ShowOn Tile="square150x150Logo" />
</uap:ShowNameOnTiles>
</uap:DefaultTile>
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
<Extensions>
<uap:Extension Category="windows.protocol" Executable="GetStoreApp.exe" EntryPoint="GetStoreApp.App">
<uap:Protocol Name="taskbarpinner">
<uap:DisplayName>ms-resource:TaskbarProtocolName</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
<uap:Extension Category="windows.protocol" Executable="GetStoreAppWebView.exe" EntryPoint="Windows.FullTrustApplication">
<uap:Protocol Name="webbrowser">
<uap:DisplayName>ms-resource:WebBrowserProtocolName</uap:DisplayName>
</uap:Protocol>
</uap:Extension>

<uap:Extension Category="windows.shareTarget" Executable="GetStoreApp.exe" EntryPoint="Windows.FullTrustApplication">
<uap:ShareTarget>
<uap:DataFormat>Uri</uap:DataFormat>
</uap:ShareTarget>
</uap:Extension>
<uap:Extension Category="windows.shareTarget" Executable="GetStoreApp.exe" EntryPoint="Windows.FullTrustApplication">
<uap:ShareTarget>
<uap:DataFormat>Uri</uap:DataFormat>
</uap:ShareTarget>
</uap:Extension>

<uap5:Extension Category="windows.appExecutionAlias" Executable="GetStoreApp.exe" EntryPoint="Windows.FullTrustApplication">
<uap5:AppExecutionAlias>
<uap5:ExecutionAlias Alias="GetStoreApp.exe" />
</uap5:AppExecutionAlias>
</uap5:Extension>
</Extensions>
</Application>
</Applications>
<uap5:Extension Category="windows.appExecutionAlias" Executable="GetStoreApp.exe" EntryPoint="Windows.FullTrustApplication">
<uap5:AppExecutionAlias>
<uap5:ExecutionAlias Alias="GetStoreApp.exe" />
</uap5:AppExecutionAlias>
</uap5:Extension>
</Extensions>
</Application>
</Applications>

<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="broadFileSystemAccess" />
<rescap:Capability Name="packageManagement" />
<rescap:Capability Name="packageQuery" />
<rescap:Capability Name="runFullTrust" />
</Capabilities>
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="broadFileSystemAccess" />
<rescap:Capability Name="packageManagement" />
<rescap:Capability Name="packageQuery" />
<rescap:Capability Name="runFullTrust" />
</Capabilities>
</Package>
Binary file modified GetStoreAppWebView/GetStoreAppWebView.res
Binary file not shown.
6 changes: 3 additions & 3 deletions GetStoreAppWebView/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
[assembly: AssemblyCompany("高怡飞")]
[assembly: AssemblyCopyright("Copyright ©2022-2024 高怡飞, All Rights Reserved.")]
[assembly: AssemblyDescription("网页浏览器")]
[assembly: AssemblyFileVersion("4.0.602.0")]
[assembly: AssemblyInformationalVersion("4.0.602.0")]
[assembly: AssemblyFileVersion("4.0.603.0")]
[assembly: AssemblyInformationalVersion("4.0.603.0")]
[assembly: AssemblyProduct("网页浏览器")]
[assembly: AssemblyTitle("网页浏览器")]
[assembly: AssemblyVersion("4.0.602.0")]
[assembly: AssemblyVersion("4.0.603.0")]

// 应用程序默认区域性的资源控制器设置
[assembly: NeutralResourcesLanguage("en-us")]
Expand Down
30 changes: 25 additions & 5 deletions GetStoreAppWebView/Views/Windows/WebWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using GetStoreAppWebView.UI.Dialogs.Common;
using GetStoreAppWebView.WindowsAPI.PInvoke.Comctl32;
using GetStoreAppWebView.WindowsAPI.PInvoke.User32;
using GetStoreAppWebView.WindowsAPI.PInvoke.Uxtheme;
using Microsoft.UI;
using Microsoft.UI.Content;
using Microsoft.UI.Windowing;
Expand Down Expand Up @@ -144,9 +145,10 @@ public WebWindow()
IsWindowMaximized = overlappedPresenter.State is OverlappedPresenterState.Maximized;
contentCoordinateConverter = ContentCoordinateConverter.CreateForWindowId(AppWindow.Id);

// 标题栏设置
// 标题栏和右键菜单设置
SetTitleBar(AppTitlebar);
SetTitleBarColor((Content as FrameworkElement).ActualTheme);
SetTitleBarTheme((Content as FrameworkElement).ActualTheme);
SetClassicMenuTheme((Content as FrameworkElement).ActualTheme);

// 挂载相应的事件
AppWindow.Changed += OnAppWindowChanged;
Expand Down Expand Up @@ -305,7 +307,8 @@ private void OnCloseClicked(object sender, RoutedEventArgs args)
/// </summary>
private void OnActualThemeChanged(FrameworkElement sender, object args)
{
SetTitleBarColor(sender.ActualTheme);
SetTitleBarTheme(sender.ActualTheme);
SetClassicMenuTheme(sender.ActualTheme);
}

#endregion 第四部分:窗口内容挂载的事件
Expand Down Expand Up @@ -605,9 +608,9 @@ private void OnNewWindowRequested(object sender, CoreWebView2NewWindowRequestedE
#region 第六部分:窗口属性设置

/// <summary>
/// 设置标题栏按钮的颜色
/// 设置标题栏按钮的主题色
/// </summary>
private void SetTitleBarColor(ElementTheme theme)
private void SetTitleBarTheme(ElementTheme theme)
{
AppWindowTitleBar titleBar = AppWindow.TitleBar;

Expand Down Expand Up @@ -640,6 +643,23 @@ private void SetTitleBarColor(ElementTheme theme)
}
}

/// <summary>
/// 设置传统菜单标题栏按钮的主题色
/// </summary>
private static void SetClassicMenuTheme(ElementTheme theme)
{
if (theme is ElementTheme.Light)
{
UxthemeLibrary.SetPreferredAppMode(PreferredAppMode.ForceLight);
}
else
{
UxthemeLibrary.SetPreferredAppMode(PreferredAppMode.ForceDark);
}

UxthemeLibrary.FlushMenuThemes();
}

#endregion 第六部分:窗口属性设置

#region 第七部分:窗口过程
Expand Down
14 changes: 14 additions & 0 deletions GetStoreAppWebView/WindowsAPI/PInvoke/Uxtheme/PreferredAppMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace GetStoreAppWebView.WindowsAPI.PInvoke.Uxtheme
{
/// <summary>
/// 菜单样式
/// </summary>
public enum PreferredAppMode
{
Default,
AllowDark,
ForceDark,
ForceLight,
Max
};
}
Loading

0 comments on commit 67a7306

Please sign in to comment.