Mastodon
Programmierung

Transparent WinControls

In article <7kv799$ efd8@forums.borland.com >, Robert wrote:
> WMEraseBkgnd message fills the TWincontrol with the brush color of the
> control, that makes the control opaque. Is it posible to make a
> TWincontrol transparent?

Well, you have half the answer: since it is WM_ERASEBKGND that causes
the background fill, trap the message and block it.

Here is my standard reply on this transparent issue:

> Please give me help on how to make a descendent class of TGroupBox
> transparent. Thanks in advance.

Scott,

a quick & dirty method is

[code lang=”delphi”]
groupbox1.Brush.style := bsclear;
groupbox1.handleneeded;
setwindowlong( groupbox1.handle, GWL_EXSTYLE, WS_EX_TRANSPARENT );[/code]

in the forms OnCreate event handler.

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

)
No e-mail responses, please, unless explicitly requested!

[tags]Delphi, Components[/tags]

0 Kommentare zu “Transparent WinControls

Schreibe einen Kommentar

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