Transparent group box

I inherited a native Delphi control from TGroupBox and overrides its Paint method to draw a rounded rectangle.

procedure TclTransparentGroupBox.CreateParams(var params : TCreateParams); begin inherited; Params.ExStyle := params.ExStyle or WS_EX_TRANSPARENT; end; 

After overriding the Create parameters, the Paint method is as follows.

  procedure TclTransparentGroupBox.Paint; begin // Draw the rounded rect to show the group box bounds Canvas.Pen.Color := clWindowFrame; Canvas.RoundRect(5, 15, ClientRect.Right - 5, ClientRect.Bottom - 5, 10, 10); if Caption <> EmptyStr then begin Canvas.Brush.Style := bsClear; Canvas.TextOut(10, 0, Caption); end; end; 

The main problem I am facing is that I have few shortcuts on top of a transparent group field. When I open the form, the labels look great, but when the text changes, some bounding rectangles of the labels will be visible. It looks weird on top of a transparent box.

Even when I resize the form, the group box itself disappears, when I change focus to another application and return my application, the group block draws itself.

Am I missing something regarding the drawing? Any windows messages I should care about ???

Thanks in advance Rahul

+4
source share
1 answer

To make transparent control, you must:

Make it opaque

 ControlStyle := ControlStyle - [csOpaque] 

Process WM_ERASEBKGND:

 procedure TTransPanel.WM_ERASEBKGND(var Msg: TWM_ERASEBKGND); var SaveDCInd: Integer; Position: TPoint; begin SaveDCInd := SaveDC(Msg.DC); //save device context state (TCanvas does not have that func) GetViewportOrgEx(Msg.DC, Position); SetViewportOrgEx(Msg.DC, Position.X - Left, Position.Y - Top, nil); IntersectClipRect(Msg.DC, 0, 0, Parent.ClientWidth, Parent.ClientHeight); try Parent.Perform(WM_ERASEBKGND, Msg.DC, 0 ); Parent.Perform(WM_PAINT, Msg.DC, 0); //or // Parent.Perform(WM_PRINTCLIENT, Msg.DC, prf_Client); //Themeing except end; RestoreDC(Msg.DC, SaveDCInd); Canvas.Refresh; Msg.Result := 1; //We painted out background end; 

In the above process, you first save the state of the device context, and then draw the canvas of our parent (possibly TForm) onto our canvas (TGroupBox). At the end, restore the DC and return 1 to indicate that we were painting the background.

+3
source

Source: https://habr.com/ru/post/1391849/


All Articles