@@ -6,7 +6,8 @@ interface
66
77uses
88 Classes, SysUtils, LCLType, Forms, Controls, Graphics, Dialogs,
9- ExtCtrls, StdCtrls, ShellCtrls, ComCtrls, apphelpers, extra_controls, Math, generic_types;
9+ ExtCtrls, StdCtrls, ShellCtrls, ComCtrls, apphelpers, extra_controls, Math, generic_types
10+ { $IFDEF WINDOWS} , Windows, WinDirs, ActiveX, ShlObj, LazUTF8 { $ENDIF} ;
1011
1112type
1213
@@ -47,6 +48,8 @@ TfrmExtFileDialog = class(TExtForm)
4748 procedure ShellListViewFileAdded (Sender: TObject; Item: TListItem);
4849 procedure ShellListViewSelectItem (Sender: TObject; Item: TListItem;
4950 Selected: Boolean);
51+ procedure ShellListViewContextPopup (Sender: TObject; MousePos: TPoint;
52+ var Handled: Boolean);
5053 procedure ShellTreeViewChange (Sender: TObject; Node: TTreeNode);
5154 procedure ShellTreeViewChanging (Sender: TObject; Node: TTreeNode;
5255 var AllowChange: Boolean);
@@ -65,6 +68,9 @@ TfrmExtFileDialog = class(TExtForm)
6568 FFiles: TStringList;
6669 FOnTypeChange: TNotifyEvent;
6770 FClickedPathPart: String;
71+ { $IFDEF WINDOWS}
72+ function ShowShellContextMenu (FileName: String; ScreenPos: TPoint): Boolean;
73+ { $ENDIF}
6874 procedure SetTitle (AValue: String);
6975 function GetFileName : String;
7076 procedure SetFileName (const AValue: String);
@@ -132,6 +138,9 @@ procedure TfrmExtFileDialog.FormCreate(Sender: TObject);
132138 ShellListView.SmallImages := MainForm.ImageListMain;
133139 ShellListView.OnFileAdded := ShellListViewFileAdded;
134140 { $ENDIF}
141+ { $IFDEF WINDOWS}
142+ ShellListView.OnContextPopup := ShellListViewContextPopup;
143+ { $ENDIF}
135144 FFilterNames := TStringList.Create;
136145 FFilterMasks := TStringList.Create;
137146 FFilterIndex := 0 ;
@@ -360,6 +369,27 @@ procedure TfrmExtFileDialog.ShellListViewSelectItem(Sender: TObject;
360369 end ;
361370end ;
362371
372+ procedure TfrmExtFileDialog.ShellListViewContextPopup (Sender: TObject;
373+ MousePos: TPoint; var Handled: Boolean);
374+ var
375+ { $IFDEF WINDOWS}
376+ Item: TListItem;
377+ FileName: String;
378+ { $ENDIF}
379+ begin
380+ { $IFDEF WINDOWS}
381+ Item := ShellListView.GetItemAt(MousePos.X, MousePos.Y);
382+ if (Item <> nil ) and (not Item.Selected) then
383+ ShellListView.Selected := Item; // let the context menu act on the clicked item, like in Explorer
384+ if Item <> nil then
385+ FileName := ShellListView.GetPathFromItem(Item)
386+ else
387+ FileName := ShellTreeView.Path; // right-click on empty space: context menu of the current folder
388+ if not FileName.IsEmpty then
389+ Handled := ShowShellContextMenu(FileName, ShellListView.ClientToScreen(MousePos));
390+ { $ENDIF}
391+ end ;
392+
363393procedure TfrmExtFileDialog.ShellTreeViewChange (Sender: TObject; Node: TTreeNode
364394 );
365395begin
@@ -452,6 +482,64 @@ procedure TfrmExtFileDialog.SetFilterIndex(AValue: Integer);
452482end ;
453483
454484
485+ { $IFDEF WINDOWS}
486+ function TfrmExtFileDialog.ShowShellContextMenu (FileName: String;
487+ ScreenPos: TPoint): Boolean;
488+ var
489+ DesktopFolder, Folder: IShellFolder;
490+ FullPidl: PItemIDList;
491+ RelPidl: LPCITEMIDLIST;
492+ FolderPtr: Pointer;
493+ Eaten, Attrs: ULONG;
494+ ContextMenu: IContextMenu;
495+ Menu: HMENU;
496+ Cmd, CmdFirst: Integer;
497+ InvokeInfo: TCMINVOKECOMMANDINFO;
498+ Wide: WideString;
499+ begin
500+ Result := False;
501+ FileName := ExcludeTrailingPathDelimiter(ExpandFileName(FileName));
502+ if not (FileExists(FileName) or DirectoryExists(FileName)) then
503+ Exit;
504+ if SHGetDesktopFolder(DesktopFolder) <> S_OK then
505+ Exit;
506+ Attrs := 0 ;
507+ RelPidl := nil ;
508+ Wide := UTF8ToUTF16(FileName); // strings in this app are UTF-8
509+ // Absolute PIDL of the file or folder
510+ if DesktopFolder.ParseDisplayName(Handle, nil , PWideChar(Wide), Eaten, FullPidl, Attrs) <> S_OK then
511+ Exit;
512+ try
513+ // Parent folder + relative PIDL, so the menu refers to the item itself
514+ if SHBindToParent(FullPidl, IID_IShellFolder, @FolderPtr, RelPidl) <> S_OK then
515+ Exit;
516+ Folder := IShellFolder(FolderPtr);
517+ if Folder.GetUIObjectOf(Handle, 1 , RelPidl, IID_IContextMenu, nil , ContextMenu) <> S_OK then
518+ Exit;
519+ Menu := CreatePopupMenu;
520+ try
521+ CmdFirst := 1 ;
522+ if ContextMenu.QueryContextMenu(Menu, 0 , CmdFirst, $7FFF, CMF_NORMAL or CMF_EXPLORE) < 0 then
523+ Exit;
524+ Cmd := Integer(TrackPopupMenu(Menu, TPM_RETURNCMD or TPM_NONOTIFY or TPM_LEFTALIGN or TPM_LEFTBUTTON, ScreenPos.X, ScreenPos.Y, 0 , Handle, nil ));
525+ if Cmd <= 0 then
526+ Exit;
527+ InvokeInfo := Default(TCMINVOKECOMMANDINFO);
528+ InvokeInfo.cbSize := SizeOf(InvokeInfo);
529+ InvokeInfo.hwnd := Handle;
530+ InvokeInfo.lpVerb := PAnsiChar(PtrUInt(Cmd - CmdFirst));
531+ InvokeInfo.nShow := SW_SHOWNORMAL;
532+ ContextMenu.InvokeCommand(InvokeInfo);
533+ Result := True;
534+ finally
535+ DestroyMenu(Menu);
536+ end ;
537+ finally
538+ CoTaskMemFree(FullPidl); // RelPidl points into FullPidl and must not be freed separately
539+ end ;
540+ end ;
541+ { $ENDIF}
542+
455543{ TExtFileOpenDialog }
456544
457545procedure TExtFileOpenDialog.FormCreate (Sender: TObject);
0 commit comments