TPanel 在包含 TWebBrowser 时不会自动调整大小

2024-02-12

我找到了一个another https://stackoverflow.com/questions/25449362/xe6-tlistview-column-widths-become-zero-if-you-read-column-widthDelphi 5 和 Delphi XE6 之间的回归。

我有一个TPanel即设置为AutoSize其内容(面板为绿色):

当。。。的时候TPanel包含任何其他控件,例如ATListView,面板将自动调整自身大小以适应所包含列表视图的大小:

但是当包含的控件是TWebBrowser(或替换TEmbeddedWB http://sourceforge.net/projects/embeddedwb/),面板不会自动调整大小:

一定是 TWebBrowser 的错

必须有一些 VCL 管道用于自动调整大小,TWebBrowserVCL 包装器出错。我需要知道 XE6 中的问题及其修复方法。

用户 user1611655 有一个很好的解决方法 https://stackoverflow.com/a/20224876/12597:

我有类似的问题。

这是通过放置一个解决的TPanel“在”之下TWebBrowser,并将网络浏览器对准alClient.

我对解决方法不太感兴趣,作为fix- 我可以将它添加到我们的其他 VCL 源代码修复中。事实上,因为我使用了改进的TEmbeddedWB控制,可以将修复程序放在那里;离开TWebBrowser broken.

重现步骤

The Form1.pas:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ExtCtrls, Vcl.OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
     Panel1: TPanel;
     WebBrowser1: TWebBrowser;
  private
     { Private declarations }
  public
     { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

end.

The 表格1.dfm:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 248
  ClientWidth = 373
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 32
    Top = 32
     Width = 209
     Height = 97
     AutoSize = True
     BevelOuter = bvNone
     Color = clLime
     ParentBackground = False
     TabOrder = 0
     object WebBrowser1: TWebBrowser
        Left = 0
        Top = 0
        Width = 190
        Height = 161
        ParentShowHint = False
        ShowHint = False
        TabOrder = 0
        ControlData = {
          4C00000023260000E40500000000000000000000000000000000000000000000
          000000004C000000000000000000000001000000E0D057007335CF11AE690800
          2B2E126208000000000000004C0000000114020000000000C000000000000046
          8000000000000000000000000000000000000000000000000000000000000000
          00000000000000000100000000000000000000000000000000000000}
     end
  end
end

该问题是由两个回归引起的。

  • 一进一进TWinControl.AlignControls
  • 另一个是由更改引起的TOleControl.SetBounds,虽然实际的错误是在TWinControl.WMWindowPosChanged.

“永远不会自动调整大小”错误

我在 Stackoverflow 问题中详细介绍了第一个错误包含 TPanel 时 TPanel 不自动调整大小 https://stackoverflow.com/a/29421315/12597:

procedure TWinControl.AlignControls(AControl: TControl; var Rect: TRect);
begin
   //...snip

   // Apply any constraints
   if Showing and ((sfWidth in FScalingFlags) or (sfHeight in FScalingFlags)) then
      DoAdjustSize;

   //...snip
end;

这里的错误是它不会调用DoAdjustSize除非sfWidth or sfHeight存在缩放标志。

解决办法是不要试图超越自己,并且DoAdjustSize不管:

procedure TWinControl.AlignControls(AControl: TControl; var Rect: TRect);
begin
   //...snip

   // Apply any constraints
   //QC125995: Don't look to scaling flags to decide if we should adjust size
   if Showing {and ((sfWidth in FScalingFlags) or (sfHeight in FScalingFlags))} then
      DoAdjustSize;

   //...snip
end;

“调整大小时不自动调整大小”错误

先前的修复使面板在包含子项时自动调整大小TControl or 双控。但是当面板包含一个时还有另一个错误远程控制。该错误是在 Delphi XE 中引入的。与上面的错误不同,这个错误是由某人认为自己很聪明引起的,而这个错误要微妙得多。

When a 远程控制调整大小后,其设置边界方法被调用。这是原始的功能代码:

procedure TOleControl.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
   if ((AWidth <> Width) and (Width > 0)) or ((AHeight <> Height) and (Height > 0)) then
   begin
      //...snip: perhaps tweak AWidth and AHeight
   end;

   inherited SetBounds(ALeft, ATop, AWidth, AHeight);
end;

在 XE2 时间范围内,代码已更改为以便通知底层Ole控制它的边界即将改变:

procedure TOleControl.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
var
   LRect: TRect;
begin
   if ((AWidth <> Width) and (Width > 0)) or ((AHeight <> Height) and (Height > 0)) then
   begin
      //...snip: perhaps tweak AWidth and AHeight

      //Notify the underlying Ole control that its bounds are about to change
      if FOleInplaceObject <> nil then
      begin
         LRect := Rect(Left, Top, Left+AWidth, Top+AHeight);
         FOleInplaceObject.SetObjectRects(LRect, LRect);
      end;
   end;

   inherited SetBounds(ALeft, ATop, AWidth, AHeight);
end;

作者不知道的是,这暴露了一个错误双控。打电话的问题是 Ole 控件(例如 Internet Explorer)转身并发送WM_WindowPosChanged https://msdn.microsoft.com/en-us/library/windows/desktop/ms632652(v=vs.85).aspx信息。这WM_WindowPoschanged处理程序在双控不能正确处理消息。

虽然常规的SetBounds方法正确调用:

procedure SetBounds;
begin
   UpdateAnchorRules;
   UpdateExplicitBounds;
   RequestAlign; //the important one we need
end;

The WMWindowPosChanged方法仅调用:

procedure WMWindowPosChanged;
begin
   UpdateBounds; //which only calls UpdateAnchorRules
end;

这意味着WinControl调整其大小;但其父级永远不会重新调整以处理新的自动尺寸。

The Fix

修复方法是:

  • 不要打电话IOleInPlaceObject.SetObjectRects完全来自 SetBounds。 Delphi 5没有做到,而且运行得很好
  • 更改 WM_WindowPosChanged 以便它也被调用请求对齐:

      procedure TWinControl.WMWindowPosChanged;
      begin
         UpdateBounds;
         RequestAlign; //don't forget to autosize our parent since we're changing our size behind our backs (e.g. TOleControl)
      end;
    
  • 更改 UpdateBounds 以同时调用请求对齐:

     procedure TWinControl.UpdateBounds;
     begin
        UpdateAnchorRules;
        //UpdateExplicitBounds; SetBounds calls this; why are we not calling it?
        RequestAlign; //in response to WM_WindowPosChanged            
     end;
    

我选择了第四种解决方案;一个完整保留错误的方法,但对我来说已经足够修复它了。

错误在于:

  • WM_WindowPosChanged不能正确处理尺寸变化
  • but 设置边界 does

所以让我们使用设置边界 first.

利用(大部分)正确的代码设置边界完成所有自动调整大小。然后我们可以调用SetObjectRects. When WM_WindowPosChanged收到它的WM_WindowPosChanging消息,它不会做任何事情 - 因此不会做任何错误的事情。

tl;dr

procedure TOleControl.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
var
  LRect: TRect;
begin
  if ((AWidth <> Width) and (Width > 0)) or ((AHeight <> Height) and (Height > 0)) then
  begin
     //...snip: perhaps fiddle with AWidth or AHeight

     {Removed. Call *after* inheirted SetBounds
     //Notify the underlying Ole control that its bounds are about to change
     if FOleInplaceObject <> nil then
     begin
        LRect := Rect(Left, Top, Left+AWidth, Top+AHeight);
        FOleInplaceObject.SetObjectRects(LRect, LRect);
     end;}
  end;

  inherited SetBounds(ALeft, ATop, AWidth, AHeight);

  //moved to call *after* SetBounds, we need SetBounds to happen first.       
  //TWinControl's WMWindowPosChanged does not handle autosizing correctly
  //while SetBounds does.
  //Notify the underlying Ole control that its bounds are already about to change
  if FOleInplaceObject <> nil then
  begin
     LRect := Rect(Left, Top, Left+AWidth, Top+AHeight);
     FOleInplaceObject.SetObjectRects(LRect, LRect);
  end;
end;

Note:发布到公共领域的任何代码。无需归属。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TPanel 在包含 TWebBrowser 时不会自动调整大小 的相关文章

随机推荐