启用了 Delphi VCL 样式的平面工具栏按钮?

2024-04-17

如果没有启用 VCL 样式,我的 TActionToolbar 看起来就像平面工具栏。然而,如果我启用几乎任何 VCL 样式,突然间所有工具栏按钮看起来都像 3d 按钮。

VCL Style Viewer 应用程序显示具有平面和类似按钮外观的工具栏按钮:

当我启用 VCL 样式时,如何使我的 TActionToolbar 具有平面工具栏按钮样式,而不是看起来像一堆按钮?


与该相关的所有控件使用的绘制方法TA动作管理器 http://docwiki.embarcadero.com/Libraries/XE4/en/Vcl.ActnMan.TActionManager由一个处理TPlatform默认样式ActionBars http://docwiki.embarcadero.com/Libraries/XE3/en/Vcl.PlatformDefaultStyleActnCtrls.TPlatformDefaultStyleActionBars此处的类用于绘制控件的类根据 Windows 版本、是否启用了 vcl 样式等进行选择。在这种情况下csThemed TActionControl风格被选择并且类中定义Vcl.ThemedActnCtrls使用单位。

因此,要修改按钮的外观,您需要创建一个TActionBarStyleEx后代类,然后重写中定义的类和方法Vcl.ThemedActnCtrls单元。幸运的是,这项工作已经在Vcl.PlatformVclStylesActnCtrls http://code.google.com/p/vcl-styles-utils/source/browse/trunk/Common/Vcl.PlatformVclStylesActnCtrls.pas是该单位的一部分Vcl 样式实用程序 http://code.google.com/p/vcl-styles-utils/项目。因此,您只需要进行一些小的修改即可获得所需的结果。

尝试这个示例(这是一个修改版本Vcl.PlatformVclStylesActnCtrls http://code.google.com/p/vcl-styles-utils/source/browse/trunk/Common/Vcl.PlatformVclStylesActnCtrls.pas单元)我添加了一些注释来显示代码必须修改的地方。

unit Vcl.PlatformVclStylesActnCtrls;

interface

uses
   Vcl.ActnMan,
   Vcl.Buttons,
   Vcl.PlatformDefaultStyleActnCtrls;

type
  TPlatformVclStylesStyle = class(TPlatformDefaultStyleActionBars)
  public
    function GetControlClass(ActionBar: TCustomActionBar; AnItem: TActionClientItem): TCustomActionControlClass; override;
    function GetStyleName: string; override;
  end;

var
  PlatformVclStylesStyle: TPlatformVclStylesStyle;

implementation

uses
  Vcl.Menus,
  Winapi.Windows,
  System.SysUtils,
  Vcl.ActnMenus,
  Vcl.ActnCtrls,
  Vcl.ThemedActnCtrls,
  Vcl.Forms,
  Vcl.ListActns,
  Vcl.ActnColorMaps,
  Vcl.Themes,
  Vcl.XPActnCtrls,
  Vcl.StdActnMenus,
  Vcl.Graphics;

type
  TActionControlStyle = (csStandard, csXPStyle, csThemed);

  TThemedMenuItemEx = class(Vcl.ThemedActnCtrls.TThemedMenuItem)
  private
    procedure NativeDrawText(DC: HDC; const Text: string; var Rect: TRect; Flags: Longint);
  protected
    procedure DrawText(var Rect: TRect; var Flags: Cardinal; Text: string); override;
  end;

  TThemedMenuButtonEx = class(Vcl.ThemedActnCtrls.TThemedMenuButton)
  private
    procedure NativeDrawText(const Text: string; var Rect: TRect; Flags: Longint);
  protected
    procedure DrawText(var ARect: TRect; var Flags: Cardinal;
      Text: string); override;
  end;

  TThemedMenuItemHelper = class Helper for TThemedMenuItem
  private
   function GetPaintRect: TRect;
   property PaintRect: TRect read GetPaintRect;
  end;

  TThemedButtonControlEx = class(TThemedButtonControl)
  protected
    procedure DrawBackground(var PaintRect: TRect); override;
  end;


{ TThemedMenuItemHelper }
function TThemedMenuItemHelper.GetPaintRect: TRect;
begin
 Result:=Self.FPaintRect;
end;

function GetActionControlStyle: TActionControlStyle;
begin
  if TStyleManager.IsCustomStyleActive then
    Result := csThemed
  else
  if TOSVersion.Check(6) then
  begin
    if StyleServices.Theme[teMenu] <> 0 then
      Result := csThemed
    else
      Result := csXPStyle;
  end
  else
  if TOSVersion.Check(5, 1) then
    Result := csXPStyle
  else
    Result := csStandard;
end;

{ TPlatformDefaultStyleActionBarsStyle }

function TPlatformVclStylesStyle.GetControlClass(ActionBar: TCustomActionBar;
  AnItem: TActionClientItem): TCustomActionControlClass;
begin
  if ActionBar is TCustomActionToolBar then
  begin
    if AnItem.HasItems then
      case GetActionControlStyle of
        csStandard: Result := TStandardDropDownButton;
        csXPStyle: Result := TXPStyleDropDownBtn;
      else
        Result := TThemedDropDownButton;
      end
    else
    if (AnItem.Action is TStaticListAction) or (AnItem.Action is TVirtualListAction) then
      Result := TCustomComboControl
    else
    case GetActionControlStyle of
      csStandard: Result := TStandardButtonControl;
      csXPStyle: Result := TXPStyleButton;
    else
      Result := TThemedButtonControlEx;//this is the class used to draw the buttons of the TActionToolbar
    end
  end
  else
  if ActionBar is TCustomActionMainMenuBar then
    case GetActionControlStyle of
      csStandard: Result := TStandardMenuButton;
      csXPStyle: Result := TXPStyleMenuButton;
    else
      Result := TThemedMenuButtonEx;
    end
  else
  if ActionBar is TCustomizeActionToolBar then
  begin
    with TCustomizeActionToolbar(ActionBar) do
      if not Assigned(RootMenu) or (AnItem.ParentItem <> TCustomizeActionToolBar(RootMenu).AdditionalItem) then
        case GetActionControlStyle of
          csStandard: Result := TStandardMenuItem;
          csXPStyle: Result := TXPStyleMenuItem;
        else
          Result := TThemedMenuItemEx;
        end
      else
      case GetActionControlStyle of
          csStandard: Result := TStandardAddRemoveItem;
          csXPStyle: Result := TXPStyleAddRemoveItem;
      else
          Result := TThemedAddRemoveItem;
      end
  end
  else
  if ActionBar is TCustomActionPopupMenu then
    case GetActionControlStyle of
      csStandard: Result := TStandardMenuItem;
      csXPStyle: Result := TXPStyleMenuItem;
    else
      Result := TThemedMenuItemEx;
    end
  else
  case GetActionControlStyle of
    csStandard: Result := TStandardButtonControl;
    csXPStyle: Result := TXPStyleButton;
  else
    Result := TThemedButtonControl;
  end
end;

function TPlatformVclStylesStyle.GetStyleName: string;
begin
  Result := 'Platform VclStyles Style';
end;

{ TThemedMenuItemEx }

procedure TThemedMenuItemEx.NativeDrawText(DC: HDC; const Text: string;
  var Rect: TRect; Flags: Integer);
const
  MenuStates: array[Boolean] of TThemedMenu = (tmPopupItemDisabled, tmPopupItemNormal);
var
  LCaption: string;
  LFormats: TTextFormat;
  LColor: TColor;
  LDetails: TThemedElementDetails;
  LNativeStyle : TCustomStyleServices;
begin
  LNativeStyle:=TStyleManager.SystemStyle;

  LFormats := TTextFormatFlags(Flags);
  if Selected and Enabled then
  begin
    LDetails := StyleServices.GetElementDetails(tmPopupItemHot);
    if TOSVersion.Check(5, 1) then
     SetBkMode(DC, Winapi.Windows.TRANSPARENT);
  end
  else
    LDetails := StyleServices.GetElementDetails(MenuStates[Enabled or ActionBar.DesignMode]);

  if not StyleServices.GetElementColor(LDetails, ecTextColor, LColor) or (LColor = clNone) then
    LColor := ActionBar.ColorMap.FontColor;

  LCaption := Text;
  if (tfCalcRect in LFormats) and ( (LCaption = '') or (LCaption[1] = cHotkeyPrefix) and (LCaption[2] = #0) ) then
    LCaption := LCaption + ' ';

  LNativeStyle.DrawText(DC, LDetails, LCaption, Rect, LFormats, LColor);
end;

procedure TThemedMenuItemEx.DrawText(var Rect: TRect; var Flags: Cardinal;
  Text: string);
var
  LRect: TRect;
begin
  if Selected and Enabled then
    StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(tmPopupItemHot), PaintRect)
  else if Selected then
    StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(tmPopupItemDisabledHot), PaintRect);

  if (Parent is TCustomActionBar) and (not ActionBar.PersistentHotkeys) then
    Text := FNoPrefix;
  Canvas.Font := Screen.MenuFont;

  if ActionClient.Default then
    Canvas.Font.Style := Canvas.Font.Style + [fsBold];

  LRect := PaintRect;
  NativeDrawText(Canvas.Handle, Text, LRect, Flags or DT_CALCRECT or DT_NOCLIP);
  OffsetRect(LRect, Rect.Left,
    ((PaintRect.Bottom - PaintRect.Top) - (LRect.Bottom - LRect.Top)) div 2);
  NativeDrawText(Canvas.Handle, Text, LRect, Flags);

  if ShowShortCut and ((ActionClient <> nil) and not ActionClient.HasItems) then
  begin
    Flags := DrawTextBiDiModeFlags(DT_RIGHT);
    LRect := TRect.Create(ShortCutBounds.Left, LRect.Top, ShortCutBounds.Right, LRect.Bottom);
    LRect.Offset(Width, 0);
    NativeDrawText(Canvas.Handle, ActionClient.ShortCutText, LRect, Flags);
  end;
end;

{ TThemedMenuButtonEx }
procedure TThemedMenuButtonEx.NativeDrawText(const Text: string; var Rect: TRect;
  Flags: Integer);
const
  MenuStates: array[Boolean] of TThemedMenu = (tmMenuBarItemNormal, tmMenuBarItemHot);
var
  LCaption: string;
  LFormats: TTextFormat;
  LColor: TColor;
  LDetails: TThemedElementDetails;
  LNativeStyle : TCustomStyleServices;
begin
  LNativeStyle:=TStyleManager.SystemStyle;

  LFormats := TTextFormatFlags(Flags);
  if Enabled then
    LDetails := StyleServices.GetElementDetails(MenuStates[Selected or MouseInControl or ActionBar.DesignMode])
  else
    LDetails := StyleServices.GetElementDetails(tmMenuBarItemDisabled);

  Canvas.Brush.Style := bsClear;
  if Selected then
    Canvas.Font.Color := clHighlightText
  else
    Canvas.Font.Color := clMenuText;

  if not StyleServices.GetElementColor(LDetails, ecTextColor, LColor) or (LColor = clNone) then
    LColor := ActionBar.ColorMap.FontColor;

  LCaption := Text;
  if (tfCalcRect in LFormats) and ( (LCaption = '') or (LCaption[1] = cHotkeyPrefix) and (LCaption[2] = #0) ) then
    LCaption := LCaption + ' ';

  if Enabled then
    LDetails := StyleServices.GetElementDetails(MenuStates[Selected or MouseInControl]);

  LNativeStyle.DrawText(Canvas.Handle, LDetails, LCaption, Rect, LFormats, LColor);
end;

procedure TThemedMenuButtonEx.DrawText(var ARect: TRect; var Flags: Cardinal;
  Text: string);
var
  LRect: TRect;
begin
  if Parent is TCustomActionMainMenuBar then
    if not TCustomActionMainMenuBar(Parent).PersistentHotkeys then
      Text := StripHotkey(Text);

  LRect := ARect;
  Inc(LRect.Left);
  Canvas.Font := Screen.MenuFont;
  NativeDrawText(Text, LRect, Flags or DT_CALCRECT or DT_NOCLIP);
  NativeDrawText(Text, LRect, Flags);
end;

{ TThemedButtonControlEx }
//Here you must modify the code to draw the buttons
procedure TThemedButtonControlEx.DrawBackground(var PaintRect: TRect);
const
  DisabledState: array[Boolean] of TThemedToolBar = (ttbButtonDisabled, ttbButtonPressed);
  CheckedState: array[Boolean] of TThemedToolBar = (ttbButtonHot, ttbButtonCheckedHot);
var
  SaveIndex: Integer;
begin
  if not StyleServices.IsSystemStyle and ActionClient.Separator then Exit;

  SaveIndex := SaveDC(Canvas.Handle);
  try
    if Enabled and not (ActionBar.DesignMode) then
    begin
      if (MouseInControl or IsChecked) and
         Assigned(ActionClient) {and not ActionClient.Separator)} then
      begin
        StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(CheckedState[IsChecked or (FState = bsDown)]), PaintRect);

        if not MouseInControl then
          StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(ttbButtonPressed), PaintRect);
      end
      else
        ;//StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(ttbButtonNormal), PaintRect);// the code to draw the button in normal state was commented to get the desired look and feel
    end
    else
      ;//StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(DisabledState[IsChecked]), PaintRect);// the code to draw the button in disabled state was commented to get the desired look and feel

  finally
    RestoreDC(Canvas.Handle, SaveIndex);
  end;
