delphi - 在运行时创建浮动 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 组件
解决方案
我对你的解决方案有点困惑,所以我提供了我对这个主题的两个看法。具体来说,我不明白你为什么要使用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()
推荐阅读
- javascript - 我需要如何定义用户?ReferenceError: 用户未定义 Steam 用户
- magento2 - 分层导航减慢我们的网站。我应该升级托管计划吗
- mysql - phpMyAdmin - 如何手动更改日期时间值
- php - 在 WooCommerce 中为越南国家/地区设置状态结帐字段
- python-2.7 - pytorch backports.functools_lru_cache 冲突
- docker - 在 .dockerignore 文件中包含 pkg
- for-loop - 如何在双循环中扫描整数。c语言
- python-3.x - PyCharm IDE 不会突出显示未声明的变量
- git - Git 没有将存储库推送到 github
- ios - iOS curl -X POST 快速集成