procedure ContextMenuForFile(FileName: TFileName; X, Y: Integer;
Handle: HWND);
var
aContextMenu : IContextMenu;
aPrgOut : Pointer;
aPopup : HMENU;
aCmd : Integer;
aCmdInfo : TCMInvokeCommandInfo;
PIDL : PItemIDList;
ShellFolder : IShellFolder;
begin
PIDL := SHGetIDListFromPath(FileName, ShellFolder);
if not Assigned(PIDL)
then
Exit;
aPrgOut :=
nil;
OLECheck(ShellFolder.GetUIObjectOf(0, 1, PIDL, IID_IContextMenu, aPrgOut,
Pointer(aContextMenu)));
aPopup := CreatePopUpMenu;
if aPopup = 0
then
Exit;
try
OLECheck(aContextMenu.QueryContextMenu(aPopup, 0, 1, $7FFF, CMF_NORMAL));
aCmd := Integer(TrackPopupMenuEx(aPopup, TPM_LEFTALIGN
or TPM_RETURNCMD
or
TPM_RIGHTBUTTON
or TPM_HORIZONTAL
or TPM_VERTICAL, X, Y,
Handle,
nil));
if aCmd <> 0
then
begin
FillChar(aCmdInfo, Sizeof(aCmdInfo), 0);
with aCmdInfo
do
begin
cbSize := SizeOf(TCMInvokeCommandInfo);
lpVerb := MakeIntResource(aCmd - 1);
nShow := SW_SHOWNORMAL;
end;
try
aContextMenu.InvokeCommand(aCmdInfo);
except
end;
end;
finally
DestroyMenu(aPopup);
end;
end;