end;

initialization
  PlatformVclStylesStyle := TPlatformVclStylesStyle.Create;
  RegisterActnBarStyle(PlatformVclStylesStyle);
  DefaultActnBarStyle :=PlatformVclStylesStyle.GetStyleName;
finalization
  UnregisterActnBarStyle(PlatformVclStylesStyle);
  PlatformVclStylesStyle.Free;
end.

要使用它,只需将 Vcl.PlatformVclStylesActnCtrls 单元添加到您的项目中,然后设置 TActionManager 的样式,如下所示:

  ActionManager1.Style:=PlatformVclStylesStyle;

Before

After

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

启用了 Delphi VCL 样式的平面工具栏按钮? 的相关文章

随机推荐

  • RODBC 错误 - ODBC 连接失败 - 无法使用我的 Mac 连接到 MySQL(小牛)

    我正在尝试在我的 mac 小牛 上使用 RODBC 通过 R 连接到 mysql 数据库 但经过几个小时的愚蠢努力后 我仍然无法让它工作 当我尝试连接到数据库时 RODBC 抛出此错误 错误消息非常简洁 我不知道这意味着什么 ch 我按照以
  • AWS RDS 公共访问

    我对 AWS 配置感到困惑 我的目标是创建一个可从网络内部和外部访问的数据库 这是我所拥有的 RDS 实例运行 postgresql VPC 内部的连接正常工作 端点被设置为可公开访问 当从外部访问时 实际上确实可以解析 但是 连接挂起 表
  • 如何验证一个 XSD 架构是另一个 XSD 架构的子集?

    如何验证一个 XSD 架构是否是另一个 XSD 架构的子集 我们正在使用 蓝图 XSD 模式的集合 定义子组件可用的所有可能的输入或输出 创建一个系统应用程序 许多子组件正在被实现 并且这些子组件使用 XML 文件在它们之间传递数据 每个子
  • 选择特定日期之后的数据

    我希望能够从数据框中选择特定日期之后或之前的日期 例如 使用黄金价格的 quandl 数据 pGold lt read csv http www quandl com api v1 datasets BUNDESBANK BBK01 WT5
  • 从光滑表定义创建表

    在 PlaySlick 示例中 有一个包含示例数据访问对象的文件 https github com playframework play slick blob master samples basic app dao CatDAO scal
  • 条件渲染和 ReactCSSTransitionGroup 动画

    我制作了一个小应用程序 它根据 Redux 状态呈现不同的组件 我想在其中一个组件渲染时应用 淡入淡出 动画 但是 由于某种原因 它对我不起作用 这是我到目前为止所拥有的 内容 js class Content extends Compon
  • 根据速度或其他变量向左、向右、向上和向下滑动

    我有一个从简单的手势扩展而来的类 我正在使用 onfling 方法 class MyGestureListener extends GestureDetector SimpleOnGestureListener Override publi
  • 这是 C 中未定义的行为吗? [复制]

    这个问题在这里已经有答案了 我正在 gcc 上运行我的 C 代码来理解前 后增量运算符 然而我看到的结果并不是我所期望的 就像第 6 行一样 因为 i 是 5 所以应该是 8 7 6 5 5 但它是8 7 6 5 8 然后到最后一行 显示1
  • 如何在 PHP 中的会话中存储对象?

    您好 我想使用类似的类在会话中存储或保存对象SessionHandler或数组 SESSION 我已经看到如果我序列化该对象是可能的 并且我不想丢失该对象实例的方法 我已经看到序列化是可能的 但我想要存储的对象是由 PDOStatement
  • Windows 版 GitHub:登录失败

    我当时用的是git scm http git scm com用客户端很久了 看到了一个 GitHub for Windows 客户端 为什么不试试呢 我用CCleaner删除了git scm 然后安装了这个客户端 在登录阶段 它显示 登录失
  • Proguard AWS s3 问题

    尝试为 amazon s3 实现 proguard 时遇到一个真正的问题 它在第 29 行加载应用程序时不断崩溃 即 s3Client new AmazonS3Client new BasicAWSCredentials Constants
  • 对行进行排序后,Ag-grid 拖放不起作用

    在 Ag grid react 上对行进行排序后 拖放停止工作 在排序之前 拖放已全部设置完毕并且运行良好 postSort API 似乎没有什么可以实现我需要的东西 重置行 甚至设置可拖动 就像选择一样 postSort nodes gt
  • 从谷歌地图中删除标记簇[不仅仅是标记]

    在我的phonegapp cordova应用程序中 我使用谷歌地图工具 有时我需要更改其上显示的点 我发现了一些有用的代码here https developers google com maps documentation javascr
  • 集成 Gitlab 和 TravisCi

    有没有办法将 Travis Ci 与 Gitlab 集成 或者至少使用用户名和密码而不是 Github 凭据登录 TravisCi 不 截至目前 Travis CI 与 GitHub 严格绑定 虽然确实有关于减少这一要求的讨论 但它仍然存在
  • 为什么 std::pow(double, int) 从 C++11 中删除?

    在调查的同时计算 p q 求幂 的有效方法 其中 q 是整数 https stackoverflow com questions 5625431回顾 C 98 和 C 11 标准 我注意到显然std pow double int C 11
  • Google 脚本:将列中的新值附加到另一个工作表

    我一直在研究这个项目 但无法让它按照我想要的方式工作 我有两张表 其中一列具有唯一 ID 我需要确保每当在一张工作表 Sheet1 中添加新 ID 时 它都会被复制到另一张工作表 Sheet2 中的最后一个空行 如果该值已经存在 请确保它忽
  • 解析日期后从 Oracle Select 语句返回数字

    我想编写一个 Oracle SQL select 语句 告诉它是否可以通过返回代码来解析给定格式的日期字符串 失败时返回零 异常 成功时返回正数 SELECT CASE WHEN PARSING SUCCESSFUL ie to date
  • 使用字典替换字符串

    我正在学习 python 并处理字符串 以找到使用字典进行字符串替换的更好方法 我有一个包含自定义占位符的字符串 如下所示 placeholder prefix placeholder suffix dict key1 string key
  • Azure Function-使用 python 代码解压缩受密码保护的文件

    我正在尝试解压缩存储在 Azure Blob 容器上的受密码保护的文件 我想将其提取到 Azure Blob 本身上 我已经使用Python创建了一个Azure函数应用程序 目前它是基于定时器控制事件的 来测试东西 以下是我的代码 我不确定
  • 启用了 Delphi VCL 样式的平面工具栏按钮?

    如果没有启用 VCL 样式 我的 TActionToolbar 看起来就像平面工具栏 然而 如果我启用几乎任何 VCL 样式 突然间所有工具栏按钮看起来都像 3d 按钮 VCL Style Viewer 应用程序显示具有平面和类似按钮外观的