Discuz! BBS

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 546|回复: 0

Delphi shape 画一个任意多边形

[复制链接]

254

主题

363

帖子

2431

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2431
发表于 2024-12-13 04:31:43 | 显示全部楼层 |阅读模式
<草稿>使用TGPGraphics, 在窗体上绘制5边形:
  1. uses
  2.   Gdiplus;

  3. procedure TForm1.FormPaint(Sender: TObject);
  4. var
  5.   Graphics: TGPGraphics;
  6.   Pen: TGPPen;
  7.   Path: TGPGraphicsPath;
  8.   Points: array[0..4] of TGPPoint;
  9. begin
  10.   Graphics := TGPGraphics.Create(Canvas.Handle);
  11.   Pen := TGPPen.Create(MakeColor(255, 0, 0, 255), 2); // 蓝色边框的画笔
  12.   Path := TGPGraphicsPath.Create;

  13.   // 设置五边形的顶点坐标
  14.   Points[0].X := ClientWidth / 2;
  15.   Points[0].Y := 10;
  16.   Points[1].X := ClientWidth / 4;
  17.   Points[1].Y := ClientHeight - 10;
  18.   Points[2].X := ClientWidth / 2;
  19.   Points[2].Y := ClientHeight - 10;
  20.   Points[3].X := ClientWidth * 3 / 4;
  21.   Points[3].Y := ClientHeight - 10;
  22.   Points[4].X := ClientWidth / 2;
  23.   Points[4].Y := 10;

  24.   // 添加多边形路径
  25.   Path.AddPolygon(PGPPoint(@Points), Length(Points));

  26.   // 绘制多边形
  27.   Graphics.DrawPath(Pen, Path);

  28.   Path.Free;
  29.   Pen.Free;
  30.   Graphics.Free;
  31. end;
复制代码

在这个例子中,我们首先创建了一个TGPGraphics对象来处理绘图,然后创建了一个TGPPen对象来指定线条的颜色和宽度。接着,我们创建了一个TGPGraphicsPath对象,并使用AddPolygon方法添加了一个五边形。最后,我们使用DrawPath方法来绘制多边形。

你可以将这段代码放入一个窗体的OnPaint事件中,以确保每次窗体重绘时都会显示多边形。记得在实际使用时,需要将GDI+初始化,并在不使用时释放资源。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-4-16 05:01 , Processed in 0.013733 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表