Author: Tomas Rutkauskas
I have a TPaintBox that I use to draw a representation of the data the user is
entering. It updates whenever the form is repainted or data is changed. This works
fine. Using D5, I've noticed that when the user is moving the mouse around causing
hints to pop up and down, it causes a tremendous amount of flicker. In part, this
is caused by the fact that I clear the canvas before redrawing. Should I draw to an
invisible paintbox and then copy to a TImage?
Answer:
1
2 { Overrides the WM_ERASEBKGND message in TWinControl and TForm to prevent flicker
3 of csOpaque child controls.
4 Unpublished; (c) 1999, David Best, davebest@usa.net
5 You are free to use this and derived works provided you acknowlege it's source in
6 your code.}
7
8 procedure WMEraseBkgndEx(WinControl: TWinControl; var message: TWmEraseBkgnd);
9 var
10 i, Clip, SaveIndex: Integer;
11 begin
12 { Only erase background if we're not doublebuffering or painting to memory }
13 with WinControl do
14 if not DoubleBuffered or (TMessage(message).wParam = TMessage(message).lParam)
15 then
16 begin
17 SaveIndex := SaveDC(message.DC);
18 Clip := SimpleRegion;
19 if ControlCount > 0 then
20 begin
21 for i := 0 to ControlCount - 1 do
22 if not (Controls[i] is TWinControl) then
23 {child windows already excluded}
24 with Controls[i] do
25 begin
26 if (Visible or (csDesigning in ComponentState) and not
27 (csNoDesignVisible in ControlStyle))
28 and (csOpaque in ControlStyle) then
29 begin
30 Clip := ExcludeClipRect(message.DC, Left, Top, Left +
31 Width, Top + Height);
32 if Clip = NullRegion then
33 break;
34 end;
35 end;
36 end;
37 if Clip <> NullRegion then
38 FillRect(message.DC, ClientRect, Brush.Handle);
39 RestoreDC(message.DC, SaveIndex);
40 end;
41 message.Result := 1;
42 end;
43
44 procedure TNoFlickerForm.WMEraseBkGnd(var msg: TWMEraseBkGnd);
45 begin
46 WMEraseBkgndEx(Self, msg);
47 end;
|