# Display not drawn correctly in Delphi

**URL:** <https://forum.commonvisionblox.com/t/display-not-drawn-correctly-in-delphi/76>\
**Category:** C-style API\
**Tags:** display, activex, delphi\
**Created:** [May 15, 2017, 7:36am UTC](https://forum.commonvisionblox.com/t/display-not-drawn-correctly-in-delphi/76 "2017-05-15T07:36:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DeprecatedCoder](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/deprecatedcoder/32/53_2.png) [@DeprecatedCoder](https://forum.commonvisionblox.com/u/DeprecatedCoder)\
**Post date:** [May 15, 2017, 7:36am UTC](https://forum.commonvisionblox.com/t/display-not-drawn-correctly-in-delphi/76/1 "2017-05-15T07:36:32Z")

</div>

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:

```auto
procedure Display.FormCreate(Sender: TObject);
begin
  cvImg.LoadImage('%CVB%\Tutorial\clara.bmp');
  cvDisp.Image := cvImg.Image;
end

```

Crosschecking with C# the following works fine:

```auto
public MainForm()
{
  axCVimage1.LoadImage(@"%cvb%\Tutorial\Clara.bmp");
  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 🕶.

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [May 15, 2017, 7:39am UTC](https://forum.commonvisionblox.com/t/display-not-drawn-correctly-in-delphi/76/2 "2017-05-15T07:39:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Frank](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/frank/32/42_2.png) [@Frank](https://forum.commonvisionblox.com/u/Frank)\
**Post date:** [May 15, 2017, 7:44am UTC](https://forum.commonvisionblox.com/t/display-not-drawn-correctly-in-delphi/76/3 "2017-05-15T07:44:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DeprecatedCoder](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/deprecatedcoder/32/53_2.png) [@DeprecatedCoder](https://forum.commonvisionblox.com/u/DeprecatedCoder)\
**Post date:** [May 15, 2017, 8:11am UTC](https://forum.commonvisionblox.com/t/display-not-drawn-correctly-in-delphi/76/4 "2017-05-15T08:11:50Z")

</div>

> [@illusive](#):
>
> What exactly do you see?

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…😖  
Might be related to the the OS - (it’s still XP 😇 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.

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [May 15, 2017, 8:38am UTC](https://forum.commonvisionblox.com/t/display-not-drawn-correctly-in-delphi/76/5 "2017-05-15T08:38:09Z")

</div>

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 `COleControl`and `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.

---

<div class="post-metadata">

**Author:** ![DeprecatedCoder](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/deprecatedcoder/32/53_2.png) [@DeprecatedCoder](https://forum.commonvisionblox.com/u/DeprecatedCoder)\
**Post date:** [May 15, 2017, 8:51am UTC](https://forum.commonvisionblox.com/t/display-not-drawn-correctly-in-delphi/76/6 "2017-05-15T08:51:14Z")

</div>

> [@illusive](#):
>
> Delphi before the form’s base class’s OnShow handler has been processed.

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?

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [May 15, 2017, 9:21am UTC](https://forum.commonvisionblox.com/t/display-not-drawn-correctly-in-delphi/76/7 "2017-05-15T09:21:15Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![DeprecatedCoder](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/deprecatedcoder/32/53_2.png) [@DeprecatedCoder](https://forum.commonvisionblox.com/u/DeprecatedCoder)\
**Post date:** [May 15, 2017, 9:32am UTC](https://forum.commonvisionblox.com/t/display-not-drawn-correctly-in-delphi/76/8 "2017-05-15T09:32:56Z")

</div>

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