加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 创建TToolbutton运行时

发布时间:2020-12-15 04:28:33 所属栏目:大数据 来源:网络整理
导读:在运行时创建TToolbuttons以及它在TToolbar中的显示方式有问题. 基本上我有一个工具栏里面有一些按钮.我可以在运行时创建按钮 并将父项设置为工具栏.但它们始终显示为工具栏中的第一个按钮. 我如何让它们出现在我的工具栏的末尾?或任何我想要他们的职位. 解
在运行时创建TToolbuttons以及它在TToolbar中的显示方式有问题.

基本上我有一个工具栏里面有一些按钮.我可以在运行时创建按钮
并将父项设置为工具栏.但它们始终显示为工具栏中的第一个按钮.

我如何让它们出现在我的工具栏的末尾?或任何我想要他们的职位.

解决方法

这是一个通用过程,它使用工具栏,并添加一个按钮,并附带指定的标题:
procedure AddButtonToToolbar(var bar: TToolBar; caption: string);
var
  newbtn: TToolButton;
  lastbtnidx: integer;
begin
  newbtn := TToolButton.Create(bar);
  newbtn.Caption := caption;
  lastbtnidx := bar.ButtonCount - 1;
  if lastbtnidx > -1 then
    newbtn.Left := bar.Buttons[lastbtnidx].Left + bar.Buttons[lastbtnidx].Width
  else
    newbtn.Left := 0;
  newbtn.Parent := bar;
end;

这里是该过程的示例用法:

procedure Button1Click(Sender: TObject);
begin
  ToolBar1.ShowCaptions := True;  //by default,this is False
  AddButtonToToolbar(ToolBar1,IntToStr(ToolBar1.ButtonCount));
end;

您的问题还会询问如何在TToolbar上的任意位置添加按钮.此代码与之前类似,但它也允许您指定要在其后显示新按钮的索引.

procedure AddButtonToToolbar(var bar: TToolBar; caption: string;
  addafteridx: integer = -1);
var
  newbtn: TToolButton;
  prevBtnIdx: integer;
begin
  newbtn := TToolButton.Create(bar);
  newbtn.Caption := caption;

  //if they asked us to add it after a specific location,then do so
  //otherwise,just add it to the end (after the last existing button)
  if addafteridx = -1 then begin
    prevBtnIdx := bar.ButtonCount - 1;
  end
  else begin
    if bar.ButtonCount <= addafteridx then begin
      //if the index they want to be *after* does not exist,//just add to the end
      prevBtnIdx := bar.ButtonCount - 1;
    end
    else begin
      prevBtnIdx := addafteridx;
    end;
  end;

  if prevBtnIdx > -1 then
    newbtn.Left := bar.Buttons[prevBtnIdx].Left + bar.Buttons[prevBtnIdx].Width
  else
    newbtn.Left := 0;

  newbtn.Parent := bar;
end;

这是修改版本的示例用法:

procedure Button1Click(Sender: TObject);
begin
  //by default,"ShowCaptions" is false
  ToolBar1.ShowCaptions := True;

  //in this example,always add our new button immediately after the 0th button
  AddButtonToToolbar(ToolBar1,IntToStr(ToolBar1.ButtonCount),0);
end;

祝你好运!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读