How to enable dark system menu in WPF

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

User's image

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,706 Reputation points Microsoft External Staff
    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

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.