Display not drawn correctly in Delphi

I’ve inherited a project that now extending. Unfortunately it hast to be done in Delphi which is not my strongest language. It seems that the display is not drawn correctly, when I do the following:

procedure Display.FormCreate(Sender: TObject);
  cvDisp.Image := cvImg.Image;

Crosschecking with C# the following works fine:

public MainForm()
  axCVdisplay1.Image = axCVimage1.Image;

I would conclude not that this is a Delphi issue. However, all the other controls are drawn correctly, and the display is not drawn at all - you can seed the desktop behind the form.

PS: I would add a screenshot, but I guess my desktop violates the terms of service :sunglasses:.

Can you provide more details about “is not drawn correctly”? What exactly do you see? If you are concerned about us seeing the rest of your desktop you can always crop it to the area where the Display Control appears (or is supposed to appear…).

If memory serves, you have to assign the image to the display OCX in the OnShow() for Delphi. I can’t recall why that is though. Might be just a Delphi quirk… :confounded:

1 Like

Thanks, for this fast answer. What I see/saw is that the area, where the display should be there is just a hole in the form showing part of my desktop background - or more likely, old graphics memory.
Just tried to reproduce it on a different computer, works fine now…:confounded:
Might be related to the the OS - (it’s still XP :innocent: on the target machine) or the even the graphics card…

@Frank I did a quick test, and FormShow() works. I will test this on the target machine.

That might in fact be the reason and then this is in fact only semi-specific to Delphi and I would not go as far as to dub it a Delphi quirk as the underlying problem is the same for Delphi as well for all the other languages and GUI toolkits. The Display Control is basically just a regular ActiveX control and no matter what toolkit you use, you are always operating on a wrapper class that in one way or another your development environment has created for the ActiveX control that you are using.

Control creation - although it is often mapped to a seemingly innocent constructor in these wrapper classes - is more complex than it seems because the control needs to be created into a control host and that control host in turn needs a suitably prepared window to work on. To get an idea of all the steps involved have a look at the code that C# creates in InitializeComponent for a form that uses ActiveX controls or (for the full story) at the source code of COleControland COleControlContainer in the MFC.

The bottom line is: Creating and/or initializing an ActiveX control too early can lead to all kinds of problems (undefined behavior, exceptions, … these effects are in fact specific to the development environment you are using). Specifically, use of ActiveX controls is not safe in…

  • Delphi before the form’s base class’s OnShow handler has been processed.
  • MFC/Visual C++ before the dialog’s base class’s OnInitDialog has been processed.
  • Windows Forms applications before the the form’s InitializeComponents method has been called.

Just tested - and it works when I assign the image in the OnShow event. However if this is not safe either, what should I do?

Why is that not safe either? Don’t mix up calling of your overload of OnShow and the invocation of the OnShow implementation of the base class of your form… Doing the initialization in your overload of OnShow is safe!

1 Like

Ok, I mixed it up.
So, the OnShow event is safe as it happens after the OnShow procedure call of the base class.