type pshnotifystruct = ^shnotifystruct; shnotifystruct = record dwitem1: pitemidlist; dwitem2: pitemidlist; end;
type pshfileinfobyte = ^shfileinfobyte; _shfileinfobyte = record hicon: integer; iicon: integer; dwattributes: integer; szdisplayname: array[0..259] of char; sztypename: array[0..79] of char; end; shfileinfobyte = _shfileinfobyte;
type pidlstruct = ^idlstruct; _idlstruct = record pidl: pitemidlist; bwatchsubfolders: integer; end; idlstruct = _idlstruct;
function shnotify_register(hwnd: integer): bool; function shnotify_unregister: bool; function sheventname(strpath1, strpath2: string; lparam: integer): string;
function shchangenotifyderegister(hnotify: integer): integer; stdcall; external ''''shell32.dll'''' index 4; function shchangenotifyregister(hwnd, uflags, dweventid, umsg, citems: longword; lpps: pidlstruct): integer; stdcall; external ''''shell32.dll'''' index 2; function shgetfileinfopidl(pidl: pitemidlist; dwfileattributes: integer; psfib: pshfileinfobyte; cbfileinfo: integer; uflags: integer): integer; stdcall; external ''''shell32.dll'''' name ''''shgetfileinfoa'''';
var Form1: TForm1; m_hshnotify: integer; m_pidldesktop: pitemidlist;
function shnotify_register(hwnd: integer): bool; var ps: pidlstruct; begin {$R-} result := false; if m_hshnotify = 0 thenbegin //获取桌面文件夹的pidl if shgetspecialfolderlocation(0, CSIDL_DESKTOP, m_pidldesktop) <> noerror then form1.close; if boolean(m_pidldesktop) thenbegin new(ps); try ps.bwatchsubfolders := 1; ps.pidl := m_pidldesktop;
// 利用shchangenotifyregister函数注册系统消息处理 m_hshnotify := shchangenotifyregister(hwnd, (shcnf_typeor shcnf_idlist), (shcne_allevents or shcne_interrupt), wm_shnotify, 1, ps); result := boolean(m_hshnotify); finally FreeMem(ps); end; end