如何解决Delphi - 应用 VCL 主题时更改功能区菜单颜色
我在应用了 VCL 主题 的 Delphi XE7 应用程序上使用 TRibbon,我想更改菜单颜色(因为很难看到深色主题),如下所示:
我尝试了以下代码,但它仅在禁用样式时有效:
Ribbon1.ApplicationMenu.Menu.ColorMap.MenuColor:= clYellow;
这行也没有影响:
Ribbon1.ApplicationMenu.Menu.ColorMap.MenuColor:= StyleServices.GetStyleColor(scButtonHot);
有谁知道这可能吗? 非常感谢!
解决方法
用你喜欢的颜色创造你自己的风格。
,经过一番尝试,我找到了解决方案。我不知道这是否是最好的方法,但它对我有用,并且可能对其他人有用。
问题是下面的方法 (Vcl.ActnMenus.pas),当 StyleServices 启用时:
procedure TCustomActionPopupMenu.DrawBackground;
begin
inherited;
if StyleServices.Enabled and not StyleServices.IsSystemStyle then
StyleServices.DrawElement(Canvas.Handle,StyleServices.GetElementDetails(tmPopupBackground),Rect(0,Width,Height))
else
begin
Canvas.Brush.Color := ColorMap.MenuColor;
Canvas.FillRect(ClientRect);
end;
end;
所以,为了绕过这个方法,我直接hook了(改编自here):
unit MethodHooker;
interface
uses Windows,Vcl.ActnMenus;
type
PInstruction = ^TInstruction;
TInstruction = packed record
Opcode: Byte;
Offset: Integer;
end;
TCustomActionPopupMenu = class(Vcl.ActnMenus.TCustomActionPopupMenu)
procedure DrawBackgroundEx;
end;
implementation
procedure Patch(Address: Pointer; const NewCode; Size: NativeUInt);
var
NumberOfBytes: NativeUInt;
begin
WriteProcessMemory(GetCurrentProcess,Address,@NewCode,Size,NumberOfBytes);
end;
procedure Redirect(OldAddress,NewAddress: Pointer);
var
NewCode: TInstruction;
begin
NewCode.Opcode := $E9;//jump relative
NewCode.Offset := Integer(NewAddress)-Integer(OldAddress)-SizeOf(NewCode);
Patch(OldAddress,NewCode,SizeOf(NewCode));
end;
{ TCustomActionPopupMenu }
procedure TCustomActionPopupMenu.DrawBackgroundEx;
begin
Canvas.Brush.Color := $00EEEAE9;
Canvas.FillRect(ClientRect);
end;
initialization
Redirect(@TCustomActionPopupMenu.DrawBackground,@TCustomActionPopupMenu.DrawBackgroundEx);
end.
就是这样。只需保存此单元并将其添加到项目中即可。无需在任何地方调用它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。