Ian*_*oyd 4 delphi twebbrowser delphi-xe6
我在Delphi 5和Delphi XE6之间发现了另一个回归.
我有一个TPanel
设置AutoSize
为其内容(面板为绿色):
当TPanel
包含任何其他控件(例如a)时TListView
,面板将自动调整其大小为包含列表视图的大小:
但是当包含的控件是TWebBrowser
(或替换TEmbeddedWB
)时,面板将不会自动调整大小:
TWebBrowser
VCL包装器出错时,必须有一些自动调整大小所需的VCL管道.我需要知道什么在XE6中被破坏了以及它的修复.
我遇到了类似的问题.
它通过在
TPanel
"下面" 放置并将TWebBrowser
Web浏览器对齐来解决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.
Run Code Online (Sandbox Code Playgroud)
该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
Run Code Online (Sandbox Code Playgroud)
这个问题是由两个回归引起的.
我在Stackoverflow问题中详述的第一个错误TPanel在包含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;
Run Code Online (Sandbox Code Playgroud)
这里的错误是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;
Run Code Online (Sandbox Code Playgroud)
当包含子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;
Run Code Online (Sandbox Code Playgroud)
在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;
Run Code Online (Sandbox Code Playgroud)
作者不知道,这暴露了TWinControl中的一个错误.调用的问题IOleInPlaceObject.SetObjectRects
是Ole控件(例如Internet Explorer)转身并发送WM_WindowPosChanged
消息.TWinControl中的WMWindowPoschanged处理程序无法正确处理消息.
正则SetBounds
方法正确调用时:
procedure SetBounds;
begin
UpdateAnchorRules;
UpdateExplicitBounds;
RequestAlign; //the important one we need
end;
Run Code Online (Sandbox Code Playgroud)
该WMWindowPosChanged
方法只调用:
procedure WMWindowPosChanged;
begin
UpdateBounds; //which only calls UpdateAnchorRules
end;
Run Code Online (Sandbox Code Playgroud)
这意味着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;
Run Code Online (Sandbox Code Playgroud)更改UpdateBounds也可以调用RequestAlign:
procedure TWinControl.UpdateBounds;
begin
UpdateAnchorRules;
//UpdateExplicitBounds; SetBounds calls this; why are we not calling it?
RequestAlign; //in response to WM_WindowPosChanged
end;
Run Code Online (Sandbox Code Playgroud)我决定采用第四种解决方案; 一个让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;
Run Code Online (Sandbox Code Playgroud)
注意:任何代码都会发布到公共领域.无需归属.