Delphi 在图像上绘制多边形
我已经搜索了很长一段时间,但没有找到答案 我想在图像上画一个多边形,但我想通过创建点来实现这一点; 使用Delphi 在图像上绘制多边形,delphi,delphi-xe2,Delphi,Delphi Xe2,我已经搜索了很长一段时间,但没有找到答案 我想在图像上画一个多边形,但我想通过创建点来实现这一点; 使用MouseCursor创建此特定点,并使用按钮沿这些点绘制一条线 我发现: var Poly: array of TPoint; begin // Allocate dynamic array of TPoint SetLength(Poly, 6); // Set array elements Poly[0] := Point(10, 10); Poly[1
MouseCursor
创建此特定点,并使用按钮沿这些点绘制一条线
我发现:
var
Poly: array of TPoint;
begin
// Allocate dynamic array of TPoint
SetLength(Poly, 6);
// Set array elements
Poly[0] := Point(10, 10);
Poly[1] := Point(30, 5);
Poly[2] := Point(100, 20);
Poly[3] := Point(120, 100);
Poly[4] := Point(50, 120);
Poly[5] := Point(10, 60);
// Pass to drawing routine
Canvas.Polygon(Poly);
// Redim if needed
SetLength(Poly, 7);
Poly[6] := Point(1, 5);
// Pass to drawing routine
Canvas.Polygon(Poly);
end;
这就是我想要的,但不同的是,
点[1]
,点[2]
,等等是由用户使用MouseEvent
创建一个由表单管理的点数组。在窗体类中声明动态数组字段:
private
FPoly: array of TPoint;
在OnClick
事件中,延长阵列并向其添加新坐标:
procedure TFruitForm.ImageClick(Sender: TObject);
var
p: TPoint;
begin
p := ...;
SetLength(FPoly, Length(FPoly) + 1);
FPoly[High(FPoly)] := p;
end;
要分配p
,请参阅
代替数组,你也可以考虑使用一个通用的列表:<代码> tList .< /P> < P>你可以在图像上叠加一个PruttBox并使用这样的代码
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TPointArray=array of TPoint;
TForm3 = class(TForm)
Image1: TImage;
PaintBox1: TPaintBox;
Button1: TButton;
procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1Paint(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
FPointArray:TPointArray;
public
{ Public-Deklarationen }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
PaintBox1.Visible := false;
Image1.Canvas.Polygon(FPointArray);
end;
procedure TForm3.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
SetLength(FPointArray,Length(FPointArray)+1);
FPointArray[High(FPointArray)].X := X;
FPointArray[High(FPointArray)].Y := Y;
Paintbox1.Invalidate;
end;
procedure TForm3.PaintBox1Paint(Sender: TObject);
var
i:Integer;
begin
PaintBox1.Canvas.Brush.Style := bsClear; //as suggested by TLama
PaintBox1.Canvas.Polygon(FPointArray);
for I := 0 to High(FPointArray) do
begin
PaintBox1.Canvas.TextOut(FPointArray[i].X-5,FPointArray[i].y-5,IntToStr(i));
end;
end;
end.
问题是什么?处理OnClick事件并将点添加到点列表中。问题是,如何使用OnClick事件在数组中存储点?感谢您的回答,但我不需要在图像上也绘制点吗?I分配p:=屏幕到客户端(p);是的,你需要描绘要点,但这不是你的问题。您问,“如何使用onclick事件在数组中存储点?”您显然不想在onclick事件中绘制多边形,因为在了解所有点之前,您无法绘制整个多边形。是的,存储它们,然后在存储点时的mouseclick事件中绘制按钮等,如果我能看到点在哪里不是更好吗?是的,我想会更好。请随意这样写你的程序。+1在这里平衡我对另一篇文章的支持,因为这篇文章回答了问题,而另一篇文章提供了更好的方法。你好,bummi,我真的很喜欢你的想法。我必须声明FPointArray:TPointArray;as
FPointArray:Tpoint的数组
。项目运行良好,但如何使多边形不绘制其区域?只是沿着点画线?在我看来,这是更好的方法。尽管我会在TPaintBox.OnPaint
事件中自己渲染TImage
图片,并删除TImage
组件。如果要绘制空多边形,请将PaintBox1.Canvas.Brush.Style:=bsClear这里调用多边形
函数之前的code>行。[+1]@TLama我也更喜欢只使用TPaintbox。但是,如果所有步骤都完成了,水果可能会想要保持结果。