首页 > 解决方案 > 在运行时创建浮动 TToolbar 而不会闪烁

问题描述

我试图在运行时创建一个自定义的 TToolbar 浮动在表单上(在与之关联的控件下方)。

我的问题是,在创建时浮动和定位工具栏的过程会产生可怕的闪烁,它最初是在监视器的左上角绘制的,然后才被移动到表单上我想要的位置。

我找不到避免这种情况的方法。有办法吗?

procedure TMainForm.Button3Click(Sender: TObject);
var
  newToolbar : TToolbar;
  newButton : TToolButton;
begin
  newToolbar := TToolbar.Create(Self);

  newToolbar.Visible := False;

  newToolbar.ManualFloat( Rect( 0, 0, newToolbar.Width, newToolbar.Height ));

  newToolbar.Parent := Self;

  newToolbar.left := 100;
  newToolbar.Top  := 100;

  newToolbar.ShowCaptions := True;

  newButton := TToolButton.Create(Self);
  newButton.Parent := newToolbar;
  newButton.Caption := 'Test';

  newToolbar.Visible := True;
end;

参考: -创建 TToolbutton 运行时 -带有在运行时创建的操作的工具按钮 - Delphi - 创建自定义 TToolBar 组件

标签: delphi

解决方案


我对你的解决方案有点困惑,所以我提供了我对这个主题的两个看法。具体来说,我不明白你为什么要使用ManualFloat(),几行之后设置了工具栏的父级,这使得它不浮动。

这是浮动工具栏的解决方案,使用ManualFloat(). TCustomDockForm工具栏在给定位置以自己的临时形式浮动在表单上方。

所需要的记录ManualFloat()是为最终位置设置的,因此不会在错误的位置闪烁,并且控件立即正确定位。

procedure TForm1.Button3Click(Sender: TObject);
var
  newToolbar : TToolbar;
  newButton : TToolButton;
  p: TPoint;
begin
  newToolbar := TToolbar.Create(Self);

  // calculate position in screen coordinates for the floating toolbar
  p := ClientOrigin;
  p.Offset(100, 100);
  // and make it floating in final position
  newToolbar.ManualFloat( Rect(p.X, p.Y, p.X+NewToolbar.Width, p.Y+newToolbar.Height) );

  newToolbar.Visible := False; // really needed ?

  // Then create the toolbar buttons
  newToolbar.ShowCaptions := True;

  newButton := TToolButton.Create(self);
  newButton.Parent := newToolbar;
  newButton.Caption := 'Test';

  newToolbar.Visible := True;
end;

但是,由于您实际上似乎想要一个非浮动工具栏,它位于表单上您喜欢的任何位置(而不是在表单的默认顶部),更好的解决方案是ManualFloat()完全跳过并设置 Align属性工具栏到alNone. 这使得它可以移动到父窗体上的任何位置。

procedure TForm1.Button4Click(Sender: TObject);
var
  newToolbar : TToolbar;
  newButton : TToolButton;
begin
  newToolbar := TToolbar.Create(Self);
  newToolbar.Align := alNone; // constructor sets it to alTop

  newToolbar.Visible := False; // really needed ?

  newToolbar.Parent := Self;
  newToolbar.Left := 100;
  newToolbar.Top := 200;

  newToolbar.ShowCaptions := True;

  newButton := TToolButton.Create(self);
  newButton.Parent := newToolbar;
  newButton.Caption := 'Test';

  newToolbar.Visible := True; //
end;

这为您提供了与您自己的代码相同的外观,但省略了ManualFloat().

最后,一张显示外观的图像:

在此处输入图像描述

底部工具栏是用Button4Click()


推荐阅读