delphi – 透明组框

前端之家收集整理的这篇文章主要介绍了delphi – 透明组框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我继承了 TGroupBox的Delphi本机控件并覆盖其Paint方法以绘制圆角矩形.
procedure TclTransparentGroupBox.CreateParams(var params : TCreateParams);
   begin
     inherited;
     Params.ExStyle := params.ExStyle or WS_EX_TRANSPARENT;
   end;

覆盖Create params后,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,Caption);
     end;
   end;

我面临的主要问题是,透明组框顶部的标签很少.当我打开表单时,标签看起来很好,但是当文本更改时,标签的某些边界矩形将是可见的.这在透明盒子上看起来很奇怪.

即使我调整表单大小,组合框本身也会消失,当我将焦点更改为另一个应用程序并带回我的应用程序时,组合框会自行绘制.

我错过了关于绘画的任何内容吗?我需要照顾的任何Windows消息???

提前致谢
拉胡尔

解决方法

要使控件透明,您必须:

使它不透明

ControlStyle := ControlStyle - [csOpaque]

处理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,Parent.ClientWidth,Parent.ClientHeight);
    try
        Parent.Perform(WM_ERASEBKGND,Msg.DC,0 );
        Parent.Perform(WM_PAINT,0);
        //or
        // Parent.Perform(WM_PRINTCLIENT,prf_Client); //Themeing
    except
    end;       
    RestoreDC(Msg.DC,SaveDCInd);
    Canvas.Refresh;       
    Msg.Result := 1; //We painted out background
end;

在上面的过程中,您首先保存设备上下文状态,然后将我们的父(可能是TForm)的画布绘制到我们的画布上(TGroupBox).最后恢复DC并返回1表示我们确实绘制了背景.

猜你在找的Delphi相关文章