简体   繁体   English

TToolbar会在一段时间后停止显示按钮标题

[英]TToolbar stops displaying button captions after a while

Any idea why TToolbar with TToolButtons would stop displaying button captions after a while? 知道为什么带有TToolButtons的TToolbar会在一段时间后停止显示按钮标题吗? This happens to all toolbars on auto-created forms across the whole application. 在整个应用程序中自动创建的表单上的所有工具栏都会发生这种情况。 Toolbars on dynamically created forms work fine even after this problem. 即使遇到此问题,动态创建的表单上的工具栏也能正常工作。

I have only seen this happen on one Windows 7 notebook. 我只在一台Windows 7笔记本上看到过这种情况。 No errors are raised when this happens and I can't reproduce the problem on command. 发生这种情况时不会出现错误,我无法在命令中重现问题。 The only solution is restarting the application. 唯一的解决方案是重新启动应用程序。

TToolbar.ShowCaptions is always True and never changed. TToolbar.ShowCaptions始终为True且永远不会更改。 This is also visible on the image below because icons are vertically aligned when ShowCaptions is False. 这在下图中也是可见的,因为当ShowCaptions为False时,图标是垂直对齐的。

缺少字幕的工具栏

A similar problem happened before on a Windows 8 PC. 之前在Windows 8 PC上发生过类似的问题。 However this time captions were replaced with other text. 然而,这次字幕被其他文字取代。

带有奇怪字幕的工具栏

EDIT: 编辑:

I was able to reproduce the problem by calling TImageList.Change between 5-10k times. 我能够通过调用TImageList.Change重现5-10k次来重现问题。 I only have Delphi 2010, so I can't say if this is a Delphi or Windows issue. 我只有Delphi 2010,所以我不能说这是Delphi还是Windows问题。

Unit: 单元:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ImgList, ComCtrls, ToolWin, StdCtrls, Gauges;

type
  TImageListHelper = class helper for TImageList
  public
    procedure DoChange;
  end;

  TForm1 = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    ImageList1: TImageList;
    ProgressBar1: TProgressBar;
    procedure ToolButton1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ToolButton1Click(Sender: TObject);
begin
  repeat
    ImageList1.DoChange;
    ProgressBar1.StepIt;
    Self.Update;
  until ProgressBar1.Position >= ProgressBar1.Max;
end;

procedure TImageListHelper.DoChange;
begin
  Self.Change;
end;

end.

Form: 形成:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 66
  ClientWidth = 711
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  PixelsPerInch = 96
  TextHeight = 13
  object ToolBar1: TToolBar
    Left = 0
    Top = 0
    Width = 711
    Height = 41
    ButtonHeight = 36
    ButtonWidth = 71
    Caption = 'ToolBar1'
    Images = ImageList1
    ShowCaptions = True
    TabOrder = 0
    ExplicitWidth = 885
    object ToolButton1: TToolButton
      Left = 0
      Top = 0
      Caption = 'ToolButton1'
      ImageIndex = 0
      OnClick = ToolButton1Click
    end
    object ToolButton2: TToolButton
      Left = 71
      Top = 0
      Caption = 'ToolButton2'
      ImageIndex = 0
    end
    object ToolButton3: TToolButton
      Left = 142
      Top = 0
      Caption = 'ToolButton3'
      ImageIndex = 0
    end
    object ToolButton4: TToolButton
      Left = 213
      Top = 0
      Caption = 'ToolButton4'
      ImageIndex = 0
    end
    object ToolButton5: TToolButton
      Left = 284
      Top = 0
      Caption = 'ToolButton5'
      ImageIndex = 0
    end
    object ToolButton6: TToolButton
      Left = 355
      Top = 0
      Caption = 'ToolButton6'
      ImageIndex = 0
    end
    object ToolButton7: TToolButton
      Left = 426
      Top = 0
      Caption = 'ToolButton7'
      ImageIndex = 0
    end
    object ToolButton8: TToolButton
      Left = 497
      Top = 0
      Caption = 'ToolButton8'
      ImageIndex = 0
    end
    object ToolButton9: TToolButton
      Left = 568
      Top = 0
      Caption = 'ToolButton9'
      ImageIndex = 0
    end
    object ToolButton10: TToolButton
      Left = 639
      Top = 0
      Caption = 'ToolButton10'
      ImageIndex = 0
    end
  end
  object ProgressBar1: TProgressBar
    Left = 0
    Top = 49
    Width = 711
    Height = 17
    Align = alBottom
    Max = 10000
    Step = 1
    TabOrder = 1
    ExplicitTop = 48
  end
  object ImageList1: TImageList
    Left = 8
    Top = 16
    Bitmap = {
      494C010101000500040010001000FFFFFFFFFF10FFFFFFFFFFFFFFFF424D3600
      0000000000003600000028000000400000001000000001002000000000000010
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      FF000000FF000000000000000000000000000000000000000000000000000000
      FF000000FF000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      FF000000FF000000000000000000000000000000000000000000000000000000
      FF000000FF000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000FF000000FF0000000000000000000000FF000000FF000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000FF000000FF0000000000000000000000FF000000FF000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      00000000000000000000000000000000FF000000FF0000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      00000000000000000000000000000000FF000000FF0000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000FF000000FF0000000000000000000000FF000000FF000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000FF000000FF0000000000000000000000FF000000FF000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      FF000000FF000000000000000000000000000000000000000000000000000000
      FF000000FF000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      FF000000FF000000000000000000000000000000000000000000000000000000
      FF000000FF000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000424D3E000000000000003E000000
      2800000040000000100000000100010000000000800000000000000000000000
      000000000000000000000000FFFFFF00FFFF000000000000FFFF000000000000
      FFFF000000000000E7E7000000000000E7E7000000000000F99F000000000000
      F99F000000000000FE7F000000000000FE7F000000000000F99F000000000000
      F99F000000000000E7E7000000000000E7E7000000000000FFFF000000000000
      FFFF000000000000FFFF00000000000000000000000000000000000000000000
      000000000000}
  end
