1

How to set Groupbox to transparent? I found this Transparent, But in my case, I put TImage and put a background image, then the Groupbox, I don't know how can I make the groupbox transparent, and show the image as a background.

I tried searching this on google, but can't find the answer, and as much as possible, i want to use VCL Application.

Community
  • 1
  • 1
Dakoy
  • 53
  • 2
  • 12
  • So do you want a TRANSPARENT groupbox (that is, group box with NO BACKGROUND (and if nitpicking - with now visible caption and no borderlines as well)) or do you want a groupbox WITH BACKGROUND PCITURE (visible picture - that means non-transparent) ? – Arioch 'The Oct 23 '13 at 08:27
  • the first one, transparent groupbox with no background, with visible caption and borderline, is this possible sir? – Dakoy Oct 23 '13 at 08:31
  • JediVCL ( http://jvcl.sf.net ) comes with JvGGroupBox.pas having TJvgGroupBox class having property Transparent - you may try to look how it was implemented. Also to some extent you maybe also actualyl need http://docwiki.embarcadero.com/Libraries/XE3/en/Vcl.Controls.TWinControl.ParentBackground – Arioch 'The Oct 23 '13 at 08:42
  • DeveloperExpress components also has transparent property, TCxRadioGroupBox in your case – Agustin Seifert Oct 23 '13 at 14:36

1 Answers1

4

I think you'll need to take over painting the group box. Here's a simple example using an interposer class. Place this class in the same unit as your form, before your form is declared:

type
  TGroupBox = class(StdCtrls.TGroupBox)
  protected
    procedure Paint; override;
  end;

  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    ....
  end;

....

procedure TGroupBox.Paint;
begin
  Canvas.Draw(0, 0, SomeGraphicObjectContainingYourBackground);
  inherited;
end;

The output looks like this:

enter image description here

You may want to customise the rest of the painting. Perhaps it's enough to draw the background inside the group box so that the caption and frame appear as normal. Specify different coordinates in the call to Canvas.Draw if you want that. If you need the background to cover the entire parent canvas then your call to Draw needs to pass -Left and -Top for the coordinates.

Or perhaps you want to take over the drawing of the frame. Do that by not calling the inherited Paint method and doing your own work.

To avoid flicker, you are actually best off moving this painting code into WM_ERASEBKGND. That makes things a little more complex, but not much. The code would look like this:

type
  TGroupBox = class(StdCtrls.TGroupBox)
  protected
    procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;
  end;

procedure TGroupBox.WMEraseBkgnd(var Message: TWmEraseBkgnd);
begin
  Canvas.Handle := Message.DC;
  try
    Canvas.Draw(-Left, -Top, SomeGraphicObjectContainingYourBackground);
  finally
    Canvas.Handle := 0;
  end;
  Message.Result := 1;
end;

If you were going to do this properly, you'd want to make a proper component rather than hacking around with an interposer.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • looks like suggested here http://stackoverflow.com/questions/18925394/delphi-bring-timage-to-front – bummi Oct 23 '13 at 08:44
  • Now I usually avoid comments like this, but the output of your code made me chuckle. Have a +1. – FLClover Oct 24 '13 at 10:17