Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to make a TPanel transparent?

5,023 views
Skip to first unread message

Nick Barrett

unread,
Nov 19, 2001, 8:54:32 PM11/19/01
to
Does any know how to make a TPanel that is transparent? I found one at
Torry.net but it was for D1 and D3 and I use D6. I did manage to load it
but the image wasn't very stable when I moved it around in run time. I was
thinking that TImage has a transparent property that allows a particular
colour in the image to be removed and show what is behind the TImage. Is
there some way I could adapt this to a TPanel component. I had a look at
TImage's code but got very confused very quickly.

TIA =)

Cheers,
Nick Barrett

PS: Maybe I should be posting to borland.public.delphi.graphics?


serge gubenko

unread,
Nov 20, 2001, 3:26:07 AM11/20/01
to

"Nick Barrett" <NBar...@SentryCorp.com> wrote in message
news:3bf9b79d$1_2@dnews...

> Does any know how to make a TPanel that is transparent? I found one at
> Torry.net but it was for D1 and D3 and I use D6. I did manage to load it
> but the image wasn't very stable when I moved it around in run time. I
was
> thinking that TImage has a transparent property that allows a particular
> colour in the image to be removed and show what is behind the TImage. Is
> there some way I could adapt this to a TPanel component. I had a look at
> TImage's code but got very confused very quickly.
>

Hi, Nick

Seems your question (or similar) was discussed in this ng not long ago.
Anyway, to paint a panel with transparent color you should override its
Paint method, draw it there to the buffer bitmap, repaint a part of the
Parent window and then Copy this BitMap to the main Canvas by using a
TCanvas.CopyBrush function.

Here's an example:
_____________________________________________________
TMyTransparentPanel = class(TPanel)
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure Paint; override;
end;

{...}

procedure TMyTransparentPanel.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
if not (csDesigning in ComponentState)
then Params.ExStyle:=Params.ExStyle or WS_EX_TRANSPARENT;
end;

procedure TMyTransparentPanel.Paint;
var
XBitMap: TBitMap;
XOldDC: HDC;
XRect: TRect;
begin
{This panel will be transparent only in Run Time}
if (csDesigning in ComponentState)
then inherited Paint
else begin
XRect:=ClientRect;
XOldDC:=Canvas.Handle;
XBitMap:=TBitMap.Create;
try
XBitMap.Height:=Height; XBitMap.Width:=Width;
Canvas.Handle:=XBitMap.Canvas.Handle;
inherited Paint;
RedrawWindow(Parent.Handle, @XRect, 0,
RDW_ERASE or RDW_INVALIDATE or
RDW_NOCHILDREN or RDW_UPDATENOW);
finally
Canvas.Handle:=XOldDC;
Canvas.BrushCopy(XRect, XBitMap, XRect, Color);
XBitMap.Free;
end;
end;
end;
______________________________________________________

Best regards, Serge Gubenko


Nick Barrett

unread,
Nov 20, 2001, 5:18:41 AM11/20/01
to
> Seems your question (or similar) was discussed in this ng not long ago.

Sorry about that.
Thank you very much for your help =)

Cheers,
Nick Barrett


0 new messages