等待IContextMenu.InvokeCommand启动进程

问题描述 投票:3回答:1

我有一个TListView,其项目是文件,用户可以通过双击打开它们。

为此,我将文件保存在windows temp文件夹中,启动一个用ShellExecuteEx()打开保存文件的线程,让它等待ShellExecuteInfo.hProcess,如下所示:

TNotifyThread = class(TThread)
private
  FFileName: string;
  FFileAge: TDateTime;
public
  constructor Create(const FileName: string; OnClosed: TNotifyEvent); overload;
  procedure Execute; override;

  property FileName: String read FFileName;
  property FileAge: TDateTime read FFileAge;
end;

{...}

constructor TNotifyThread.Create(const FileName: string; OnClosed: TNotifyEvent);
begin
  inherited Create(True);
  if FileExists(FileName) then
    FileAge(FileName, FFileAge);

  FreeOnTerminate := True;
  OnTerminate := OnClosed;
  FFileName := FileName;

  Resume;
end;

procedure TNotifyThread.Execute;
var
  se: SHELLEXECUTEINFO;
  ok: boolean;
begin
  with se do
  begin
    cbSize := SizeOf(SHELLEXECUTEINFO);
    fMask := SEE_MASK_INVOKEIDLIST or SEE_MASK_NOCLOSEPROCESS or SEE_MASK_NOASYNC;
    lpVerb := PChar('open');
    lpFile := PChar(FFileName);
    lpParameters := nil;
    lpDirectory := PChar(ExtractFilePath(ParamStr(0)));
    nShow := SW_SHOW;
  end;

  if ShellExecuteEx(@se) then
  begin
    WaitForSingleObject(se.hProcess, INFINITE);
    if se.hProcess <> 0 then
      CloseHandle(se.hProcess);
  end;
end;

这样,我可以使用TThread.OnTerminate事件来回写用户关闭后对文件所做的任何更改。

我现在在JclShell.DisplayContextMenu()(使用IContextMenu)的帮助下显示windows上下文菜单。

我的目标:等待在上下文菜单中选择的执行操作(例如“属性”,“删除”,...)完成(或以任何方式获得通知),以便我可以检查临时文件的更改将这些写回来,或删除时删除TListItem

由于CMINVOKECOMMANDINFO没有返回像SHELLEXECUTEINFO那样的进程句柄,我无法以同样的方式执行。

MakeIntResource(commandId-1)分配给SHELLEXECUTEINFO.lpVerbShellExecuteEx()EAccessViolation坠毁。这种方法似乎不支持SHELLEXECUTEINFO

我试图用IContextMenu.GetCommandString()获取命令字符串,并从TrackPopupMenu()获取命令ID,然后将其传递给SHELLEXECUTEINFO.lpVerb,但GetCommandString()不会返回点击某些项目的命令。

工作菜单项:

属性,编辑,复制,剪切,打印,7z:添加到存档(动词是'SevenZipCompress',不会返回processHandle),KapserskyScan(动词是'KL_scan',不会返回processHandle)

不工作:

“打开”或“发送到”内的任何内容

这只是IContextMenu实施的错误吗?

也许这与我使用AnsiStrings有关?但是我无法让GCS_VERBW工作。有没有比这更可靠地获得CommandString的方法?

function CustomDisplayContextMenuPidlWithoutExecute(const Handle: THandle; 
const Folder: IShellFolder;
  Item: PItemIdList; Pos: TPoint): String;
var
  ContextMenu: IContextMenu;
  ContextMenu2: IContextMenu2;
  Menu: HMENU;
  CallbackWindow: THandle;
  LResult: AnsiString;
  Cmd: Cardinal;
begin
  Result := '';
  if (Item = nil) or (Folder = nil) then
    Exit;
  Folder.GetUIObjectOf(Handle, 1, Item, IID_IContextMenu, nil,
    Pointer(ContextMenu));
  if ContextMenu <> nil then
  begin
    Menu := CreatePopupMenu;
    if Menu <> 0 then
    begin
      if Succeeded(ContextMenu.QueryContextMenu(Menu, 0, 1, $7FFF, CMF_EXPLORE)) then
      begin
        CallbackWindow := 0;
        if Succeeded(ContextMenu.QueryInterface(IContextMenu2, ContextMenu2)) then
        begin
          CallbackWindow := CreateMenuCallbackWnd(ContextMenu2);
        end;
        ClientToScreen(Handle, Pos);
        cmd := Cardinal(TrackPopupMenu(Menu, TPM_LEFTALIGN or TPM_LEFTBUTTON or
          TPM_RIGHTBUTTON or TPM_RETURNCMD, Pos.X, Pos.Y, 0, CallbackWindow, nil));
        if Cmd <> 0 then
        begin
          SetLength(LResult, MAX_PATH);
          cmd := ContextMenu.GetCommandString(Cmd-1, GCS_VERBA, nil, LPSTR(LResult), MAX_PATH);
          Result := String(LResult);
        end;
        if CallbackWindow <> 0 then
          DestroyWindow(CallbackWindow);
      end;
      DestroyMenu(Menu);
    end;
  end;
end;

我读过关于How to host an IContextMenu的Raymond Chen的博客,以及关于MSDN的研究(例如CMINVOKECOMMANDINFOGetCommandString()SHELLEXECUTEINFOTrackPopupMenu()),但我可能错过了一些微不足道的东西。

windows delphi winapi delphi-10.2-tokyo
1个回答
0
投票

我最终使用TJvChangeNotify监视windows临时文件夹,同时将监视文件保存在TDictionary<FileName:String, LastWrite: TDateTime>中。

因此,每当TJvChangeNotify触发OnChangeNotify事件时,我都可以检查我的哪些监控文件已被删除(通过检查存在)或已经更改(通过比较上次写入时间)。

示例ChangeNotifyEvent

procedure TFileChangeMonitor.ChangeNotifyEvent(Sender: TObject; Dir: string;
  Actions: TJvChangeActions);
var
  LFile: TPair<String, TDateTime>;
  LSearchRec: TSearchRec;
  LFoundErrorCode: Integer;
begin
  for LFile in FMonitoredFiles do
  begin
    LFoundErrorCode := FindFirst(LFile.Key, faAnyFile, LSearchRec);
    try
      if LFoundErrorCode = NOERROR then
      begin
        if LSearchRec.TimeStamp > LFile.Value then
        begin
          // do something with the changed file
          {...}

          // update last write time
          FMonitoredFiles.AddOrSetValue(LFile.Key, LSearchRec.TimeStamp);
        end;
      end // 
      else if (LFoundErrorCode = ERROR_FILE_NOT_FOUND) then
      begin
        // do something with the deleted file
        {...}

        // stop monitoring the deleted file
        FMonitoredFiles.Remove(LFile.Key);
      end;
    finally
      System.SysUtils.FindClose(LSearchRec);
    end;
  end;
end;
© www.soinside.com 2019 - 2024. All rights reserved.