views:

390

answers:

2

I'm trying to write a custom TGraphicControl descendant that will allow a user to embed a SDL rendering surface on a VCL form. This will provide a simple way to display SDL animations in a form.

Apparently a TGraphicControl is redrawn with the Paint method. How often does this get called? Is it only when something happens to invalidate the control, or are there other things that call it? How does that interact with something that does its own drawing?

+5  A: 

The Paint method of a non-windowed control (which TGraphicControl is) is called when the windowed control underneath redraws itself. This happens when a WM_PAINT message for the windowed control is processed.

A windowed control is redrawn either when its entire area or parts of it have been invalidated, or when another window that was higher in the Z-order is closed or moved, so that the window is exposed and needs to be redrawn.

Invalidating a TGraphicControl will calculate the corresponding part of the windowed parent control, and invalidate this part.

If you need a TGraphicControl to redraw itself then you need to call Invalidate on it or its parent control. There is no other way to cause a redraw.

mghie
+1  A: 

Question 1: How often does this get called?

See answer Mghie: whenever the parent window control processes the WM_PAINT message

Question 2: Is it only when something happens to invalidate the control, or are there other things that call it?

See answer Mghie: only when it's invalidated

Question 3: How does that interact with something that does its own drawing?

You must put your own drawning routine in the Paint method of TGraphicControl descendant - using the override directive. Drawing in the Paint method will not send another WM-PAINT message. While in the Paint method you can draw on the canvas without getting in a WM-PAINT message loop. Make your drawing code as fast as possible because there can be quite some WM-PAINT messages flying around. If your drawing routine takes a lot of time, your application will appear sluggish/non-responsive.

Kris De Decker
You make it sound like you could create some sort of endless paint loop if you paint on the control outside a wm_Paint event. That's not the case. You can paint on a control any time the canvas is available, which is pretty much whenever the parent control has a window handle.
Rob Kennedy