「ダークモード対応」の版間の差分

提供: MeryWiki
ナビゲーションに移動 検索に移動
Admin (トーク) による版 3913 を取り消し
編集の要約なし
15行目: 15行目:
uses
uses
{$IF CompilerVersion > 22.9}
{$IF CompilerVersion > 22.9}
   Winapi.Windows, System.SysUtils, System.Classes, System.Math;
   Winapi.Windows, System.SysUtils;
{$ELSE}
{$ELSE}
   Windows, SysUtils, Classes, Math;
   Windows, SysUtils;
{$IFEND}
{$IFEND}


48行目: 48行目:
function DarkModeLoadLibrary: Boolean;
function DarkModeLoadLibrary: Boolean;
begin
begin
   if CheckWin32Version(10) and (TOSVersion.Build = 17763) then
   if CheckWin32Version(10) and (TOSVersion.Build >= 17763) then
     FHandle := LoadLibrary(PChar(THEME_LIB));
     FHandle := LoadLibrary(PChar(THEME_LIB));
   Result := FHandle <> 0;
   Result := FHandle <> 0;
61行目: 61行目:
       (@ShouldAppsUseDarkMode <> nil) and (@AllowDarkModeForWindow <> nil) and
       (@ShouldAppsUseDarkMode <> nil) and (@AllowDarkModeForWindow <> nil) and
       (@AllowDarkModeForApp <> nil) and (@FlushMenuThemes <> nil) then
       (@AllowDarkModeForApp <> nil) and (@FlushMenuThemes <> nil) then
    begin
      // AllowDarkModeForApp(True);
      // FlushMenuThemes;
       DarkModeSupported := True;
       DarkModeSupported := True;
    end;
   end;
   end;
end;
end;

2019年3月28日 (木) 21:36時点における版

Windows 10 のダークモードに対応するためには?

現時点 (Windows 10 1809) では、Windows 10 のダークモードに対応するためには UWP アプリケーションである必要があります。

しかし、Windows 10 1809 では explorer.exe がダークモードに対応したことで、Win32 アプリケーションのダークモード対応も徐々に開発されているような気がします。

Windows 10 1809 ではいくつかの非公開 API の存在が確認されており、これらを呼び出すことで Win32 アプリケーションでもダークモードに対応できる可能性が見えてきました。

下記は Delphi でダークモードの API を呼び出す実験で作成したソースコード (雑) です。

unit DarkModeClasses;

interface

uses
{$IF CompilerVersion > 22.9}
  Winapi.Windows, System.SysUtils;
{$ELSE}
  Windows, SysUtils;
{$IFEND}


resourcestring
  SDllLoadError = '%s をロードできません';

const
  THEME_LIB = 'uxtheme.dll';

type
  TShouldAppsUseDarkMode = function: BOOL; cdecl;
  TAllowDarkModeForWindow = function(hwnd: HWND; allow: BOOL): BOOL; cdecl;
  TAllowDarkModeForApp = function(allow: BOOL): BOOL; cdecl;
  TFlushMenuThemes = procedure; cdecl;
  TRefreshImmersiveColorPolicyState = procedure; cdecl;

var
  FHandle: THandle;
  FLoaded: Boolean;
  RefreshImmersiveColorPolicyState: TRefreshImmersiveColorPolicyState;
  ShouldAppsUseDarkMode: TShouldAppsUseDarkMode;
  AllowDarkModeForWindow: TAllowDarkModeForWindow;
  AllowDarkModeForApp: TAllowDarkModeForApp;
  FlushMenuThemes: TFlushMenuThemes;
  DarkModeSupported: Boolean;

implementation

function DarkModeLoadLibrary: Boolean;
begin
  if CheckWin32Version(10) and (TOSVersion.Build >= 17763) then
    FHandle := LoadLibrary(PChar(THEME_LIB));
  Result := FHandle <> 0;
  if Result then
  begin
    @RefreshImmersiveColorPolicyState := GetProcAddress(FHandle, MakeIntResource(104));
    @ShouldAppsUseDarkMode := GetProcAddress(FHandle, MakeIntResource(132));
    @AllowDarkModeForWindow := GetProcAddress(FHandle, MakeIntResource(133));
    @AllowDarkModeForApp := GetProcAddress(FHandle, MakeIntResource(135));
    @FlushMenuThemes := GetProcAddress(FHandle, MakeIntResource(136));
    if (@RefreshImmersiveColorPolicyState <> nil) and
      (@ShouldAppsUseDarkMode <> nil) and (@AllowDarkModeForWindow <> nil) and
      (@AllowDarkModeForApp <> nil) and (@FlushMenuThemes <> nil) then
      DarkModeSupported := True;
  end;
end;

procedure DarkModeFreeLibrary;
begin
  if FHandle <> 0 then
    FreeLibrary(FHandle);
  FHandle := 0;
end;

initialization

if not FLoaded then
  FLoaded := DarkModeLoadLibrary;

finalization

if FLoaded then
  DarkModeFreeLibrary;

end.
スポンサーリンク