Delphi:绘制抗锯齿三角形

发布于 2024-12-22 17:43:04 字数 115 浏览 0 评论 0原文

如何在 ListBoxDrawItem 上绘制抗锯齿三角形?

ListBox.Canvas.Polygon

绘制有锯齿。

谢谢!!!

How can I draw anti-aliased triangle on ListBoxDrawItem?

ListBox.Canvas.Polygon

draws with jags.

Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-12-29 17:43:04

使用 GDI 实现抗锯齿绘图的最简单方法是绘制到更大的表面,然后在启用半色调拉伸模式的情况下缩小到原始尺寸。

下面的代码示例使用了一个比列表框的客户区大 16 倍的位图(这是一个相当大的位图,绘制起来会花费很多时间,但应该更容易看到效果)。

procedure TForm1.Button1Click(Sender: TObject);
const
  ZOOM = 16;
var
  Bmp: TBitmap;
  StretchMode: Integer;
begin
  // for comparison
  ListBox2.Canvas.Polygon([Point(20, 10), Point(10, 50), Point(80, 30)]);


  Bmp := TBitmap.Create;
  // create a large bitmap and set coordinate extents accordingly
  Bmp.SetSize(ListBox1.ClientWidth * ZOOM, ListBox1.ClientHeight * ZOOM);
  SetMapMode(Bmp.Canvas.Handle, MM_ISOTROPIC);
  SetWindowExtEx(Bmp.Canvas.Handle, 100, 100, nil);
  SetViewportExtEx(Bmp.Canvas.Handle, 100 * ZOOM, 100 * ZOOM, nil);
  // without halftone we won't gain anything
  SetStretchBltMode(Bmp.Canvas.Handle, HALFTONE);
  // transfer what's on the list box to bitmap canvas
  BitBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
          ListBox1.Canvas.Handle, 0, 0, SRCCOPY);

  Bmp.Canvas.Polygon([Point(20, 10), Point(10, 50), Point(80, 30)]);

  // transfer bitmap contents
  StretchMode := SetStretchBltMode(ListBox1.Canvas.Handle, HALFTONE);
  StretchBlt(ListBox1.Canvas.Handle, 0, 0,
      ListBox1.ClientWidth * ZOOM, ListBox1.ClientHeight * ZOOM,
      Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, SRCCOPY);
  SetStretchBltMode(ListBox1.Canvas.Handle, StretchMode);

  Bmp.Free;
end;

在下图中,左侧是 ListBox1 - 使用抗锯齿绘制的列表框。请注意,文本也获得了一些效果:

在此处输入图像描述

我当然建议您考虑 David 的建议之一。这段代码相当实验性:)。

The simplest approach to achieve anti-aliased drawing with GDI is to draw to a larger surface, then scale back to original dimensions having halftone enabled stretching mode.

Below code example uses a 16 times larger bitmap then the list box's client area (this is a considerably larger bitmap and it will take a good time to do the drawings, but the effect should be seen easier).

procedure TForm1.Button1Click(Sender: TObject);
const
  ZOOM = 16;
var
  Bmp: TBitmap;
  StretchMode: Integer;
begin
  // for comparison
  ListBox2.Canvas.Polygon([Point(20, 10), Point(10, 50), Point(80, 30)]);


  Bmp := TBitmap.Create;
  // create a large bitmap and set coordinate extents accordingly
  Bmp.SetSize(ListBox1.ClientWidth * ZOOM, ListBox1.ClientHeight * ZOOM);
  SetMapMode(Bmp.Canvas.Handle, MM_ISOTROPIC);
  SetWindowExtEx(Bmp.Canvas.Handle, 100, 100, nil);
  SetViewportExtEx(Bmp.Canvas.Handle, 100 * ZOOM, 100 * ZOOM, nil);
  // without halftone we won't gain anything
  SetStretchBltMode(Bmp.Canvas.Handle, HALFTONE);
  // transfer what's on the list box to bitmap canvas
  BitBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
          ListBox1.Canvas.Handle, 0, 0, SRCCOPY);

  Bmp.Canvas.Polygon([Point(20, 10), Point(10, 50), Point(80, 30)]);

  // transfer bitmap contents
  StretchMode := SetStretchBltMode(ListBox1.Canvas.Handle, HALFTONE);
  StretchBlt(ListBox1.Canvas.Handle, 0, 0,
      ListBox1.ClientWidth * ZOOM, ListBox1.ClientHeight * ZOOM,
      Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, SRCCOPY);
  SetStretchBltMode(ListBox1.Canvas.Handle, StretchMode);

  Bmp.Free;
end;

In the below picture, in the left is the ListBox1 - the one drawn with anti-aliasing. Please notice that the text also have gained some of the effect:

enter image description here

I would of course advise you to take one of David's suggestions into account. This code was rather experimental :).

怀里藏娇 2024-12-29 17:43:04

没有内置任何东西可以进行抗锯齿。您可以使用 GDI+,但我会推荐 graphics32 ,它会愉快地绘制 graphics32 。 org/documentation/Docs/Units/GR32_Polygons/Routines/Polygon.htm" rel="nofollow">抗锯齿多边形。

There is nothing built in that will do anti-aliasing. You could use GDI+ but I would recommend graphics32 which will happily draw anti-aliased polygons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文