多个TShape 重叠载一起,如何得到后层TShape某点颜色(100分)

  • 主题发起人 主题发起人 LiGen
  • 开始时间 开始时间
L

LiGen

Unregistered / Unconfirmed
GUEST, unregistred user!
多个TShape 重叠载一起,各个TShape 都画有相应多边形,想知道某一层 TShape 的
某点颜色,也就是透过上层TShape 查看下层 TShape 的颜色,使用
Color:= getpixel(Shape1.canvas.handle,x,y);
只得到屏幕 点(x,y)的颜色,而不是 Shape1 上的 点(x,y)的颜色。

不知道有什么办法,请各位支招,谢谢!
 
这个有点难办,因为TShape是由TGraphicControl继承过来的
 
你可以考虑自定义组件TShape,
重载 Paint ,然后在自己把 Canvas 复制到bitmap中备份起来。
然后输出一个方法从 bitmap中取点,就可以了。
或者 在输出的方法中重新画一次到一个临时的 bitmap 中,再取点,这样更节约内存。

unit shape1;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls, Graphics;

type
tshape1 = class(tshape)
private
FBmp: TBitmap;
{ Private declarations }
protected
{ Protected declarations }
procedure Paint; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;

function getpixcolor(x, y: integer): tcolor;
published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [tshape1]);
end;

{ tshape1 }

constructor tshape1.Create(AOwner: TComponent);
begin
FBmp := TBitmap.Create;

inherited;
end;

destructor tshape1.Destroy;
begin
FBmp.Free;

inherited;
end;

function tshape1.getpixcolor(x, y: integer): tcolor;
begin
result := fbmp.Canvas.Pixels[x, y];
end;

procedure tshape1.Paint;
begin
inherited;

FBmp.Width := Width;
FBmp.Height := Height;

FBmp.Canvas.CopyRect(ClientRect, Canvas, ClientRect);
end;

end.
 
to wnjer:
谢谢!我试了一下
result := fbmp.Canvas.Pixels[x, y];
得到的也是屏幕 点(x,y)的颜色,而不是 fbmp.Pixels[x, y];
 
不会吧, 我试过行的呀.
那你就把 TShape的 Paint里面的代码 复制一份, 在取点颜色时在自己的 TBitmap中画一次,再在Bitmap中取点, 这样总该可以了吧, 但如果你要频繁取点, 这样是不高效的.
 
后退
顶部