Use Delphi to write empty Recycle Bin program

Source: Internet
Author: User
Tags response code

This program shows you how to empty the Recycle Bin through a program.

Add a TButton component to the form that activates the emptying of the Recycle Bin. The design-finished main interface is shown in Figure 1.

Figure 1 Main interface

First, you define 3 constants and a type declaration in the implementation section of your program, and the code is as follows:

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

At the beginning of the program, the program will first load the dynamic link library Shell32.dll through the LoadLibrary function, and store the loaded operation handle in the Libhandle. The address of the Shemptyrecyclebina function is then read through the @shemptyrecyclebin:=getprocaddress (Libhandle, ' Shemptyrecyclebina ') statement if the above statement executes correctly (That is, @shemptyrecyclebin is not equal to nil), then call Shemptyrecyclebin to complete the empty Recycle Bin operation. Finally, release the dynamic link library. The response code is as follows:

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.