好学IT学院:IT信息技术分享交流平台
标签:Delphi  来源:互联网  作者:本站整理  发布时间:2009-05-16  ★★★加入收藏〗〖手机版
摘要: 在自己的界面中弹出系统右键菜单。unit PopupShell;interfaceusesWindows, Messages, SysUtils, StrUtils, ComObj,ShlObj,ActiveX;function Disp……

function DriveToPidlBind(const DriveName: string; out Folder: IShellFolder):
  PItemIdList;
var
  Attr: ULONG;
  Eaten: ULONG;
  DesktopFolder: IShellFolder;
  Drives: PItemIdList;
  Path: TUnicodePath;
begin
  Result := nil;
  if Succeeded(SHGetDesktopFolder(DesktopFolder)) then begin
  if Succeeded(SHGetSpecialFolderLocation(0, CSIDL_DRIVES, Drives)) then begin
    if Succeeded(DesktopFolder.BindToObject(Drives, nil, IID_IShellFolder, Pointer(Folder))) then  begin
      MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PChar(PathAddSeparator(DriveName)), -1, Path, MAX_PATH);
      if FAILED(Folder.ParseDisplayName(0, nil, Path, Eaten, Result, Attr)) then begin
        Folder := nil;
      end;
    end;
  end;
  PidlFree(Drives);
  end;
end;

function PathToPidlBind(const FileName: string; out Folder: IShellFolder):
  PItemIdList;
var
  Attr, Eaten: ULONG;
  PathIdList: PItemIdList;
  DesktopFolder: IShellFolder;
  Path, ItemName: pwidechar;
  s1,s2: string;
  k: integer;
begin
  Result := nil;
 
  s1:= ExtractFilePath(FileName);
  s2:= ExtractFileName(FileName);
  Path:= a2u(s1);
  ItemName:= a2u(s2);

if Succeeded(SHGetDesktopFolder(DesktopFolder)) then begin
  if Succeeded(DesktopFolder.ParseDisplayName(0, nil, Path, Eaten, PathIdList, Attr)) then begin // FAIL
    if Succeeded(DesktopFolder.BindToObject(PathIdList, nil, IID_IShellFolder, Pointer(Folder))) then begin
      if FAILED(Folder.ParseDisplayName(0, nil, ItemName, Eaten, Result, Attr)) then begin
        Folder := nil;
        Result := DriveToPidlBind(FileName, Folder);
      end;
    end;
    PidlFree(PathIdList);
  end
  else
    Result := DriveToPidlBind(FileName, Folder);
  end;

FreeMem(Path);
  FreeMem(ItemName);
end;

function LANToPidlBind(NetPath: string; out Folder: IShellFolder): PItemIdList;
var
  Attr, Eaten: ULONG;
  DesktopFolder: IShellFolder;
  nt,pid1,pid2: PItemIdList;
  Path,FullPath: pwidechar;
  s1,s2: string;
  k: integer;
  err: HResult;
begin
  Result := nil;
end;

function DisplayContextMenu(const Handle: Thandle; const FileName: string; Pos: TPoint): Boolean;
var
  ItemIdList: PItemIdList;
  Folder: IShellFolder;
begin
  Result := False;
  ItemIdList := PathToPidlBind(FileName, Folder);

if ItemIdList <> nil then
  begin
  Result := DisplayContextMenuPidl(Handle, Folder, ItemIdList, Pos);
  PidlFree(ItemIdList);
  //Folder:= nil;
  end;
end;

end.

// ========================================= //

procedure TFormTest.Button59Click(Sender: TObject);
var
  pos: TPoint;
begin
  GetCursorPos(pos);
  pos:= ScreenToClient(pos);
  DisplayContextMenu(handle, 'c:\tools\setup', pos);
end;

delphi7从入门到精通之四
  Delphi编辑器在表面上,对IDE的第7个版本来说Delphi的编辑器看来似乎没有改变许多。然而,在幕后,它是一个全新的工具。除…
  • 好学触屏公众号虎力全开、杨帆起航!
  • 四大名著全套小学生版注音版
  • 诗词中的科学全4册
  • 曹文轩系列儿童文学全套画本