end

Given the reproduction in the question, I think that the problem is that the VCL toolbar code removes all the buttons and then recreates them whenever the image list is modified. 鉴于问题中的再现,我认为问题是VCL工具栏代码删除了所有按钮,然后在修改图像列表时重新创建它们。

I'm looking at Delphi 6 code because I don't have Delphi 2010 immediately to hand, but the code has not materially changed. 我正在查看Delphi 6代码,因为我没有立即提供Delphi 2010,但代码没有实质性改变。 The pertinent code is in TToolBar.CreateButtons . 相关代码位于TToolBar.CreateButtons Towards the bottom of this method we have: 在这个方法的底部,我们有:

for I := 0 to InternalButtonCount - 1 do Perform(TB_DELETEBUTTON, 0, 0);
UpdateButtons;

The loop removes all the buttons, and then UpdateButtons adds them back. 循环删除所有按钮,然后UpdateButtons将它们添加回来。 It seems that the underlying control does not appreciate being treated this way. 看起来潜在的控制并不喜欢被这样对待。 Instead of removing all the buttons we can instead just remove any excess buttons. 我们可以改为删除任何多余的按钮,而不是删除所有按钮。

var
  Count: Integer;
....
Count := InternalButtonCount;
while Count>FButtons.Count do
begin
  Perform(TB_DELETEBUTTON, Count-1, 0);
  dec(Count);
end;
UpdateButtons;

In your example code, and in the real application, you aren't changing the number of buttons so this version does not even enter the loop. 在您的示例代码和实际应用程序中,您不会更改按钮的数量,因此此版本甚至不会进入循环。

With this change, your program runs correctly. 通过此更改,您的程序可以正常运行。

You can apply this change in your application by doing the following: 您可以通过执行以下操作在应用程序中应用此更改:

  1. Take a copy of ComCtrls.pas from the source folder of your installation directory, and save it in your project tree. 从安装目录的源文件夹中获取ComCtrls.pas的副本,并将其保存在项目树中。
  2. Add that copied ComCtrls unit to your project. 将复制的ComCtrls单元添加到项目中。
  3. Make the modifications described above. 进行上述修改。

It appears to be a bug in Delphi 2010, in ComCtrls unit at the end of function TToolBar.UpdateItem() . 它似乎是Delphi 2010中的一个错误,在function TToolBar.UpdateItem()末尾的ComCtrls单元中。 The function starts at line 21476. 该功能从第21476行开始。

In Delphi XE4 (it might have been fixed earlier, I can't check) the following comment and code (which is missing from Delphi 2010) appears at the end of the function : 在Delphi XE4中(它可能已经修复过,我无法检查)下面的注释和代码(Delphi 2010中缺少的)出现在函数的末尾

  // If more than 2^16 strings are TB_ADDSTRING-ed to the tool bar's string
  // pool, the Windows API assumes iString is a pointer to a null terminated
  // string, not an index in the string pool.  Therefore we have to recreate
  // the toolbar to reset the string pool so the strings display propperly.
  if Button.iString >= 65536 then
    RecreateWnd;

Taking a copy of Delphi 2010 ComCtrls.pas to the projects folder and adding the above code, cured the problem that your test creates. 将Delphi 2010 ComCtrls.pas的副本复制到projects文件夹并添加上述代码,可以解决测试创建的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM