简体   繁体   English

如何使groupbox的背景透明?

[英]How to make a groupbox's background transparent?

How to set Groupbox to transparent? 如何将Groupbox设置为透明? 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. 我发现这是透明的 ,但在我的情况下,我把TImage放了背景图像,然后是Groupbox,我不知道如何让groupbox透明,并将图像显示为背景。

I tried searching this on google, but can't find the answer, and as much as possible, i want to use VCL Application. 我尝试在谷歌搜索这个,但找不到答案,并尽可能地,我想使用VCL应用程序。

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: 输出如下所示:

在此输入图像描述

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. 如果需要,在Canvas.Draw调用中指定不同的坐标。 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. 如果你需要背景来覆盖整个父画布,那么你对Draw的调用需要传递-Left-Top作为坐标。

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. 通过不调用继承的Paint方法并完成自己的工作来做到这一点。

To avoid flicker, you are actually best off moving this painting code into WM_ERASEBKGND . 为了避免闪烁,你最好将这个绘画代码移动到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. 如果你要正确地做到这一点,你需要制作一个合适的组件,而不是用插入器进行黑客攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM