您可以invoke "open" from context popup menu on .lnk file。這會給你同樣的行爲,雙擊在資源管理器.lnk文件:
function SHBindToParent(pidl: PItemIDList; const riid: TIID; out ppv; out ppidlLast: PItemIDList): HResult; stdcall; external 'shell32.dll' name 'SHBindToParent';
procedure ExecuteFile(const AWnd: HWND; const AFileName: String);
function GetUIObjectOfFile(wnd: HWND; const pszPath: WideString; const riid: TGUID; out ppv): HRESULT;
var
pidl: PItemIDList;
sfgao: DWord;
psf: IShellFolder;
pidlChild: PItemIDList;
begin
DWord(ppv) := 0;
Result := SHParseDisplayName(PWideChar(pszPath), nil, pidl, 0, sfgao);
if SUCCEEDED(Result) then
try
Result := SHBindToParent(pidl, IID_IShellFolder, psf, pidlChild);
if SUCCEEDED(Result) then
try
Result := psf.GetUIObjectOf(wnd, 1, pidlChild, riid, nil, ppv);
finally
psf := nil;
end;
finally
CoTaskMemFree(pidl);
end;
end;
const
SCRATCH_QCM_FIRST = 1;
SCRATCH_QCM_LAST = $7FFF;
var
pcm: IContextMenu;
Menu: HMENU;
Info: TCMInvokeCommandInfo;
Id: UINT;
begin
if SUCCEEDED(GetUIObjectOfFile(AWnd, PChar(AFileName), IID_IContextMenu, pcm)) then
try
Menu := CreatePopupMenu;
if Menu <> 0 then
try
if SUCCEEDED(pcm.QueryContextMenu(Menu, 0, SCRATCH_QCM_FIRST, SCRATCH_QCM_LAST, CMF_DEFAULTONLY)) then
begin
Id := GetMenuDefaultItem(Menu, 0, 0);
if Id <> UINT(-1) then
begin
FillChar(Info, SizeOf(Info), 0);
Info.cbSize := SizeOf(info);
Info.hwnd := Handle;
Info.lpVerb := MAKEINTRESOURCEA(Id - SCRATCH_QCM_FIRST);
SetLastError(pcm.InvokeCommand(Info));
if GetLastError <> 0 then
RaiseLastOSError;
end;
end;
finally
DestroyMenu(Menu);
end;
finally
pcm := nil;
end;
end;
同樣應該調用的ShellExecuteEx與SEE_MASK_INVOKEIDLIST標誌來archieved。
@TobiasR明白了,我會試試看,謝謝U – Hanlin
@TobiasR這就是如何調用Open With對話框。 –
@DavidHeffernan uups - 正如你所說的那樣......是真的。 –