Z
zjwyyh
Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手: 我照书上对listbox进行扩展(能从资源管理器中将文件拖进去),我又想给他添个背景图片。但写好之后,为什么在设计期图片显示正常,而一运行程序,图片就消失了。我认为一定是listbox又重绘了自身。]
我因该咋办呢?
unit DropFileListBox;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls,ShellApi,Graphics;
type
//自定义onDropFiles事件的原型,该原型为事件的用户提供一个拖放文件的文件名列表
//TDropFileNotifyEvent=procedure(Sender:TObject;FileNames:TStringList) of object;
TMYNotifyEvent=procedure(Sender:TObject;FileNames:TStringList) of object;
//自定义组件类
TDropFileListBox = class(TListBox)
private
{ Private declarations }
Fenabled:Boolean;
Fpicture:Tpicture;
FDropFile:TMyNotifyEvent;//onDropFiles事件的指针
//DropFiles函数接受系统的WM_DROPFILES消息,并转换成OnDropFiles 事件
procedure DropFiles(var Msg:TMessage);message WM_DROPFILES;
procedure SetDropEnabled(bEnabled:Boolean);
procedure setpic(thepic:Tpicture);
procedure setbackgroudpic;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent);Override;
destructor Destroy;override;
published
{ Published declarations }
property OnDropFiles:TMyNotifyEvent read FDropFile write FDropFile;
property DropEnabled:Boolean read Fenabled write SetDropEnabled;
property backgroudpic:Tpicture read Fpicture write setpic;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('ZJWVCL', [TDropFileListBox]);
end;
constructor TDropFileListBox.Create(Aowner:TComponent);
begin
inherited Create(AOwner);
Fenabled:=true;
Fpicture:=Tpicture.Create;
end;
Destructor TDropFileListBox.Destroy;
begin
if Assigned(Fpicture) then
begin
Fpicture.Free;
Fpicture:=nil;
end;
inherited;
end;
procedure TDropFileListBox.setpic(thepic:Tpicture);
begin
Fpicture.Assign(thepic);
setbackgroudpic;
end;
procedure TDropFileListBox.setbackgroudpic;
begin
canvas.StretchDraw(ClientRect,Fpicture.Graphic);
end;
procedure TDropFileListBox.SetDropEnabled(bEnabled:Boolean);
begin
//设置DropEnabled属性时
Fenabled:=bEnabled;
DragAcceptFiles(Handle,bEnabled);
end;
procedure TDropFileListBox.DropFiles(var Msg:TMessage);
var
FileNames:TStringList;
FileName:array[0..MAX_PATH-1] of char;
i,nCount:integer;
begin
FileNames:=TStringList.Create;
//取得拖放的文件总数
nCount:=DragQueryFile(Msg.WParam,$FFFFFFFF,@FileName,MAX_PATH);
//将所有文件名添加到FileName列表中
For i:=0 to nCount-1 do
begin
DragQueryFile(Msg.WParam,i,FileName,MAX_PATH);
FileNames.Add(Filename);
end;//end for
//释放资源
DragFinish(Msg.WParam);
//调用用户事件代码
if Assigned(FDropFile) then
FDropFile(self,FileNames);
FileNames.Free;
end;
end.
我因该咋办呢?
unit DropFileListBox;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls,ShellApi,Graphics;
type
//自定义onDropFiles事件的原型,该原型为事件的用户提供一个拖放文件的文件名列表
//TDropFileNotifyEvent=procedure(Sender:TObject;FileNames:TStringList) of object;
TMYNotifyEvent=procedure(Sender:TObject;FileNames:TStringList) of object;
//自定义组件类
TDropFileListBox = class(TListBox)
private
{ Private declarations }
Fenabled:Boolean;
Fpicture:Tpicture;
FDropFile:TMyNotifyEvent;//onDropFiles事件的指针
//DropFiles函数接受系统的WM_DROPFILES消息,并转换成OnDropFiles 事件
procedure DropFiles(var Msg:TMessage);message WM_DROPFILES;
procedure SetDropEnabled(bEnabled:Boolean);
procedure setpic(thepic:Tpicture);
procedure setbackgroudpic;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent);Override;
destructor Destroy;override;
published
{ Published declarations }
property OnDropFiles:TMyNotifyEvent read FDropFile write FDropFile;
property DropEnabled:Boolean read Fenabled write SetDropEnabled;
property backgroudpic:Tpicture read Fpicture write setpic;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('ZJWVCL', [TDropFileListBox]);
end;
constructor TDropFileListBox.Create(Aowner:TComponent);
begin
inherited Create(AOwner);
Fenabled:=true;
Fpicture:=Tpicture.Create;
end;
Destructor TDropFileListBox.Destroy;
begin
if Assigned(Fpicture) then
begin
Fpicture.Free;
Fpicture:=nil;
end;
inherited;
end;
procedure TDropFileListBox.setpic(thepic:Tpicture);
begin
Fpicture.Assign(thepic);
setbackgroudpic;
end;
procedure TDropFileListBox.setbackgroudpic;
begin
canvas.StretchDraw(ClientRect,Fpicture.Graphic);
end;
procedure TDropFileListBox.SetDropEnabled(bEnabled:Boolean);
begin
//设置DropEnabled属性时
Fenabled:=bEnabled;
DragAcceptFiles(Handle,bEnabled);
end;
procedure TDropFileListBox.DropFiles(var Msg:TMessage);
var
FileNames:TStringList;
FileName:array[0..MAX_PATH-1] of char;
i,nCount:integer;
begin
FileNames:=TStringList.Create;
//取得拖放的文件总数
nCount:=DragQueryFile(Msg.WParam,$FFFFFFFF,@FileName,MAX_PATH);
//将所有文件名添加到FileName列表中
For i:=0 to nCount-1 do
begin
DragQueryFile(Msg.WParam,i,FileName,MAX_PATH);
FileNames.Add(Filename);
end;//end for
//释放资源
DragFinish(Msg.WParam);
//调用用户事件代码
if Assigned(FDropFile) then
FDropFile(self,FileNames);
FileNames.Free;
end;
end.