3

グループ ボックスでラジオ ボタンの背景色を制御したいのですが、ラジオ ボタンが透明ではありません。テーマが有効になっており、ラジオボタンの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ラジオボタンの色を他の場所に設定していません。これが実行されているときに確認しました。

4

1 に答える 1

5

質問に投稿された写真と、コードとコードの説明との間に矛盾があります(質問に対する私のコメントを参照してください)。グループ ボックスの背景は、グループ ボックスのペイント サイクルでペイントする必要があります。以下は動作します (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;


次のように見えます:
デフォルトウィンドウ スタイル, スタイル付きアメジスト カムリ スタイル

于 2012-05-21T15:38:23.687 に答える