我有一个包含文件列表的列表框。我可以访问列表框中的 Windows 右键菜单来访问打开、属性、删除和重命名项目吗?
4 回答
Kermia 检查JclShell
JEDI JCL 库中的单元,在该单元内部存在一个名为的函数 ,该函数DisplayContextMenu
显示与文件关联的上下文菜单。该函数封装了对 IContextMenu 接口的调用,使您的工作更加轻松。
function DisplayContextMenu(const Handle: HWND; const FileName: string;
Pos: TPoint): Boolean;
检查IContextMenu接口。但请注意,Windows shell 不会通过文件名识别其对象——实际上它们不可能是文件。它使用 id 的串联,在调用一些 shell 函数之前,您可能需要获取文件分配到的项目 id 列表。
这是一个使用列表框的“OnContextPopup”事件的实现示例,该列表框填充了项目目录中的文件名,在右键单击文件名时启动文件的快捷菜单:
type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure ListBox1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
private
protected
procedure WndProc(var Msg: TMessage); override;
public
end;
var
Form1: TForm1;
implementation
uses
shlobj, comobj;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
SearchRec: TSearchRec;
begin
ListBox1.Clear;
// populate list box with files in the project folder
if FindFirst(ExtractFilePath(Application.ExeName) + '*.*',
0, SearchRec) = 0 then
repeat
ListBox1.Items.Add(SearchRec.Name);
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
var
// Required to handle messages for owner drawn items, as in 'SendTo' menu.
// Also used as a flag in WndProc to know if we're tracking a shortcut menu.
ContextMenu2: IContextMenu2 = nil;
procedure TForm1.ListBox1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
var
Item: Integer;
DeskFolder, Folder: IShellFolder;
Eaten, Attributes: ULONG;
pIdl, FolderpIdl: PItemIDList;
ContextMenu: IContextMenu;
Menu: HMENU;
Pos: TPoint;
Cmd: DWORD;
CommandInfo: TCMInvokeCommandInfo;
begin
Item := (Sender as TListBox).ItemAtPos(MousePos, True);
Handled := Item <> -1;
if not Handled then
Exit;
TListBox(Sender).ItemIndex := Item;
// IShellFolder for Desktop folder (root)
OleCheck(SHGetDesktopFolder(DeskFolder));
// Item ID List for the folder that the file is in
Attributes := 0;
OleCheck(DeskFolder.ParseDisplayName(Handle, nil,
PWideChar(WideString(ExtractFilePath(Application.ExeName))),
Eaten, FolderpIdl, Attributes));
// IShellFolder for the folder the file is in
OleCheck(DeskFolder.BindToObject(FolderpIdl, nil, IID_IShellFolder, Folder));
CoTaskMemFree(FolderpIdl);
// Item ID List for the file, relative to the folder it is in
Attributes := 0;
OleCheck(Folder.ParseDisplayName(Handle, nil,
PWideChar(WideString(ExtractFileName(TListBox(Sender).Items[Item]))),
Eaten, pIdl, Attributes));
// IContextMenu for the relative Item ID List
OleCheck(Folder.GetUIObjectOf(Handle, 1, pIdl, IID_IContextMenu,
nil, ContextMenu));
CoTaskMemFree(pIdl);
Menu := CreatePopupMenu;
try
// Populate our menu with shortcut items
OleCheck(ContextMenu.QueryContextMenu(Menu, 0, 1, $7FFF, CMF_EXPLORE));
// ContextMenu2 used in WndProc
ContextMenu.QueryInterface(IID_IContextMenu2, ContextMenu2);
try
Pos := TWinControl(Sender).ClientToScreen(MousePos);
// launch the menu
Bool(Cmd) := TrackPopupMenu(Menu,
TPM_LEFTBUTTON or TPM_RIGHTBUTTON or TPM_RETURNCMD,
Pos.X, Pos.Y, 0, Handle, nil);
finally
// clear so that we don't intervene every owner drawn menu item message in
// WndProc
ContextMenu2 := nil;
end;
// Invoke command if we have one
if Bool(Cmd) then begin
FillChar(CommandInfo, SizeOf(CommandInfo), 0);
CommandInfo.cbSize := SizeOf(CommandInfo);
CommandInfo.hwnd := Handle;
CommandInfo.lpVerb := MakeIntResource(Cmd - 1);
CommandInfo.nShow := SW_SHOWNORMAL;
OleCheck(ContextMenu.InvokeCommand(CommandInfo));
end;
finally
DestroyMenu(Menu);
end;
end;
procedure TForm1.WndProc(var Msg: TMessage);
begin
if ((Msg.Msg = WM_INITMENUPOPUP) or (Msg.Msg = WM_DRAWITEM)
or (Msg.Msg = WM_MEASUREITEM)) and Assigned(ContextMenu2) then
ContextMenu2.HandleMenuMsg(Msg.Msg, Msg.WParam, Msg.LParam)
else
inherited;
end;
当您想在 Delphi 应用程序中显示类似 shell 的控件时,我建议您查看类似tpShellShock的内容。它提供了可以像资源管理器 Windows 一样连接在一起的树视图、列表视图等。它将显示文件的相应图标。我相信它也提供了你所说的设施。
如果您使用的是现代 Unicode Delphi,它可能需要一些移植工作,但是当我这样做时,它被证明相对简单。
毫无疑问,还有其他提供 shell 控件的库,这只是我熟悉的一个。
否则,如果您想坚持当前的解决方案,最容易实现自己的菜单操作。Open 和 Properties 只是使用适当动词对 ShellExecute 的简单调用。Delete 是对 DeleteFile 的调用,而 Rename 是对 MoveFile 的调用。