【问题标题】:Why isn't my radio button transparent?为什么我的单选按钮不透明?
【发布时间】:2012-05-21 14:31:31
【问题描述】:

我想让一个组合框控制单选按钮的背景颜色,但单选按钮不透明。主题已启用,我将单选按钮的 parentbackground 设置为 true。

这是我得到的:

我希望通过单选按钮显示蓝色渐变,并且不使用任何颜色作为单选按钮的背景。

我正在调用这个方法,用 GDI+ 在组框上绘图:

procedure TMyRadio.PaintBackground(AParentColor : TColor; Graphics: IGPGraphics);
var
  ExpandedRect : TGPRect;
  Path : IGPGraphicsPath;
  GradientBrush : IGPPathGradientBrush;
  SurroundColors : array[0..0] of TGPColor;
begin

  ExpandedRect := TGPRect.Create(ExpandBorder(BoundsRect, 10));
  Path := TGPGraphicsPath.Create;
  Path.AddRectangle(ExpandedRect);
  GradientBrush :=  TGPPathGradientBrush.Create(Path);

  GradientBrush.CenterColor := TGPColor.Create(255,
                                               GetRValue(ColorToRGB(BackgroundColor)),
                                               GetGValue(ColorToRGB(BackgroundColor)),
                                               GetBValue(ColorToRGB(BackgroundColor))
                                               );

  SurroundColors[0].Initialize(0,
                               GetRValue(ColorToRGB(AParentColor)),
                               GetGValue(ColorToRGB(AParentColor)),
                               GetBValue(ColorToRGB(AParentColor))
                               );

  GradientBrush.SetSurroundColors(SurroundColors);
  Graphics.FillRectangle(GradientBrush, ExpandedRect);

end;

从覆盖的绘制函数中,单选按钮所在的组框不会通过单选按钮控件显示。我没有在其他任何地方设置单选按钮的颜色,并且在运行时我已经检查过ParentBackground = true

【问题讨论】:

  • 是否启用了双缓冲,例如DoubleBuffered := True?有时这会导致控件出现图形问题。如果是,则通过代码或从 Object Inspector 中的父控件将其设置为 false。
  • @blobby,两种方法我都试过了。我也试过这个:stackoverflow.com/questions/2461793/… 并在没有主题的情况下运行它(在终端服务器上),它看起来很糟糕。所以无论如何我都可以选择纯色。
  • 我认为这可能是第 3 方组件可能更适合您尝试做的事情的情况之一。
  • BoundsRect 如果 PaintBackground 是单选组的方法,则应该引用单选组的边界矩形。但是从您发布的图片看来,矩形是根据单选按钮的边界矩形计算的。你确定你覆盖了组框的 Paint,而不是单选按钮?
  • 我使用 Dream Controls 对 XE2 稍作修改

标签: delphi radio-button transparency


【解决方案1】:

问题中发布的图片与代码和代码描述之间存在不一致(请参阅我对问题的评论)。组框的背景应该在组框的绘制周期中绘制。下面的作品(用XE2测试,单选按钮是透明的):

type
  TMyGroupBox = class(TGroupBox)
  protected
    procedure Paint; override;
    procedure PaintBackground(AParentColor : TColor; Graphics: IGPGraphics);
  end;

  ..

procedure TMyGroupBox.Paint;
var
  G: IGPGraphics;
begin
  inherited;
  G := TGPGraphics.Create(Canvas.Handle);
  PaintBackground(clYellow, G);
end;

procedure TMyGroupBox.PaintBackground(AParentColor: TColor; Graphics: IGPGraphics);
var
  ..
begin
  // Same as in the question. Of course the rectangle should be calculated
  // based on the positions of radio button...
  ..

以下是如何将其与样式结合的示例(我想知道这是否容易):

uses
  .., gdiplus;

type
  TGroupBoxStyleHook = class(vcl.stdctrls.TGroupBoxStyleHook)
  strict protected
    procedure PaintBackground(Canvas: TCanvas); override;
  end;

  TGroupBox = class(vcl.stdctrls.TGroupBox)
  private
    FButtonSize: Integer;
    FGradientMargin: Integer;
    function GetButtonSize: Integer;
  protected
    procedure Paint; override;
    procedure PaintBackground(AParentColor: TColor; Graphics: IGPGraphics);
    procedure CMWininichange(var Message: TMessage); message CM_WININICHANGE;
    property ButtonSize: Integer read GetButtonSize;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property GradientMargin: Integer
        read FGradientMargin write FGradientMargin default 10;
  end;

  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    Button1: TButton;
  end;

var
  Form1: TForm1;

implementation

