关于CreatePolygonRgn函数(200分)

  • 主题发起人 主题发起人 cAkk
  • 开始时间 开始时间
(Big_Z 吃饭去了!)
 
I Can't Answer This Question, But ...

好象是与参数类型的不同定义有关,
什么 Dynamic arrays,什么 Open array 等等等等???
要请高人解答!!!
如果你弄同了,麻烦告诉我,那些 Help 实在没有耐心看下去了!(又臭又长!)

=====================================================================

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
rgn:hrgn;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;
TP = array of TPoint;

var
P: PPoints;
pary: TP;

function CreatePolyRgn(var Points: TP): HRgn;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;
begin
Result := CreatePolygonRgn(PPoints(Points)^, High(Points) + 1, WINDING);
end;

begin
canvas.brush.color:=clred;
setlength(pary,4);
pary[0]:=point(0,0);
pary[1]:=point(180,0);
pary[2]:=point(180,180);
pary[3]:=point(0,180);
try
rgn:=CreatePolyRgn(Pary);
if rgn<>0 then
fillrgn(canvas.handle,rgn,canvas.brush.handle)
else
showmessage('invalid region');
finally
deleteobject(rgn);
end;
end;



procedure TForm1.Button2Click(Sender: TObject);
var pary:array of tpoint;
i:integer;
rgn:hrgn;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;

var
P: PPoints;

// Copy From RXHints.pas
function CreatePolyRgn(const Points: array of TPoint): HRgn;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;
begin
Result := CreatePolygonRgn(PPoints(@Points)^, High(Points) + 1, WINDING);
end;

begin
canvas.brush.color:=clBlue;
setlength(pary,4);
pary[0]:=point(0,0);
pary[1]:=point(180,0);
pary[2]:=point(180,180);
pary[3]:=point(0,180);
try
rgn:=CreatePolyRgn(Pary);
if rgn<>0 then
fillrgn(canvas.handle,rgn,canvas.brush.handle)
else
showmessage('invalid region');
finally
deleteobject(rgn);
end;
end;

end.
 
cAkk==十万个为什么
hahaha:-)
 
可是我还是没弄明白我最先的代码为什么不能用?
 
也许是游戏的规则吧,

参数的定义方式影响着其使用时的方式,
你的代码和 RXHints 中的并不一样,
function CreatePolyRgn(const Points: array of TPoint): HRgn
的定义过程中,参数 Points 的性质已经发生变化,类似于静态数组,
可以试一下, 改成下面这样子编译通不过的:

function CreatePolyRgn(Points: array of TPoint): HRgn;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;
begin
SetLength(Points, 4);
Result := CreatePolygonRgn(PPoints(@Points)^, High(Points) + 1, WINDING);
end;

而如下的修改却又可行:
Type
TP = array of TPoint;

function CreatePolyRgn(Points: TP): HRgn;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;
begin
SetLength(Points, 3);
Result := CreatePolygonRgn(PPoints(Points)^, High(Points) + 1, WINDING);
end;

静态数组和动态数组应该有所不同,
至少,动态数组和 String 类型一样,
需要有一个地方存放长度,
而 CreatePolygonRgn 需要指定的是第一个数据的位置,
静态数组的定义也就指向第一个数据,
动态数据的定义却不是,
传入第一个数据的位置是可以的:

procedure TForm1.Button3Click(Sender: TObject);
var pary:array of tpoint;
i:integer;
rgn:hrgn;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;
begin
canvas.brush.color:=clred;
setlength(pary,4);
pary[0]:=point(0,0);
pary[1]:=point(180,0);
pary[2]:=point(180,180);
pary[3]:=point(0,180);
try
//rgn:=CreatePolygonRgn(Pary, 3, WINDING);
rgn:=CreatePolygonRgn(PPoints(@Pary[0])^, High(Pary) + 1, ALTERNATE );
if rgn<>0 then
fillrgn(canvas.handle,rgn,canvas.brush.handle)
else
showmessage('invalid region');
finally
deleteobject(rgn);
end;
end;

实在没法弄懂的情况下先记着,
再总结规律,……
我也不是很清楚,但我不急!!!
 
那就这样吧.

不管怎么说,PPoints(@Pary)^和PPoints(Pary)^居然都能用,delphi实在奇怪!

Big_Z到外面来拿分.
 
btw: CreatePolygonRgn(PPoints(@Pary[0])^, High(Pary) + 1, ALTERNATE );
不管是静态的还是动态的,都可以。
 
我下面的代码是在窗体上画一个正方形。不知能否满足你的要求。
unit Unit1incur;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
FRgn :HRGN;
pt :array [0..3] of TPoint;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
pt[0] :=Point(0,0);
pt[1] :=Point(180,0);
pt[2] :=Point(180,180);
pt[3] :=Point(0,180);
form1.Canvas.Polygon(pt);
FRgn :=CreatePolygonRgn(pt,4,Winding);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
DeleteObject(FRgn);

end;

end.
 
后退
顶部