怎样在panle上加载图片(100分)

  • 主题发起人 主题发起人 hhjjhhjj
  • 开始时间 开始时间
H

hhjjhhjj

Unregistered / Unconfirmed
GUEST, unregistred user!
就像speedbutton的GlyPH属性一样.
不要在Panle放Image控件的.
 
自己写一个控件,做个属性,然后把图画出来
 
自己重新写个控件吧。
 
用它的canvas对象
 
你想干什么
自己画吧
 
从TCustomControl类继承一个类出来,比如叫做TCustomPanel,在该类中公开(Published)受保护(Protected)的Canvas属性既可!
 
从TCustomControl类继承一个类
property Picture: TPicture read m_Picture write SetPicture;

SetPicture里
m_Picture.Assign(Value);

Paint里
Canvas.StretchDraw(ClientRect, m_Picture.Graphic);

应该就可以了
 
具体代码得你自己完善了,以上只是一种方法,自己试一下吧
 
var
MyCanvas:TCanvas;
bit:TBitmap;
begin
try
MyCanvas:=TCanvas.Create;
MyCanvas.Handle:=Getdc(Panel1.Handle);
bit:=TBitmap.Create;
bit.LoadFromFile('c:/qq.bmp');
MyCanvas.StretchDraw(rect(0,0,Panel1.Width,Panel1.Height),bit);
finally
MyCanvas.Free;
end;
 
用Johnny_du的方法
TDrawPanel = class(TPanel)
private
fOnPaint: TKeyEvent;
protected
procedure Paint; override;
public
property Canvas;
published
property OnPaint: TKeyEvent write fOnPaint read fOnPaint;
end;

procedure TDrawPanel.Paint;
begin
inherited;
if Assigned(fOnPaint) then
fOnPaint(Self);
end;
 
var
c:TCanvas;
bit:TBitmap;
begin
try
c:=TControlCanvas.Create;
TControlCanvas(c).Control:=Panel1;
bit:=TBitmap.Create;
bit.LoadFromFile('c:/qq.bmp');
c.StretchDraw(rect(0,0,Panel1.Width,Panel1.Height),bit);
finally
c.Free;
bit.Free;
end
 
呵呵,看错题目了,应该用xf-wangyi的是对的
TDrawPanel = class(TPanel)
private
fPicture: TPicture;
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Picture: TPicture write fPictureread fPicture;
end;

procedure TDrawPanel.Paint;
begin
//inherited;//根据需要看用不用
if fPicture.Graphic <> null then
begin
Canvas.StretchDraw(ClientRect, fPicture.Graphic);
end;
end;

constructor TDrawPanel.Create(AOwner: TComponent);
begin
inherited;
fPiction := TPicture.Create;
end;

destructor TDrawPanel.Destroy;
begin
fPiction .Free;
end;
 
//由xf-wangyi大侠观点修改而来:
type
TMyPanel = class(TPanel)
private
m_Picture:TPicture;
procedure SetPicture(pic:TPicture);
public
procedure Paint; override;
property
Picture: TPicture read m_Picture write SetPicture;
end;

{ TMyPanel }

procedure TMyPanel.Paint;
begin
inherited;
if m_Picture<>nil then
Canvas.StretchDraw(ClientRect, m_Picture.Graphic);
end;

procedure TMyPanel.SetPicture(pic: TPicture);
begin
if m_Picture=nil then
begin
m_Picture:=TPicture.Create;
m_Picture.Assign(pic);
end else
m_Picture := pic;
end;

//使用方法:
procedure TForm1.btn1Click(Sender: TObject);
var OpenDialog1:TOpenDialog;
pic:TPicture;
begin
OpenDialog1:=TOpenDialog.Create(nil);
if OpenDialog1.Execute then
begin
pic:=TPicture.Create;
try
pic.LoadFromFile(OpenDialog1.FileName);
except
end;
MyPanel1.SetPicture(pic);
end;
end;
 
正解来了[:D]如果有错误自己改了

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.
 
to:coscka
你这样用控件呀,PFPF[:D]
把属性published就可以在设计期做了[:)]
俺也不是大侠,不过爱吃大虾[:D],可惜可惜,北京的没有家里好吃
 
多人接受答案了。
 
后退
顶部