How to enable dark system menu in WPF

quan zhao 20 Reputation points
2024-04-01T05:58:24.4233333+00:00

User's image

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,706 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 47,176 Reputation points Microsoft Vendor
    2024-04-01T06:29:25.16+00:00

    Hi,@ quan zhao. Welcome to Microsoft Q&A. To enable the dark system menu with move, minimize, and maximize options in a WPF application, you could use the same Windows API functions. Here's how you can do it.

    You can call the EnableDarkSystemMenu method in the constructor of your MainWindow or at any appropriate place in your application where you want to enable the dark system menu.

      [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();
    
      public MainWindow()
    
      {
    
          InitializeComponent();
    
          EnableDarkSystemMenu();
    
      }
    
      private void EnableDarkSystemMenu()
    
      {
    
          SetPreferredAppMode(2); // Enable dark system menu
    
          FlushMenuThemes();
    
      }
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful