TPanel does not support AutoSize when using TWebBrowser

I found another regression between Delphi 5 and Delphi XE6.

I have TPanelone that itself installs into AutoSizeits contents (green panel):

enter image description here

When TPanelcontains any other control, for example. a TListView, the panel will automatically adjust the size of the contained list:

enter image description here

But when the contained control is TWebBrowser(or a replacement TEmbeddedWB), the panel will not automatically adjust:

enter image description here

There must be a TWebBrowser error

For automatic calibration, a VCL filter must be installed that wraps the TWebBrowserVCL shell . What I need to know is what was broken in XE6 and the fix for it.

user1611655 :

.

, TPanel "" TWebBrowser - alClient.

, fix. VCL. , TEmbeddedWB, ; TWebBrowser .

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.

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
+3
1

.

  • TWinControl.AlignControls
  • TOleControl.SetBounds, TWinControl.WMWindowPosChanged.

"Nothing autosizes ever"

, Stackoverflow TPanel AutoSize TPanel:

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 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;

" "

AutoSize, TControl TWinControl. , TOleControl. Delphi XE. , , - , , .

TOleControl, SetBounds. , :

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;

TWinControl. IOleInPlaceObject.SetObjectRects , Ole (, Internet Explorer) WM_WindowPosChanged. WMWindowPoschanged TWinControl .

SetBounds :

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

WMWindowPosChanged :

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

, WinControl ; , .

:

  • IOleInPlaceObject.SetObjectRects SetBounds. Delphi 5 ,
  • WMWindowPosChanged, RequestAlign:

      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, RequestAlign:

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

; , .

​​:

  • WMWindowPosChanged
  • SetBounds

, SetBounds.

( ) SetBounds, . SetObjectRects. WMWindowPosChanged 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 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;

. , . .

+5

All Articles