
主要代码
procedure TForm1.Button1Click(Sender: TObject);
begin
if DarkModeIsEnabled then
ShowMessage('当前系统默认应用模式为深色/黑暗模式!')
else
ShowMessage('当前系统默认应用模式为浅色/明亮模式!');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
if DarkModeIsEnabled then //如果当前系统默认应用模式为 暗 ,设置软件主题为Carbon,否则设置主题为Windows10
SetSpecificThemeMode(True, 'Carbon', 'Windows10');
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
SetSpecificThemeMode(RadioGroup1.ItemIndex = 0, 'Carbon', 'Windows10');
end;
// 检查Windows注册表,查看Windows深色模式是否可用
function DarkModeIsEnabled: boolean;
// 当Windows正在暗模式下运行自动设置深色模式主题
// 使用方法:
// 1. 转到 project - options - Appearance - Custom Styles
// 2. 勾选一个或更多个主题. 记下主题名称!
// 3. 在 FormCreate (或其他地方) 事件中,写以下一行代码:
// SetAppropriateThemeMode(**深色主题名称**, **浅色主题名称**);
//
// 例如:
// SetAppropriateThemeMode('Carbon', 'Windows10');
//
procedure SetAppropriateThemeMode(const DarkModeThemeName, LightModeThemeName: string);
// 基于“AsDarkMode”布尔值,设置深色/黑暗模式或浅色/明亮模式主题
// 例如:
// SetSpecificThemeMode(False, '深色主题名称', '浅色主题名称');
// 如果主题存在,将应用程序主题更改为名为 '浅色主题名称'
//
procedure SetSpecificThemeMode(const AsDarkMode: Boolean; const DarkModeThemeName, LightModeThemeName: string);
implementation
uses
{$IFDEF MSWINDOWS}
Winapi.Windows, // 用于预定义注册表项常量
System.Win.Registry, // 用于注册表的读访问权
{$ENDIF}
VCL.themes; // 用于访问TStyleManager
procedure SetAppropriateThemeMode(const DarkModeThemeName, LightModeThemeName: string);
begin
SetSpecificThemeMode(DarkModeIsEnabled, DarkModeThemeName, LightModeThemeName);
end;
procedure SetSpecificThemeMode(const AsDarkMode: Boolean; const DarkModeThemeName, LightModeThemeName: string);
var
ChosenTheme: string;
begin
if AsDarkMode then
ChosenTheme := DarkModeThemeName
else
ChosenTheme := LightModeThemeName;
TStyleManager.TrySetStyle(ChosenTheme, False);
end;
function DarkModeIsEnabled: boolean;
{$IFDEF MSWINDOWS}
const
TheKey = 'SoftwareMicrosoftWindowsCurrentVersionThemesPersonalize';
TheValue = 'AppsUseLightTheme';
var
Reg: TRegistry;
{$ENDIF}
begin
Result := False;
// 该操作依赖于注册表设置,仅能用于MS Windows。
// 如果开发人员已设法做到了这一点,那么告诉他们不要这样做!
{$IFNDEF MSWINDOWS}
{$MESSAGE WARN '"DarkModeIsEnabled" will only work on MS Windows targets'}
{$ELSE}
Reg := TRegistry.Create(KEY_READ);
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.KeyExists(TheKey) then
if Reg.OpenKey(TheKey, False) then
try
if Reg.ValueExists(TheValue) then
Result := Reg.ReadInteger(TheValue) = 0;
finally
Reg.CloseKey;
end;
finally
Reg.Free;
end;
{$ENDIF}
end;
来源:https://www.amingstudio.com/delphi/672.html
Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号
执行时间: 0.49717402458191 seconds