创建TToolbutton运行时 [英] Create TToolbutton runtime

查看:91
本文介绍了创建TToolbutton运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行时我有一个创建 TToolbuttons 的问题,以及它们在我的 TToolbar 中的显示方式。

I have a problem with creating TToolbuttons at runtime and how they appear in my TToolbar.

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

Basically I got a toolbar with some buttons in it already. I can create buttons at runtime and set the parent to the toolbar. But they are always shown as the first buttons in my toolbar.

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

How can I make them appear at the end of my toolbar? Or any position I want them to be.

推荐答案

这是一个通用的过程,需要一个工具栏,并添加一个按钮,具有指定的说明:

Here is a generic procedure that takes a toolbar, and adds a button to it, with a specified caption:

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;

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

And here is example usage of that procedure:

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

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

Your question does also ask how to add a button to an arbitrary place on the TToolbar. This code is similar to before, but it also allows you to specify which index you want the new button to appear after.

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;

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

And here is example usage for this revised version:

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;

祝你好运!

这篇关于创建TToolbutton运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