我想让一个组框控制单选按钮的背景颜色,但单选按钮不透明。主题已启用,我将radibackground设置为true,用于radiobutton。
这就是我得到的:
我希望通过radiobutton显示蓝色渐变,而不使用任何颜色作为radiobutton的背景。
我正在调用此方法,使用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;
来自重写的绘画功能单选按钮所在的组框,它不通过单选按钮控件显示。我没有在其他任何地方设置radiobutton的颜色,我在运行时检查了ParentBackground = true
。
答案 0 :(得分: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;
看起来像:
默认,样式为