在TBitmap周围绘制点的边界线?

use*_*348 3 delphi draw tcanvas delphi-10.1-berlin

我编写了一个例程,它应该为位图添加一个虚线边框:

procedure AddDottedBorderToBitmap(aBM: Vcl.Graphics.TBitmap);
var
  c: TCanvas;
begin
  c := aBM.Canvas;
  c.Pen.Color := clBlack;
  c.Pen.Mode  := pmXor;
  c.Pen.Style := psDot;

  c.MoveTo(0, 0);
  c.LineTo(0, aBM.Height - 1);
  c.LineTo(aBM.Width - 1, aBM.Height - 1);
  c.LineTo(aBM.Width - 1, 0);
  c.LineTo(0, 0);
end;
Run Code Online (Sandbox Code Playgroud)

但是当放大结果时,生成的边界而不是点似乎是由小破折号组成的:

在此输入图像描述

它是否正确?如果没有,我怎么能得到真正的点而不是破折号?

Tom*_*erg 6

使用起来似乎很简单DrawFocusRect,但如果您需要绘制除矩形以外的其他内容,您可能需要提前阅读.

笔式psDot并不意味着每个第二个像素都被着色而另一个像素被清除.如果你考虑一下,分辨率越高,看到点状与灰色固体f.ex的差异就越难.还有另一种笔式psAlternate交替像素.文档说:

psAlternate

笔设置每隔一个像素.(此样式仅适用于化妆笔.)此样式仅对使用ExtCreatePen API函数创建的笔有效.(请参阅MS Windows SDK文档.)这适用于VCL和VCL.NET.

要定义笔并使用它,我们执行如下操作

var
  c: TCanvas;
  oldpenh, newpenh: HPEN; // pen handles
  lbrush: TLogBrush;      // logical brush

...

  c := pbx.Canvas; // pbx is a TPintBox, but can be anything with a canvas

  lbrush.lbStyle := BS_SOLID;
  lbrush.lbColor := clBlack;
  lbrush.lbHatch := 0;

  // create the pen
  newpenh := ExtCreatePen(PS_COSMETIC or PS_ALTERNATE, 1, lbrush, 0, nil);
  try
    // select it
    oldpenh := SelectObject(c.Handle, newpenh);

    // use the pen
    c.MoveTo(0, 0);
    c.LineTo(0, pbx.Height - 1);
    c.LineTo(pbx.Width - 1, pbx.Height - 1);
    c.LineTo(pbx.Width - 1, 0);
    c.LineTo(0, 0);

    c.Ellipse(3, 3, pbx.width-3, pbx.Height-3);

    // revert to the old pen
    SelectObject(c.Handle, oldpenh);
 finally
    // delete the pen
    DeleteObject(newpenh);
 end;
Run Code Online (Sandbox Code Playgroud)

最后它看起来像(放大镜在x 10)

在此输入图像描述

  • @ user1580348我不会称之为暗示,我会将其作为接受的答案:-) (3认同)