i am using winui 3 app, when i switch theme to Dark mode, system contextMenu (Right Click on Titlebar) is not updated to Dark, so i need to use
[DllImport("uxtheme.dll", EntryPoint = "#135", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int SetPreferredAppMode(int preferredAppMode);
but this code only work when app is running:
SetPreferredAppMode(2); //Force Dark
InitializeComponent();
i need to update system contextmenu based on theme change in running mode. for example i need to change system context menu theme in Button click event. but system context menu theme is not updated in button click event.
what is wrong? is there any other methods for force update system context menu theme? how Windows Settings or other apps do this?
Try to call FlushMenuThemes()
to change the theme of the system context menu at runtime:
[DllImport("uxtheme.dll", EntryPoint = "#135", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int SetPreferredAppMode(int preferredAppMode);
[DllImport("uxtheme.dll", EntryPoint = "#136", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern void FlushMenuThemes();
private void Button_Click(object sender, RoutedEventArgs e)
{
...
SetPreferredAppMode(2);
FlushMenuThemes();
}