使用GDI +绘制透明框架

时间:2019-02-25 14:59:23

标签: delphi transparency gdi+

我想先绘制一个矩形,然后在其中切一个透明的圆,以便在矩形图像上绘制一个圆形框架。

我的努力:

procedure TTheIndicator._drawFrame(b: TGPBitmap);
const
  FRAME_WIDTH = 10;
var
  g: TGPGraphics;
  brush: TGPSolidBrush;
begin
  g := TGPGraphics.Create(b);
  try
    brush := TGPSolidBrush.Create(MakeColor(255, 255, 255));
    g.FillRectangle(brush, MakeRect(0, 0, b.GetWidth(), b.GetHeight()));
    brush.SetColor(MakeColor(0, 255, 0, 255));
    g.FillEllipse(brush, MakeRect(FRAME_WIDTH, FRAME_WIDTH, b.GetWidth()-FRAME_WIDTH*2, b.GetHeight()-FRAME_WIDTH*2));
  finally
    g.Free();
  end;
end;

这将导致一个白色矩形。

目前,我通过变通办法解决了该任务。我用很粗的线画了一个圆:

procedure TTheIndicator._drawFrame(b: TGPBitmap);
const
  FRAME_WIDTH = 20;
var
  g: TGPGraphics;
  pen: TGPPen;
  cirDiam: Integer;
  cirThick: Integer;
begin
  g := TGPGraphics.Create(b);

  //Circle line thickness is no more than a half of the longest side
  cirThick := b.GetWidth();
  if cirThick < b.GetHeight() then cirThick := b.GetHeight();
  cirThick := cirThick div 2;

  //Cicrle diameter is a size of a shortest side
  cirDiam := b.GetWidth();
  if cirDiam > b.GetHeight() then cirDiam := b.GetHeight();

  pen := TGPPen.Create(MakeColor(255, 255, 255), cirThick);
  try
    g.DrawEllipse(
      pen,
      (b.GetWidth() - cirDiam - cirThick) div 2 + FRAME_WIDTH,
      (b.GetHeight() - cirDiam - cirThick) div 2 + FRAME_WIDTH,
      cirDiam + cirThick - FRAME_WIDTH * 2,
      cirDiam + cirThick - FRAME_WIDTH * 2
    );
  finally
    pen.Free();
    g.Free();
  end;
end;

0 个答案:

没有答案