我在Delphi 5和Delphi XE6之间发现another回归。
我有一个TPanel
,其内容设置为AutoSize
(面板为绿色):
当TPanel
包含任何其他控件时,例如TListView
,面板会自动调整大小为包含列表视图的大小:
但是当包含的控件是TWebBrowser
(或替换TEmbeddedWB
)时,面板将不会自动调整大小:
自动调整大小时必须有一些VCL管道TWebBrowser
VCL包装器出错。我需要知道什么在XE6中被破坏了以及它的修复。
User user1611655 had a good workaround:
我有类似的问题。
通过将
TPanel
"置于"TWebBrowser
,并将网络浏览器与alClient
对齐。
我对解决方法不太感兴趣,作为修复 - 我可以将其添加到我们的其他一堆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.
Form1.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
答案 0 :(得分:6)
这个问题是由两次回归引起的。
我在Stackoverflow问题TPanel does not AutoSize when containing a 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;
此处的错误是除非存在 sfWidth 或 sfHeight 缩放标记,否则不会调用DoAdjustSize
。
解决方法是不要试图超越自己,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 或 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
消息。 TWinControl 中的 WMWindowPoschanged 处理程序无法正确处理邮件。
常规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
。 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;
我决定采用第四种解决方案;一个让bug完整无缺,但对我来说已经足够了。
错误是:
所以我们先使用 SetBounds 。
利用 SetBounds 中的(大部分)正确代码进行所有自动调整。然后我们可以致电SetObjectRects
。当 WMWindowPosChanged 收到其WM_WindowPosChanging
消息时,它将无所事事 - 因此不会出错。
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;
注意:任何已发布到公共领域的代码。无需归属。