Delphi编程实现清空回收站

时间:2010-01-30 12:18:58  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:86e98aa46b3b65937174.gif" />首先在程序的implementation段中定义3个常数和一个类型声明,代码如下:constSHERB_NOCONFIRMATION = $00000001 ;SHERB_NOPROGRESSUI = $00000002 ;SHERB_NOS...
  正文:本程序演示如何通过程序来清空回收站。

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

图1 主界面

首先在程序的implementation段中定义3个常数和一个类型声明,代码如下:

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中。然后通过@SHEmptyRecycleBin:=GetProcAddress (LibHandle, ’SHEmptyRecycleBinA’)语句读取SHEmptyRecycleBinA函数的地址,如果以上语句能够正确执行(也就是@SHEmptyRecycleBin不等于nil),那么就调用SHEmptyRecycleBin完成清空回收站的操作。最后释放动态链接库。响应代码如下:

procedure TForm1.Button1Click(Sender: TObject);
var
 SHEmptyRecycleBin:TSHEmptyRecycleBin;
 LibHandle:THandle;
begin
   LibHandle := LoadLibrary(PChar(’Shell32.dll’)) ;
   if LibHandle <> 0 then
   begin
      @SHEmptyRecycleBin := 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);
   @SHEmptyRecycleBin := nil ;
end;

  程序代码如下:
"Delphi编程实现清空回收站"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Language/Delphi/12660.html


关键字:

关于《Delphi编程实现清空回收站》文章的评论


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
      @SHEmptyRecycleBin := 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);
   @SHEmptyRecycleBin := nil ;
   end;
end.
站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td