正解来了[
]如果有错误自己改了
unit ImagePanel;
interface
uses Windows, Extctrls, Graphics, Classes, Messages, Controls, SysUtils, Forms;
type
TCustomImagePanel = class(TCustomPanel)
private
m_Picture: TPicture;
procedure SetPicture(const Value: TPicture);
protected
procedure Paint(); override;
procedure ClearPanel(); virtual;
procedure PictureChanged(Sender: TObject); virtual;
procedure Resize(); override;
property Picture: TPicture read m_Picture write SetPicture;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
end;
TImagePanel = class(TCustomImagePanel)
published
property BiDiMode;
property BorderWidth;
property Picture;
property Anchors;
property Alignment;
property Align;
property Font;
property TabStop;
property TabOrder;
property Caption;
property Color;
property Visible;
property PopupMenu;
property OnCanResize;
property OnClick;
property OnConstrainedResize;
property OnDockDrop;
property OnDockOver;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGetSiteInfo;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property OnUnDock;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('ImagePanel', [TImagePanel]);
end;
{ TImagePanel }
procedure TCustomImagePanel.ClearPanel;
begin
Canvas.Brush.Color := Color;
if ParentWindow <> 0 then
Canvas.FillRect(ClientRect);
end;
constructor TCustomImagePanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
m_Picture := TPicture.Create();
m_Picture.OnChange := PictureChanged;
Repaint();
end;
destructor TCustomImagePanel.Destroy;
begin
if m_Picture <> nil then
begin
m_Picture.Free();
m_Picture := nil;
end;
inherited;
end;
procedure TCustomImagePanel.Paint;
var
Buf: TBitmap;
begin
Buf := TBitmap.Create();
Buf.Height := Height;
Buf.Width := Width;
if Assigned(m_Picture.Graphic) then
begin
Buf.Canvas.StretchDraw(ClientRect, m_Picture.Graphic)
end;
Buf.Canvas.Brush.Style := bsClear;
BitBlt(Canvas.Handle, 0, 0, Width, Height, Buf.Canvas.Handle, 0, 0, SRCCOPY);
Buf.Free();
end;
procedure TCustomImagePanel.PictureChanged(Sender: TObject);
begin
ClearPanel();
RePaint();
end;
procedure TCustomImagePanel.Resize;
begin
inherited;
Repaint();
end;
procedure TCustomImagePanel.SetPicture(const Value: TPicture);
begin
m_Picture.Assign(Value);
ClearPanel();
Repaint();
end;
end.