Some ActiveX/OleControls render with zero size. I.e. Width/Height
falls back to (0,0) no matter what you do at design or runtime This
appears to be related to the fact that some of these controls do not
respond to SetExtent()
, returning "E_Fail". This is not a "random
error2, but according to MS docs as designed for components that
either do not have a UI or set their dimension internally, independent
of their containing control.
The relevant code in VCL.OleCtrls.pas
didn't change of the past few
versions, so it’s not really new in Ikaria. Still worth getting it
fixed, as ActiveX is still used in legacy projects, migrated to 12.
The reason for that behavior is apparently that some ActiveX controls
won't respond to SetExtent()
as called by SetBounds()
in
VCL.OleCtrls.pas
. This is not a “random” error, but may be as
designed - according to MS docs:
If an object's size is fixed, that is, if it cannot be set by its
container, IOleObject::SetExtent should return E_FAIL. This is always
the case with linked objects, whose sizes are set by their link
sources, not by their containers.
In those cases Delphi simply resets an ActiveX’s bounds to zero -
which is at least unconvenient.
The code in VCL.OleCtrls.pas
also has some weird HighDPI
calculations, but these do not seem to affect this bug. AWidth = 100
would result in 2645 (for a 96dpi screen), which at least seems to be
questionable. I couldn’t find anything in the MS docs about the units
used in SetExtent()
, though.
Temp := Point(MulDiv(AWidth, 2540, Screen.PixelsPerInch),
MulDiv(AHeight, 2540, Screen.PixelsPerInch));
Here is a proposal for a fixed TOleControl.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
procedure TOleControl.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
var
LRect: TRect;
Temp: TPoint;
LResult: HResult;
LIsVisible: boolean;
begin
if ((AWidth <> Width) and (Width >= 0)) or ((AHeight <> Height) and (Height >= 0)) then
begin
LResult := S_OK;
LIsVisible := (FMiscStatus and OLEMISC_INVISIBLEATRUNTIME) = 0;
//Only call SetExtent if control has visible UI at runtime
if LIsVisible then
begin
Temp := Point(MulDiv(AWidth, 2540, Screen.PixelsPerInch),
MulDiv(AHeight, 2540, Screen.PixelsPerInch));
{$IFDEF WIN64}
LResult := FOleObject.SetExtent(DVASPECT_CONTENT, @Temp);
{$ELSE}
LResult := FOleObject.SetExtent(DVASPECT_CONTENT, Temp);
{$ENDIF}
end;
// UI won't respond to SetExtend, use default dimensions for design time
if ((LResult <> S_OK) or (not LIsVisible)) then
begin
if (Height > 0) and (Width > 0) then
begin
AWidth := Height;
AHeight := Width;
end
else
begin
AWidth := 25;
AHeight := 25;
end;
end;
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;
Steps to reproduce
Install and Import an ActiveX control, such as SysTray_OCX.zip from
here: Sample Code : ActiveX Controls
Note: this control (like many others) depends on the VB5 Runtime.
Note 2: MS does no longer offer an official link - due to it's age
http://www.tools4vb.de/download/vb5run.exe
Create a new VCL app, and drop a "cSysTray" component onto the main
form.
Result: component is on form, with dimension set to "zero". You can only find the component using the structure pane.
Expected: Component should show up as 25x25 px component - like any non-visual component (TFDQuery etc…)