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

时间:2017-06-20 17:19:26

标签: delphi draw delphi-10.1-berlin tcanvas

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

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;

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

enter image description here

这是对的吗?如果没有,我怎么能得到真正的点而不是破折号?

2 个答案:

答案 0 :(得分: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;

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

enter image description here

答案 1 :(得分:2)

DrawFocusRect它是一个Windows API调用,可以根据需要创建边框。

procedure AddDottedBorderToBitmap(aBM: Vcl.Graphics.TBitmap);
begin
  DrawFocusRect(aBM.canvas.Handle,Rect(0,0,aBM.Width,aBM.Height));
end;
相关问题