我有一个包含文件列表的列表框。我可以访问列表框中的Windows右键菜单来访问打开,属性,删除和重命名项目吗?显示默认的右键菜单 - Delphi
回答
Kermia从JEDI JCL库检查JclShell
单元,本单元内部存在一个名为DisplayContextMenu
的函数,它显示与文件关联的上下文菜单。这个函数将对IContextMenu接口的调用封装起来,使你的工作变得更容易。
function DisplayContextMenu(const Handle: HWND; const FileName: string;
Pos: TPoint): Boolean;
当你想在你的Delphi应用程序中显示类似控件的shell时,我建议你看看类似tpShellShock的东西。它提供了树视图,列表视图等,它们可以像Explorer Windows一样连接在一起。它将显示文件的相应图标。我相信它也提供了你所谈论的设施。
如果您使用的是现代Unicode Delphi,那么可能需要一些移植工作,但是当我这样做时,它证明是相对简单的。
毫无疑问,还有其他的库提供了shell控件,这只是我熟悉的一个。
否则,如果您想坚持使用您当前的解决方案,那么实现您自己的菜单操作是最容易的。打开和属性只是简单的调用ShellExecute与适当的动词。 Delete是对DeleteFile的调用,Rename是对MoveFile的调用。
检查IContextMenu接口。但请注意,Windows shell不会通过文件名识别它的对象 - 实际上它们不能是文件。它使用ID的串联,并且您可能需要在调用其上的某些shell函数之前获取文件所支持的项目ID列表。
看起来像它可以让你枚举shell认为是在上下文菜单中。在我阅读它时,您仍然需要自己填充菜单,然后通过此界面调用命令来响应Delphi OnClick事件。那是对的吗? – 2011-03-12 11:45:19
+1当然看起来是实现OnClick处理程序的好方法。 – 2011-03-12 13:38:51
是的,你必须创建菜单(至少它允许你使用任何你喜欢的菜单),无论如何它给你每个元素支持的命令,并让你调用shell定义的正确命令。它只对文件不起作用,尽管这超出了用户的问题。 – 2011-03-12 15:17:33
下面是使用一个列表框,它是在项目目录中填入文件名的“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;
- 1. 如何从Delphi 2010中的默认右键菜单中删除项目?
- 2. 右键单击后不显示菜单
- 3. 如何禁用浏览器的默认右键单击菜单
- 4. 在WPF GridView中默认的右键单击上下文菜单
- 5. 右键菜单不显示在Firefox(smartgwt)
- 6. 抑制浏览器默认右键单击菜单 - 优化?
- 7. 抑制datagridview中的默认右键菜单vb.net
- 8. 如何自定义windows默认的右键弹出菜单
- 9. UIButton默认点击右键菜单点击导航条
- 10. 右键点击默认silverlight上下文菜单
- 11. 如何正确显示的基于Internet Explorer的右键菜单
- 12. 菜单左,右键菜单
- 13. 从左到右显示从右到左的菜单子菜单
- 14. 显示SWT TableItem的右键单击菜单?
- 15. Tampermonkey - 右键菜单
- 16. QTreeWidget右键菜单
- 17. NSWindow - 右键菜单
- 18. 在flex中的右键单击上下文菜单中隐藏默认选项
- 19. 右键单击菜单项并显示选项
- 20. 在WinRT中显示弹出式右键单击样式菜单
- 21. Java JList右键单击Linux(Ubuntu 12.04)没有显示菜单
- 22. JList右键单击显示菜单(使用,放下,取消)
- 23. NSStatusItem右键单击菜单
- 24. NSEvent单击右键菜单
- 25. DataGridView右键菜单/复制示例?
- 26. 如何右键单击菜单项时显示上下文菜单
- 27. 默认表单不显示
- 28. Raphael:禁用右键单击默认上下文菜单不起作用
- 29. 如何将菜单项添加到默认右键单击devexpress treelist
- 30. 如何在cimg显示窗口中显示右键单击菜单?
+1你作为Handle参数传递了什么?它是干什么用的? – 2011-03-12 19:22:52
@David你可以传递你的表单的句柄,这个句柄用在'IContextMenu.InvokeCommand'中,它接收'CMINVOKECOMMANDINFO'结构http://msdn.microsoft.com/en-us/library/bb773215%28v= vs.85%29.aspx。 – RRUZ 2011-03-12 19:48:29
这个单位有任何更新的版本? (德尔福2010) – Kermia 2011-03-13 09:39:50