Create a dark menu using the createpopupmenu function on windows 10

143 Views Asked by At

I want to use createpopupmenu to create a menu in a Delphi program and make it change the color according to the system Personalize->color settings, but it failed:

enter image description here

I tried using the

HAll := CreatePopupMenu; 
SetWindowTheme(HAll, 'Dark Mode Explorer', nil);

function to modify the color I also tried it,

procedure setDarkMode(hnd:HWND);
var
  hNTDLL, hUxtheme: HMODULE;
  RtlGetNtVersionNumbers: function(var major, minor, buildNumber: DWORD): BOOL; stdcall;
  AllowDarkModeForApp: function(allow: Boolean): Boolean; stdcall;
  SetPreferredAppMode: function(appMode: WORD): Word stdcall;
  AllowDarkModeForWindow : function (Had: hWnd; allow:Boolean):Boolean stdcall;
  FlushMenuThemes: procedure(); stdcall;
  major, minor, buildNumber: DWORD;
  Ret:Integer;
begin
  hNTDLL := GetModuleHandle('ntdll.dll');
  if hNTDLL <> 0 then
  begin
    @RtlGetNtVersionNumbers := GetProcAddress(hNTDLL, 'RtlGetNtVersionNumbers');
    if Assigned(RtlGetNtVersionNumbers) then
    begin
      RtlGetNtVersionNumbers(major, minor, buildNumber);
      buildNumber := buildNumber and not $F0000000;
      if (major >= 10) and ((buildNumber = 17763) or (buildNumber = 18362) or (buildNumber = 18363) or
        (buildNumber = 19041) or (buildNumber >= 19042)) then
      begin
        hUxtheme := LoadLibraryEx('uxtheme.dll', 0, $00000800);
        @FlushMenuThemes := GetProcAddress(hUxtheme, MAKEINTRESOURCEA(136));
        if hUxtheme <> 0 then
        begin
          if buildNumber < 18362 then
          begin
            @AllowDarkModeForApp := GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
            if Assigned(AllowDarkModeForApp) then
              AllowDarkModeForApp(True);
          end
          else
          begin
            @AllowDarkModeForWindow := GetProcAddress(hUxtheme, MAKEINTRESOURCEA(133));
            @SetPreferredAppMode := GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
            if Assigned(AllowDarkModeForWindow) then
              AllowDarkModeForWindow(hnd,True);
            if Assigned(SetPreferredAppMode) then
              SetPreferredAppMode(2);
            Ret:=GetLastError;
          end;
          FlushMenuThemes;
          FreeLibrary(hUxtheme);
        end;
      end;
    end;
  end;
end;

but it didn't work . How can I set the color of my menu to dark mode like the desktop right-click menu (I need to copy a desktop menu, so I don't consider the implementation of other menus)

0

There are 0 best solutions below