2011-03-12 84 views

回答

8

Kermia从JEDI JCL库检查JclShell单元,本单元内部存在一个名为DisplayContextMenu的函数,它显示与文件关联的上下文菜单。这个函数将对IContextMenu接口的调用封装起来,使你的工作变得更容易。

function DisplayContextMenu(const Handle: HWND; const FileName: string; 
    Pos: TPoint): Boolean; 
+0

+1你作为Handle参数传递了什么?它是干什么用的? – 2011-03-12 19:22:52

+0

@David你可以传递你的表单的句柄,这个句柄用在'IContextMenu.InvokeCommand'中,它接收'CMINVOKECOMMANDINFO'结构http://msdn.microsoft.com/en-us/library/bb773215%28v= vs.85%29.aspx。 – RRUZ 2011-03-12 19:48:29

+0

这个单位有任何更新的版本? (德尔福2010) – Kermia 2011-03-13 09:39:50

2

当你想在你的Delphi应用程序中显示类似控件的shell时,我建议你看看类似tpShellShock的东西。它提供了树视图,列表视图等,它们可以像Explorer Windows一样连接在一起。它将显示文件的相应图标。我相信它也提供了你所谈论的设施。

如果您使用的是现代Unicode Delphi,那么可能需要一些移植工作,但是当我这样做时,它证明是相对简单的。

毫无疑问,还有其他的库提供了shell控件,这只是我熟悉的一个。

否则,如果您想坚持使用您当前的解决方案,那么实现您自己的菜单操作是最容易的。打开和属性只是简单的调用ShellExecute与适当的动词。 Delete是对DeleteFile的调用,Rename是对MoveFile的调用。

5

检查IContextMenu接口。但请注意,Windows shell不会通过文件名识别它的对象 - 实际上它们不能是文件。它使用ID的串联,并且您可能需要在调用其上的某些shell函数之前获取文件所支持的项目ID列表。

+0

看起来像它可以让你枚举shell认为是在上下文菜单中。在我阅读它时,您仍然需要自己填充菜单,然后通过此界面调用命令来响应Delphi OnClick事件。那是对的吗? – 2011-03-12 11:45:19

+0

+1当然看起来是实现OnClick处理程序的好方法。 – 2011-03-12 13:38:51

+0

是的,你必须创建菜单(至少它允许你使用任何你喜欢的菜单),无论如何它给你每个元素支持的命令,并让你调用shell定义的正确命令。它只对文件不起作用,尽管这超出了用户的问题。 – 2011-03-12 15:17:33

3

下面是使用一个列表框,它是在项目目录中填入文件名的“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; 
相关问题