Mastodon
Programmierung

Transparent Controls

ransparent Background for Controls
(from Peter Below)

>
> how would I make a component with a transparent background?
> e.g. it shows the background of the parent or other components through it.
>
>

james,

lets play through it for a groupbox descendant, since i happen to have an
example for that .

To do that kind of stuff right, create a new control derived from TGroupbox,
override its CreateParam method like this:

[code lang=”delphi”] private // in control declaration
Procedure CreateParams( Var params: TCreateParams ); override;

Procedure TTransparentGroupbox.CreateParams( Var params: TCreateParams );
begin
inherited CreateParams( params );
params.ExStyle := params.ExStyle or WS_EX_TRANSPARENT;
end;

Add a handler for the WM_ERASEBKGND message:

Procedure WMEraseBkGnd( Var msg: TWMEraseBkGnd );
message WM_ERASEBKGND;

Procedure TTransparentGroupbox.WMEraseBkGnd( Var msg: TWMEraseBkGnd );
begin
SetBkMode( msg.DC, TRANSPARENT );
msg.result := 1;
end;
[/code]
That is the basic frame for a TWinControl descendent. For a TGraphicsControl
you would drop the CreateParams (since only WinControls have that method) and
override the create constructor. After calling the inherited constructor you
modify the ControlStyle of the control:
[code lang=”delphi”]
ControlStyle := ControlStyle – [csOpaque];

[/code]
Transparency actually works better for TGraphicControls than for
TWinControls. The latter have problems if the control is moved or the
background needs to change. Delphi container controls (like form or panel)
are always created with the WS_CLIPCHILDREN style, which automatically
excludes the area under child controls from updates, so the background will
not be updated if required. Removing the WS_CLIPCHILDREN style from a
controls parent is possible with

[code lang=”delphi”] SetWindowLong( parent.handle, GWL_STYLE,
GetWIndowLong( parent.handle, GWL_STYLE )
and not WS_CLIPCHILDREN );
[/code]
But that may lead to accessive flicker on screen updates.

Peter Below (TeamB)
100113.1101@compuserve.com

[tags]Delphi, Components[/tags]

0 Kommentare zu “Transparent Controls

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.