Rah*_*l W 5 delphi groupbox transparent-control delphi-6
我继承了Delphi本机控件的TGroupBox,并重写了它的Paint方法来绘制圆角矩形。
procedure TclTransparentGroupBox.CreateParams(var params : TCreateParams);
begin
inherited;
Params.ExStyle := params.ExStyle or WS_EX_TRANSPARENT;
end;
Run Code Online (Sandbox Code Playgroud)
覆盖创建参数后,Paint方法如下。
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;
Run Code Online (Sandbox Code Playgroud)
我面临的主要问题是,我在透明组框顶部没有几个标签。当我打开表单时,标签看起来很好,但是当文本更改时,标签的某些边界矩形将可见。这在透明框的顶部看起来很奇怪。
即使当我调整表单大小时,组框本身也会消失,当我将焦点更改到另一个应用程序并恢复我的应用程序时,组框会自动绘制。
我在绘画方面缺少任何东西吗?我需要照顾的任何Windows消息???
在此先感谢拉胡尔
要使控件透明,您必须:
使其不透明
ControlStyle := ControlStyle - [csOpaque]
Run Code Online (Sandbox Code Playgroud)
处理 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;
Run Code Online (Sandbox Code Playgroud)
在上面的过程中,您首先保存设备上下文状态,然后将我们的父级(可能是 TForm)的画布绘制到我们的画布(TGroupBox)上。最后恢复 DC 并返回 1 表示我们确实绘制了背景。