Delphi编程实现清空回收站

页面导航:首页 > 软件编程 > Delphi > Delphi编程实现清空回收站

Delphi编程实现清空回收站

来源: 作者: 时间:2016-02-05 11:47 【

本程序演示如何通过程序来清空回收站。 向窗体上添加一个TButton组件,它用于激活清空回收站的操作。设计完成的主界面如图1所示。 图1 主界面
本程序演示如何通过程序来清空回收站。

  向窗体上添加一个TButton组件,它用于激活清空回收站的操作。设计完成的主界面如图1所示。

\<喎"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3Bhbj48YnIvPg0KICAgICAgICAgICAgzbwxINb3vefD5jwvZGl2Pg0KICAgICAgICAgICAgPC90ZD4NCiAgICAgICAgPC90cj4NCiAgICA8L3Rib2R5Pg0KPC90YWJsZT4NCjxici8+DQqhoaGhytfPyNTas8zQ8rXEaW1wbGVtZW50YXRpb262ztbQtqjS5TO49rOjyv26zdK7uPbA4NDNyfnD96OstPrC68jnz8Kjujxici8+DQo8YnIvPg0KPHRhYmxlIGJvcmRlcmNvbG9yPQ=="#ffcc66" width="90%" align="center" bgcolor="#e3e3e3" border="1">
const

SHERB_NOCONFIRMATION = $00000001 ;

SHERB_NOPROGRESSUI = $00000002 ;

SHERB_NOSOUND = $00000004 ;

type

TSHEmptyRecycleBin=

function(Wnd:HWND;LPCTSTR:PChar;DWORD:Word):Integer;stdcall;

  程序运行初期,程序首先会通过LoadLibrary函数加载动态链接库Shell32.dll,并且把加载后的操作句柄存储在LibHandle中。然后通[email protected]:=GetProcAddress (LibHandle, ’SHEmptyRecycleBinA’)语句读取SHEmptyRecycleBinA函数的地址,如果以上语句能够正确执行([email protected]),[email protected]最后释放动态链接库。响应代码如下:

procedure TForm1.Button1Click(Sender: TObject);
var
 SHEmptyRecycleBin:TSHEmptyRecycleBin;
 LibHandle:THandle;
begin
 LibHandle := LoadLibrary(PChar(’Shell32.dll’)) ;
 if LibHandle <> 0 then
 begin
[email protected] := GetProcAddress(LibHandle, ’SHEmptyRecycleBinA’);
 end
 else
 begin
  MessageDlg(’Failed to load Shell32.dll.’, mtError, [mbOK], 0);
  Exit ;
 end ;
 if @SHEmptyRecycleBin <> nil then
 begin
  SHEmptyRecycleBin(Application.Handle,’’,SHERB_NOCONFIRMATION or
SHERB_NOPROGRESSUI or SHERB_NOSOUND) ;
 end;
 FreeLibrary(LibHandle);
[email protected] := nil ;
end;

  程序代码如下:

unit Unit1;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
 TForm1 = class(TForm)
 Button1: TButton;
 procedure Button1Click(Sender: TObject);
private
 { Private declarations }
public
 { Public declarations }
end;

var
 Form1: TForm1;
 implementation
const
 SHERB_NOCONFIRMATION = $00000001 ;
 SHERB_NOPROGRESSUI = $00000002 ;
 SHERB_NOSOUND = $00000004 ;
type
 TSHEmptyRecycleBin=function(Wnd:HWND;LPCTSTR:PChar;DWORD:Word):Integer;stdcall;

 {$R *.dfm}
 procedure TForm1.Button1Click(Sender: TObject);
var
 SHEmptyRecycleBin:TSHEmptyRecycleBin;
 LibHandle:THandle;
begin
 LibHandle := LoadLibrary(PChar(’Shell32.dll’)) ;
 if LibHandle <> 0 then
 begin
[email protected] := GetProcAddress(LibHandle, ’SHEmptyRecycleBinA’);
 end
 else
 begin
  MessageDlg(’Failed to load Shell32.dll.’, mtError, [mbOK], 0);
  Exit ;
 end ;

 if @SHEmptyRecycleBin <> nil then
 begin
  SHEmptyRecycleBin(Application.Handle,’’,SHERB_NOCONFIRMATION or SHERB_NOPROGRESSUI or SHERB_NOSOUND) ;
 end;
 FreeLibrary(LibHandle);
[email protected] := nil ;
 end;
end.

  保存文件,然后按F9键运行程序,程序运行的初始画面如图2所示。

)jvo輮韜鹝骤w/韎筤n垮vō诋〃ky系统回收站中的文件彻底删除。

  本程序同时也演示了如何调用一个.dll文件中的函数。
Tags:

文章评论


<