Realize transparent controls such as Panel Groupbox

zhaozj2021-02-16  54

The Blog of 9CBS is open. I think the blog here is when I answer someone else's problems in the future, the place where the collection answers is very good.

Because the community's post will be sinking in the morning and evening, it is very inconvenient to find it, and even if you can't even find it.

Q: http://community.9cbs.net/expert/topicview.asp?id=3106090

The transparency of the transparency of some standard container type controls is to achieve some standards, maybe it is often thought. In fact, it is quite easy to implement under the system of more than 2,000.

You don't need to redraw the effect of the parent control, everything is because the window has the attribute of WS_EX_TRANSPARENT.

Below we will illustrate TPANEL and TGROUPBOX controls. We inherit two new controls, TTRANSPANEL and TTRANSGROUPBOX.

class TTransGroupBox: public TGroupBox {void __fastcall CreateParams (Controls :: TCreateParams & Params) {TGroupBox :: CreateParams (Params); Params.ExStyle = WS_EX_TRANSPARENT;} void __fastcall AdjustColors (TPanelBevel Bevel, TColor & TopColor, TColor & BottomColor) {TopColor = clBtnHighlight ; if (Bevel == bvLowered) TopColor = clBtnShadow; BottomColor = clBtnShadow; if (Bevel == bvLowered) BottomColor = clBtnHighlight;} void __fastcall Paint () {int H; TRect R; Longint Flags; Canvas-> Font = this- > Font; h = canvas-> textheight ('0'); r = Rect (0, h / 2 - 1, width, height); if (CTL3D) {R.LEFT ; R.TOP ; Canvas -> brush-> color = CLBTNHIGHLIGHT; canvas-> framerect (r); offsetRect (r, -1, -1); canvas-> brush-> color = clbtnshadow;} else canvas-> brush-> color = clwindowframe; Canvas-> frameect (r); if (TEXT! = ") {If (! Userighttoleftalignment ()) R = Rect (8, 0, 0, h); Else R = Re (R.right - canvas-> Textwidth (Text) - 8, 0, 0, h); Flags = DrawTextBidimodeflags (DT_SINGLINE); DRAWText (canvas-> handle, Text.c_Str (), Text.Length (), & R, Flags | DT_CALCRECT); canvas-> brush-> color = color; canvas-> brush-> style = bsclear; DrawText (canvas-> handle, text.c_str ), Text.length (), & r, flags);}} public: __fastcall virtual ttransgroupbox (tComponent * aowner): TGroupBox (Aowner) {ControlStyle >> csopaque; width = 185;}};

class TTransPanel: public TPanel {void __fastcall CreateParams (Controls :: TCreateParams & Params) {TPanel :: CreateParams (Params); Params.ExStyle = WS_EX_TRANSPARENT;} void __fastcall AdjustColors (TPanelBevel Bevel, TColor & TopColor, TColor & BottomColor) {TopColor = clBtnHighlight ; if (Bevel == bvLowered) TopColor = clBtnShadow; BottomColor = clBtnShadow; if (Bevel == bvLowered) BottomColor = clBtnHighlight;} void __fastcall Paint () {DynamicArray Alignments;

Alignments.set_length

3);

Alignments [TALEFTJUSTIFY] = DT_LEFT;

Alignments [Tacenter] = DT_Center;

Alignments [TARightJustify] = DT_RIGHT;

TRECT RECT;

Tcolor Topcolor, Bottomcolor;

Int fontheight;

Longint flags;

Rect = getClientRect ();

IF (bevelouter! = bvnone)

{

AdjustColors (Bevelouter, Topcolor, Bottomcolor);

Frame3D (Canvas, Rect, Topcolor, Bottomcolor, BevelWidth);

}

Frame3D (Canvas, Rect, Color, Color, Borderwidth);

IF (bevelinner! = bvnone)

{

AdjustColors (Bevelinner, Topcolor, Bottomcolor);

Frame3D (Canvas, Rect, Topcolor, Bottomcolor, BevelWidth);

}

Canvas-> brush-> color = color;

// canvas-> FillRect (Rect); comment out this sentence, it is transparent. The following BSCLEAR guarantees that the CAPTION does not use the background. Canvas-> brush-> style = bsclear;

Canvas-> font =

THIS-> FONT;

Fontheight = Canvas-> TextHeight (

'W');

Rect.top = (Rect.Bottom Rect.Top) - FontHeight) /

2;

Rect.bottom = Rect.top fontheight;

Flags = dt_expandtabs | dt_vcenter | alignments [alignment];

Flags = DrawTextBidimodeflags (Flags);

DrawText (canvas-> handle, caption.c_str (), -

1, & Rect, Flags;

}

PUBLIC:

__fastcall Virtual TTRANSPANEL (Tcomponent * aowner): TPANEL (Aowner) {

CircolStyle >> Csopaque;

Width =

185;

HEIGHT =

41;

}

}

转载请注明原文地址:https://www.9cbs.com/read-21145.html

New Post(0)