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

如何在Delphi VCL窗体自定义标题栏的自定义按钮上添加Caption或Glyph图标

实现代码示例

procedure TForm1.TitleBarPanel1CustomButtons0Paint(Sender: TObject);
var
  Btn: TTitleBarCustomButton;
  IconMargin, TextLeft: Integer;
begin
  // 强转Sender为当前自定义按钮对象
  Btn := Sender as TTitleBarCustomButton;
  // 图标边距可根据需求自行调整
  IconMargin := 3;

  // 1. 绘制ImageList中的图标,最后一个参数为图标在ImageList中的索引,可替换为你需要的索引值
  ImageList1.Draw(Btn.Canvas, IconMargin, (Btn.Height - ImageList1.Height) div 2, 0);

  // 2. 计算文字绘制起始位置,避免和图标重叠
  TextLeft := IconMargin + ImageList1.Width + IconMargin;

  // 自定义文字样式,可按需修改
  Btn.Canvas.Font.Color := clWhite;
  Btn.Canvas.Brush.Style := bsClear; // 设置文字背景透明

  // 绘制Caption文字,此处直接调用按钮自身的Caption属性,也可替换为固定文本
  Btn.Canvas.TextOut(TextLeft, (Btn.Height - Btn.Canvas.TextHeight(Btn.Caption)) div 2, Btn.Caption);

  // 可选适配按钮状态:悬浮/按下时切换样式
  if Btn.MouseInClient then
    Btn.Canvas.Font.Color := clYellow;
  if Btn.Down then
    ImageList1.Draw(Btn.Canvas, IconMargin, (Btn.Height - ImageList1.Height) div 2, 1); // 切换为按下状态对应的图标
end;

注意事项

  • 代码中的ImageList1需要替换为你窗体上实际的ImageList组件名称
  • 图标索引需要和你ImageList中导入的图标顺序对应
  • 边距、字体、颜色等样式参数都可以根据你的界面需求调整

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 23:27:03