uses
  themes, uxtheme;

{$R *.dfm}


{ TGroupBoxStyleHook }

procedure TGroupBoxStyleHook.PaintBackground(Canvas: TCanvas);
var
  G: IGPGraphics;
begin
  inherited;
  G := TGPGraphics.Create(Canvas.Handle);
  (Control as TGroupBox).PaintBackground(clYellow, G);
end;


{ TGroupBox }

constructor TGroupBox.Create(AOwner: TComponent);
begin
  inherited;
  FGradientMargin := 10;
  TCustomStyleEngine.RegisterStyleHook(TCustomGroupBox, TGroupBoxStyleHook);
end;

procedure TGroupBox.CMWininichange(var Message: TMessage);
begin
  FButtonSize := 0;
  inherited;
end;

function TGroupBox.GetButtonSize: Integer;
var
  Size: TSize;
begin
  Result := FButtonSize;
  if StyleServices.Enabled and (Result = 0) then begin
    TStyleManager.SystemStyle.GetElementSize(0,
        TStyleManager.SystemStyle.GetElementDetails(tbRadioButtonCheckedNormal),
        TRect.Empty, esActual, Size);
    FButtonSize := Size.cx;
    Result := FButtonSize;
  end;
end;


procedure TGroupBox.PaintBackground(AParentColor: TColor; Graphics: IGPGraphics);
const
  BackGroundColor = clBlue;
var
  R: TRect;
  i: Integer;

  ExpandedRect : TGPRect;
  Path : IGPGraphicsPath;
  GradientBrush : IGPPathGradientBrush;
  SurroundColors : array[0..0] of TGPColor;
begin
  for i := 0 to ControlCount - 1 do begin
    if Controls[i] is TRadioButton then begin

      // Don't know what ExpandBorder is.
      //  ExpandedRect := TGPRect.Create(ExpandBorder(BoundsRect, 10));
      R := Controls[i].BoundsRect;
      R.Inflate(GradientMargin, GradientMargin);
      R.Right := R.Left + 2 * GradientMargin + ButtonSize;
      ExpandedRect := TGPRect.Create(R);

      Path := TGPGraphicsPath.Create;
      Path.AddRectangle(ExpandedRect);
      GradientBrush :=  TGPPathGradientBrush.Create(Path);

      GradientBrush.CenterColor := TGPColor.Create(255,
                                         GetRValue(ColorToRGB(BackgroundColor)),
                                         GetGValue(ColorToRGB(BackgroundColor)),
                                         GetBValue(ColorToRGB(BackgroundColor))
                                         );
      SurroundColors[0].Initialize(0,
                                   GetRValue(ColorToRGB(AParentColor)),
                                   GetGValue(ColorToRGB(AParentColor)),
                                   GetBValue(ColorToRGB(AParentColor))
                                   );
      GradientBrush.SetSurroundColors(SurroundColors);
      Graphics.FillRectangle(GradientBrush, ExpandedRect);
    end;
  end;
end;

procedure TGroupBox.Paint;
var
  G: IGPGraphics;
begin
  //  'Paint' is not called with styles other than the SystemStyle
  inherited;

  // Do not draw the background with no runtime themes (no default transparency)
  if StyleServices.Enabled then begin
    G := TGPGraphics.Create(Canvas.Handle);
    PaintBackground(clRed, G); // I don't notice any effect of color passed here
  end;
end;


看起来像:
默认,样式

【讨论】:

  • PaintBackground 是 RadioButton 类的成员。我不能覆盖单选按钮的绘制功能吗?
  • @Peter - 不应该。您不应覆盖单选按钮的任何方法。正如我所说,您应该在自己的绘制周期中绘制组框。
  • 我没有覆盖单选按钮的任何方法。我只从组框的绘制函数中调用 PaintBackground 函数,我发布的是我在绘制函数中绘制组框的背景。不过,我正在测试您的代码,问题可能出在 10 英里以下,导致运行时主题无法正常工作。
  • 不,它看起来与我的屏幕截图中的完全不一样,没有透明度。不过,我今天会用这些样式尝试这些新东西。
  • 终于按照我的喜好完成了所有工作,再次感谢您的帮助,我在获取样式详细信息中将 esActual 更改为 esStretch(并传入单选按钮的 DC),因为我缩放在这些控件上进出,效果非常好。我还使用 types.pas 中的 SplitRect 和 CenteredRect 函数来获取单选按钮周围的确切矩形,以防它比预期的高一点。
猜你喜欢
  • 2011-05-24
  • 2019-03-18
  • 2015-03-09
  • 2010-10-30
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2013-08-29
相关资源
最近更新 更多