绘画背景从TSeStyleFont

S.M*_*HDI 1 delphi delphi-xe2 vcl-styles

我正试图从TSeStyleFont中绘制vcl样式背景,就像在Bitmap样式设计器中一样..有没有办法绘制背景?

在此输入图像描述

我试了一下: - 使用DrawElement在位图中首先绘制对象. - 比使用'Bitmap.Canvas.CopyRect'将当前位图复制到另一个干净的位图,问题是:这个方法对于具有Glyph的对象(如CheckBox)无法正常工作...

  var
  bmp, bmp2: TBitmap;
  Details: TThemedElementDetails;
  R, Rn: TRect;
begin
  bmp := TBitmap.Create;
  bmp2 := TBitmap.Create;
  R := Rect(0, 0, 120, 20);
  Rn := Rect(0 + 4, 0 + 4, 120 - 4, 20 - 4);
  bmp.SetSize(120, 20);
  bmp2.SetSize(120, 20);
  Details := StyleServices.GetElementDetails(TThemedButton.tbPushButtonHot);
  StyleServices.DrawElement(bmp.Canvas.Handle, Details, R);
  bmp2.Canvas.CopyRect(R, bmp.Canvas, Rn);
  Canvas.Draw(10, 10, bmp2);
  bmp.Free;
  bmp2.Free;

end;
Run Code Online (Sandbox Code Playgroud)

RRU*_*RUZ 5

如果要绘制按钮的背景,则必须使用StyleServices.DrawElement传递正确TThemedButton部分的 方法.

试试这个样本

uses
  Vcl.Styles,
  Vcl.Themes;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  Details : TThemedElementDetails;
begin
  Details := StyleServices.GetElementDetails(tbPushButtonPressed);
  StyleServices.DrawElement(PaintBox1.Canvas.Handle, Details, PaintBox1.ClientRect);

  Details := StyleServices.GetElementDetails(tbPushButtonNormal);
  StyleServices.DrawElement(PaintBox2.Canvas.Handle, Details, PaintBox2.ClientRect);
end;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果你想绘制没有角落的背景,你可以调整TRect类似的边界

  Details : TThemedElementDetails;
  LRect   : TRect;
begin
  LRect:=PaintBox1.ClientRect;
  LRect.Inflate(3,3);

  Details := StyleServices.GetElementDetails(tbPushButtonPressed);
  StyleServices.DrawElement(PaintBox1.Canvas.Handle, Details, LRect);

  LRect:=PaintBox2.ClientRect;
  LRect.Inflate(3,3);
  Details := StyleServices.GetElementDetails(tbPushButtonNormal);
  StyleServices.DrawElement(PaintBox2.Canvas.Handle, Details, LRect);
end;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述