总结:
type
MyObject = object
end;
MyRecord = record
end;
MyClass = class
end;
procedure ProcA(aMyObject: MyObject);
procedure ProcB(var aMyObject: MyObject);
procedure ProcC(aMyRecord: MyRecord);
procedure ProcD(var aMyRecord: MyRecord);
procedure ProcE(aMyClass: MyOClass);
procedure ProcF(var aMyClass: MyClass);
MyObject
andMyRecord
是值类型,而是MyClass
引用类型。- 赋值类型变量的赋值会复制变量;引用类型变量的赋值将复制引用。
ProcA
和中的参数ProcC
是原始参数的副本。ProcB
和中的论点ProcD
是原始论点。- in 的参数
ProcE
是原始引用的副本。 - 中的参数
ProcF
是原始参考。 - 关于如何包装在单元 agg_2D.pas 中声明的 Agg2D 对象进行绘制,请参阅下面大卫的回答。
============================================
我正在学习使用AggPas这是一个纯帕斯卡矢量图形绘图API。具体来说,使用包含 Agg2D 对象的单元 agg_2D.pas 代替包含 TAgg2D 类的单元 Agg2D.pas。选择单位 agg_2D.pas 而不是单位 Agg2D.pas 的原因是为了跨平台能力。
但是,我无法正确传递带有 var 前缀的 Agg2D 对象类型的参数。如以下代码所示,我想将 TForm1 创建的 Agg2D 对象传递给另一个实际负责绘制形状的类。但是,它不起作用。你能帮忙评论一下可能的原因吗?看来我一定错过了有关对象类型的重要概念。任何建议表示赞赏!您可以新建一个 VCL 应用程序,附加 FormCreate 处理程序,并逐行注释绘图代码以查看效果。
unit Unit1;
interface
uses
agg_2D,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TRenderEngine_BMP = class;
TRenderEngine_Agg = class;
TForm1 = class;
TRenderEngine_BMP = class
private
fBMP: TBitmap;
public
constructor Create(var aBMP: TBitmap);
procedure DrawEllipse;
end;
TRenderEngine_Agg = class
private
fVG: Agg2D;
public
constructor Create(var aVG: Agg2D);
procedure DrawEllipse;
end;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
fBMP: TBitmap;
fVG: Agg2D;
fEngine_BMP: TRenderEngine_BMP;
fEngine_Agg: TRenderEngine_Agg;
procedure AttachBMP(var aVG: Agg2D; var aBMP: TBitmap);
procedure OnSceneResize(Sender: TObject);
procedure OnScenePaint(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Math;
{ TRenderEngine_BMP }
constructor TRenderEngine_BMP.Create(var aBMP: TBitmap);
begin
Self.fBMP := aBMP;
end;
procedure TRenderEngine_BMP.DrawEllipse;
begin
Self.fBMP.Canvas.ellipse(20, 20, 80, 80);
end;
{ TRenderEngine_Agg }
constructor TRenderEngine_Agg.Create(var aVG: Agg2D);
begin
Self.fVG := aVG;
end;
procedure TRenderEngine_Agg.DrawEllipse;
begin
Self.fVG.ellipse(50, 50, 30, 30);
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Self.OnResize := {$IFDEF FPC} @ {$ENDIF} OnSceneResize;
Self.OnPaint := {$IFDEF FPC} @ {$ENDIF} OnScenePaint;
fBMP := TBitmap.Create;
fBMP.PixelFormat := pf32bit;
fBMP.Canvas.Brush.Style := bsSolid;
fBMP.Canvas.Brush.Color := clBlue;
fBMP.Width := ClientWidth;
fBMP.Height := ClientHeight;
fVG.Construct;
Self.AttachBMP(fVG, fBMP);
fEngine_BMP := TRenderEngine_BMP.Create(fBMP);
fEngine_Agg := TRenderEngine_Agg.Create(fVG);
end;
procedure TForm1.AttachBMP(var aVG: Agg2D; var aBMP: TBitmap);
var
tmpBuffer: pointer;
tmpStride: integer;
begin
tmpStride := integer(aBMP.ScanLine[1]) - integer(aBMP.ScanLine[0]);
if tmpStride < 0 then
tmpBuffer := aBMP.ScanLine[aBMP.Height - 1]
else
tmpBuffer := aBMP.ScanLine[0];
aVG.attach(tmpBuffer, aBMP.Width, aBMP.Height, tmpStride);
end;
procedure TForm1.OnScenePaint(Sender: TObject);
begin
Self.fBMP.Canvas.FillRect(Self.ClientRect);
// Self.fBMP.Canvas.ellipse(20, 20, 80, 80); // Work
// Self.fVG.ellipse(50, 50, 30, 30); // Work
// Self.fEngine_BMP.DrawEllipse; // Work
Self.fEngine_Agg.DrawEllipse; // Do not work
Self.Canvas.Draw(0, 0, fBMP);
end;
procedure TForm1.OnSceneResize(Sender: TObject);
begin
fBMP.Width := IfThen(ClientWidth > 0, ClientWidth, 2);
fBMP.Height := IfThen(ClientHeight > 0, ClientHeight, 2);
Self.AttachBMP(fVG, fBMP);
end;
end.
如果我删除所有出现的过程参数的 var 前缀,第二个画圆代码也会停止工作,我不太明白。为方便起见,单位如下图所示:
unit Unit1;
interface
uses
agg_2D,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TRenderEngine_BMP = class;
TRenderEngine_Agg = class;
TForm1 = class;
TRenderEngine_BMP = class
private
fBMP: TBitmap;
public
constructor Create(aBMP: TBitmap);
procedure DrawEllipse;
end;
TRenderEngine_Agg = class
private
fVG: Agg2D;
public
constructor Create(aVG: Agg2D);
procedure DrawEllipse;
end;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
fBMP: TBitmap;
fVG: Agg2D;
fEngine_BMP: TRenderEngine_BMP;
fEngine_Agg: TRenderEngine_Agg;
procedure AttachBMP(aVG: Agg2D; aBMP: TBitmap);
procedure OnSceneResize(Sender: TObject);
procedure OnScenePaint(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Math;
{ TRenderEngine_BMP }
constructor TRenderEngine_BMP.Create(aBMP: TBitmap);
begin
Self.fBMP := aBMP;
end;
procedure TRenderEngine_BMP.DrawEllipse;
begin
Self.fBMP.Canvas.ellipse(20, 20, 80, 80);
end;
{ TRenderEngine_Agg }
constructor TRenderEngine_Agg.Create(aVG: Agg2D);
begin
Self.fVG := aVG;
end;
procedure TRenderEngine_Agg.DrawEllipse;
begin
Self.fVG.ellipse(50, 50, 30, 30);
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Self.OnResize := {$IFDEF FPC} @ {$ENDIF} OnSceneResize;
Self.OnPaint := {$IFDEF FPC} @ {$ENDIF} OnScenePaint;
fBMP := TBitmap.Create;
fBMP.PixelFormat := pf32bit;
fBMP.Canvas.Brush.Style := bsSolid;
fBMP.Canvas.Brush.Color := clBlue;
fBMP.Width := ClientWidth;
fBMP.Height := ClientHeight;
fVG.Construct;
Self.AttachBMP(fVG, fBMP);
fEngine_BMP := TRenderEngine_BMP.Create(fBMP);
fEngine_Agg := TRenderEngine_Agg.Create(fVG);
end;
procedure TForm1.AttachBMP(aVG: Agg2D; aBMP: TBitmap);
var
tmpBuffer: pointer;
tmpStride: integer;
begin
tmpStride := integer(aBMP.ScanLine[1]) - integer(aBMP.ScanLine[0]);
if tmpStride < 0 then
tmpBuffer := aBMP.ScanLine[aBMP.Height - 1]
else
tmpBuffer := aBMP.ScanLine[0];
aVG.attach(tmpBuffer, aBMP.Width, aBMP.Height, tmpStride);
end;
procedure TForm1.OnScenePaint(Sender: TObject);
begin
Self.fBMP.Canvas.FillRect(Self.ClientRect);
// Self.fBMP.Canvas.ellipse(20, 20, 80, 80); // Work
// Self.fVG.ellipse(50, 50, 30, 30); // Do not Work
// Self.fEngine_BMP.DrawEllipse; // Work
Self.fEngine_Agg.DrawEllipse; // Do not work
Self.Canvas.Draw(0, 0, fBMP);
end;
procedure TForm1.OnSceneResize(Sender: TObject);
begin
fBMP.Width := IfThen(ClientWidth > 0, ClientWidth, 2);
fBMP.Height := IfThen(ClientHeight > 0, ClientHeight, 2);
Self.AttachBMP(fVG, fBMP);
end;
end.