1

I'm experiencing Width and Height issue when working with a Visual Basic 6.0 OCX in Delphi.

When I drag and drop some OCX controls onto a form in Delphi 12.1, the Width and Height properties of the OCX are set to 0 (zero), and I cannot change these values even in the Object Inspector. These OCX controls work perfectly in Delphi 7. I've also disabled all visual manifests, so it's running in classic Windows mode.

I'm trying to understand why these issues occur in the newer versions of Delphi.

These OCXs were developed by the company I used to work for and they work in Delphi 7 without any problems.

4
  • 1
    It seems like there are some issues with High DPI calculations for OCX controls in new Delphi versions.
    – Dalija Prasnikar
    Commented Aug 7 at 16:04
  • 1) yes it was correct for width and height issue, because when I try non- high dpi delphi version it's working well. So HighDPI should not apply TOleControl class. I have tried with Delphi Seattle it worked well for width and Height. Commented Aug 8 at 1:14
  • 2) there is a common error that occurs in both Delphi 10 Seattle, Delphi 11, and Delphi 12, so this issue is also present in Delphi 10 Seattle. The error is: Project Project1.exe raised exception class $C000008F with message 'floating point divide by zero at 0x75861052'. When you see call stack you will see it comes from vbaStr. This is a random error that occurs when closing a form or pressing a button, and the cause of this error may be COM BSTR parameters. freeimage.host/i/da8iRAg Commented Aug 8 at 1:16
  • 1
    I can reproduce that behaviour for example with SysTray from here: thevbzone.com/s_actx_ctrls.htm I created a Quality Portal report for that, including a proposal for a fix: embt.atlassian.net/servicedesk/customer/portal/3/RSB-209 (Only visible for Delphi Beta participants) The root cause is SetExtent(), which not all ActiveX controls do respond to, which in turn resets these controls to zero size in Delphi. There are also some suspicious HighDPI calculations (as mentioned by @DalijaPrasnikar), but these don't seem to affect at least this behaviour.. Commented Aug 9 at 9:05

1 Answer 1

0

Following is the content of the issue report posted by Olaf Monien https://embt.atlassian.net/servicedesk/customer/portal/3/RSB-209

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…)

3
  • Note: this is still experimental. I am not an ActiveX expert. Some EMBT engineers are currently looking at this issue. No guarantees on how long it will take for a final fix. Commented Aug 13 at 16:27
  • @OlafMonien 1) I would like to bring something to your attention. This is IMPORTANT. TcSysTray is a non-visual component. When you place TcSysTray on a form, its width and height are fixed at 0 (zero). However, when you close the application with the X button, no error occurs. The error is caused by the ActiveX component located in the 'SampleComCtrl' folder in the provided link, specifically the 'CustmBtn.ocx' object. This component is a visual one. When you place this component on a form, it does not take on a width and height value of 0 (zero). Commented Aug 13 at 22:57
  • @OlafMonien 2) There are no problems with the width and height values. However, when you close the application with the X button, a 'floating point divide by zero at' error occurs. Note: Since TCustomButton is already present in Delphi, don't forget to rename this class in the automatically generated Wrapper class. Commented Aug 13 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.