6

Who paints TTimer at design time ?

When using the Delphi IDE's form designer, when you drop a TTimer on the form, the component is visible at design time (but, of course, non-visual at runtime).

The TTimer class is defined in unit ExtCtrls, so of course I did read the TTimer source code in that unit.

I was expecting to see something like this:

procedure TTimer.Paint;
begin
  if csDesigning in ComponentState then
    with Canvas do
    begin
      // Paint the design-time appearance of TTimer here:

      // ... code ...

    end else begin end; // no painting at runtime.
end;

But I was surprised to see no such code!

The TTimer component has this private field: FWindowHandle: HWND;, but that is only used to receive the WM_Timer message from windows itself. It is not used to paint anything, even at design time. And no canvas either.

While reading the TTimer source code, I could not find anything related to design time painting.

So the question is: what code and where is responsible to paint the TTimer's design time appearance on the form in the form designer of the Delphi IDE itself.

Now, if someone wonders why I ask this question, here's some information about that:

Elsewhere on StackOverflow someone asked if it is possible to load a .dfm file at runtime. Someone answered: "No, it is not possible".

But that is not exactly true. I have written some code to do exactly this: load "someform.dfm" from disk and create such form at runtime. That is possible, but the nuisance is that then you need to write code like this:

procedure RegisterNecessaryClasses;
begin
  RegisterClass(TfrmDynaForm);
  RegisterClass(TPanel);
  RegisterClass(TMemo);
  RegisterClass(TTimer);
  RegisterClass(TListBox);
  RegisterClass(TSplitter);
  RegisterClass(TEdit);
  RegisterClass(TCheckBox);
  RegisterClass(TButton);
  RegisterClass(TLabel);
  RegisterClass(TRadioGroup);
end;

This is just a first example that lets me load and present one particular form without errors. But, as soon as some other form contains, for example: TSpeedbutton, then the above procedure needs to be edited to add this line:

  RegisterClass(TSpeedbutton);

Skip that, and you'll have a "class Txxx not found" -exception.

Another problem is that even after I added code to find any TTimer components in the dfm to load, and I manually have set csDesigning in the ComponentState of that TTimer instance, the TTimer still stays invisible.

So what (else) do I need to do to make the TTimer visible the same way it is visible in the Delphi IDE's form designer?

Community
  • 1
  • 1
mika
  • 174
  • 1
  • 2
  • Direct `TComponent` descendants (non-visual components) doesn't support drawing. It is the Delphi IDE's form designer who is handling non-visual components (including their drawing) and if you want something similar, make a visual component which will draw a similar box with the design time icon. – TLama Apr 19 '14 at 19:16
  • Note that painting begins only with `TControl` on inheritance tree. – Free Consulting Apr 19 '14 at 23:42
  • Despite of your explanatory efforts, I am not seeing the reason for painting a TTimer component when loading a DFM at runtime. – NGLN Apr 20 '14 at 08:12

1 Answers1

5

Non-visual components have no Paint method and so are not capable of painting themselves.

Who paints TTimer at design time?

The IDE paints the representation of non-visual components.

So what (else) do I need to do to make the TTimer visible the same way it is visible in the Delphi IDE's form designer?

You have to paint it in your code. Non-visual components will not paint themselves.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    *"You have to paint it in your code."*, that's pretty vague and sounds a bit like you can just make a subclass and override `Paint` method (which OP expected to find). Attempting to make visual control (with focus and drag & drop support just like form designer does) from non-visual component requires more than this. Easiest I can think of is making some `TNonVisual` visual control which would paint the box with the icon and would be bound with the component instance through some linked property. – TLama Apr 20 '14 at 10:35