Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Events Component 1

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,311
US
In the course of making a functionally equivalent Delphi version of [link
]this[/url], I ended up making a component to handle shell events that I thought I would share. The sample at the end of the FAQ is from that code, which registers events to look for changes in the recycle bin.

faq102-7539

I don't know everything that can be done with shell events nor have tested everything that seems to be indicated for shell events. Hopefully someone can make good use of this.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Here's another example to demonstrate the component I posted to the FAQ. All this does (other than show Form1) is put up a message up when you do something with removable disks or drives:

Code:
unit Unit1;

// written by Glenn9999 @ tek-tips.com
interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  shellevents, shellapi, shlobj, ActiveX;

type
  TForm1 = class(TForm)
    ShellNotifyHandler1: TShellNotifyHandler;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ShellNotifyHandler1ShellNotify(Sender: TObject;
      LEvent: TShellEvent; pidl1, pidl2: PItemIDList);
  private
    { Private declarations }
  public
    { Public declarations }
    RegisterHandle: DWord;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
const
  WM_SHELLNOTIFY = WM_USER + $200;
var
  pidl: PItemIDList;
begin
  ShellNotifyHandler1.ShellMsg := WM_SHELLNOTIFY;
  ShellNotifyHandler1.Events :=
                [seMediaInserted,seMediaRemoved,seDriveRemoved,seDriveAdd];
  ShellNotifyHandler1.Recursive := true;
  pidl :=  nil;
  RegisterHandle := ShellNotifyHandler1.RegisterPIDL(Form1.Handle, pidl);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ShellNotifyHandler1.Deregister(RegisterHandle);
end;

function StrRetToString(inret: TStrRet): String;
var
  Malloc: IMalloc;
begin
  SHGetMalloc(Malloc);
  Result := '';
  case inRet.uType of
    STRRET_CSTR:
      Result := String(inRet.cStr);
    STRRET_OFFSET: ;
    STRRET_WSTR:
      begin
        Result := String(Widestring(inRet.pOleStr));
        Malloc.Free(inRet.pOleStr);
      end;
  end;
end;

procedure TForm1.ShellNotifyHandler1ShellNotify(Sender: TObject;
  LEvent: TShellEvent; pidl1, pidl2: PItemIDList);
var
  isf: IShellFolder;
  psi1: TStrRet;
  outstr: string;
begin
  SHGetDesktopFolder(isf);
  isf.GetDisplaynameOf(pidl1, SHGDN_NORMAL, psi1);
  outstr := StrRetToString(psi1);
  case LEvent of
    // these messages happen, for example, when you plug in or unplug a USB drive.
    seDriveAdd: ShowMessage('Drive Added: ' + outstr);
    seDriveRemoved: ShowMessage('Drive Removed: ' + outstr);
    // these messages happen, for example, when you do something to a disk in your CD/DVD drive.
    seMediaInserted: ShowMessage('Media Inserted: ' + outstr);
    seMediaRemoved: ShowMessage('Media Removed: ' + outstr);
  end;
end;

end.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top