比如,我要在TPanel中添加这个属性,我写是写了一段,我是参照Image的!
但是画不出来。
源码:
type
TPanelPipe = class(TPanel)
private
FPicture: TPicture;
function GetCanvas: TCanvas;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Canvas: TCanvas read GetCanvas;
published
end;
function TPanelPipe.GetCanvas: TCanvas;
var
Bitmap: TBitmap;
begin
if Picture.Graphic = nil then
begin
Bitmap := TBitmap.Create;
try
Bitmap.Width := Width;
Bitmap.Height := Height;
Picture.Graphic := Bitmap;
finally
Bitmap.Free;
end;
end;
if Picture.Graphic is TBitmap then
Result := TBitmap(Picture.Graphic).Canvas;
else
raise EInvalidOperation.Create(SImageCanvasNeedsBitmap);
end;constructor Tpanelpipe.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
FPicture := TPicture.Create;
//FPicture.OnChange := PictureChanged;//不注销出错
//FPicture.OnProgress := Progress;//同上
end;
但是画不出来。
源码:
type
TPanelPipe = class(TPanel)
private
FPicture: TPicture;
function GetCanvas: TCanvas;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Canvas: TCanvas read GetCanvas;
published
end;
function TPanelPipe.GetCanvas: TCanvas;
var
Bitmap: TBitmap;
begin
if Picture.Graphic = nil then
begin
Bitmap := TBitmap.Create;
try
Bitmap.Width := Width;
Bitmap.Height := Height;
Picture.Graphic := Bitmap;
finally
Bitmap.Free;
end;
end;
if Picture.Graphic is TBitmap then
Result := TBitmap(Picture.Graphic).Canvas;
else
raise EInvalidOperation.Create(SImageCanvasNeedsBitmap);
end;constructor Tpanelpipe.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
FPicture := TPicture.Create;
//FPicture.OnChange := PictureChanged;//不注销出错
//FPicture.OnProgress := Progress;//同上
end;
var
cas : TControlCanvas;
begin
cas := TControlCanvas.Create;
cas.Control := Panel1;//为了在Panel1上面作画
cas.MoveTo(10,10);
cas.LineTo(50,50);
cas.Free;
end;
var
FCanvas: TCanvas;FCanvas := TCanvas.Create;
FCanvas.Handle := GetDc(Panel1.Handle);这个FCanvas就是Panel1的canvas属性,你中意在上面画什么都行。
就是说刷新就没了!!怎么解决?!