You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取应用TStyle主题的TDBNavigator控件的图像?

解决方案:从样式化TDBNavigator获取图像到TSpeedButton

你的问题出在新建了一个未应用自定义样式的TDBNavigator实例,所以拿到的是默认图像列表。要获取自定义样式下的图像,必须使用窗体上已存在的、已经通过VCL样式引擎初始化的TDBNavigator组件。

方法一:通过Hack类访问已样式化DBNavigator的图像列表

首先定义用于暴露TDBNavigator保护方法的Hack类:

type
  THackDBNavigator = class(TDBNavigator)
  public
    function GetButtonImageList: TImageList;
  end;

function THackDBNavigator.GetButtonImageList: TImageList;
begin
  Result := inherited GetButtonImageList;
end;

然后使用窗体上已有的DBNavigator实例(假设名为DBNavigator1)来获取图像列表:

var
  ic: TImageList;
begin
  ic := TImageList.Create(Self);
  try
    // 关联已样式化的DBNavigator的图像列表
    ic.Assign(THackDBNavigator(DBNavigator1).GetButtonImageList);
    SpeedButtonFirst.Images := ic;
    SpeedButtonFirst.ImageIndex := Integer(nbFirst);
  except
    ic.Free;
    raise;
  end;
end;

方法二:直接从VCL样式引擎获取指定按钮图像

不需要依赖DBNavigator实例,直接从当前激活的样式中提取对应按钮的图像:

var
  ActiveStyle: TCustomStyleServices;
  ButtonBmp: TBitmap;
begin
  ActiveStyle := TStyleManager.ActiveStyle;
  ButtonBmp := TBitmap.Create;
  try
    // 获取First按钮的样式图像(元素类型为tnDBNavFirstButton)
    if ActiveStyle.GetElementDetails(tnDBNavFirstButton, ButtonBmp) then
      SpeedButtonFirst.Glyph.Assign(ButtonBmp);
  finally
    ButtonBmp.Free;
  end;
end;

注:其他按钮对应的元素类型分别为:tnDBNavPriorButton(上一条)、tnDBNavNextButton(下一条)、tnDBNavLastButton(最后一条)、tnDBNavInsertButton(插入)、tnDBNavDeleteButton(删除)、tnDBNavEditButton(编辑)、tnDBNavPostButton(提交)、tnDBNavCancelButton(取消)。


内容的提问来源于stack exchange,提问作者Ronaldo Brisola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 15:02:10