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

// 类型转换 PWideChar,不用StringToOleStr,测试存在内存泄
  Function A2U(const s: String): PWideChar;
  begin
  if s = '' then begin
    result:= nil;
    exit;
  end;
  result:= AllocMem((Length(s) + 1) * sizeOf(widechar));
  StringToWidechar(s, result, Length(s) * sizeOf(widechar) + 1);
  end;

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

function PidlFree(var IdList: PItemIdList): Boolean;
var
  Malloc: IMalloc;
begin
  Result := False;
  if IdList = nil then
  Result := True
  else
  begin
  if Succeeded(SHGetMalloc(Malloc)) and (Malloc.DidAlloc(IdList) > 0) then
  begin
    Malloc.Free(IdList);
    IdList := nil;
    Result := True;
  end;
  end;
end;

function MenuCallback(Wnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM):
  LRESULT; stdcall;
var
  ContextMenu2: IContextMenu2;
begin
  case Msg of
  WM_CREATE:
    begin
      ContextMenu2 := IContextMenu2(PCreateStruct(lParam).lpCreateParams);
      SetWindowLong(Wnd, GWL_USERDATA, Longint(ContextMenu2));
      Result := DefWindowProc(Wnd, Msg, wParam, lParam);
    end;
  WM_INITMENUPOPUP:
    begin
      ContextMenu2 := IContextMenu2(GetWindowLong(Wnd, GWL_USERDATA));
      ContextMenu2.HandleMenuMsg(Msg, wParam, lParam);
      Result := 0;
    end;
  WM_DRAWITEM, WM_MEASUREITEM:
    begin
      ContextMenu2 := IContextMenu2(GetWindowLong(Wnd, GWL_USERDATA));
      ContextMenu2.HandleMenuMsg(Msg, wParam, lParam);
      Result := 1;
    end;
  else
  Result := DefWindowProc(Wnd, Msg, wParam, lParam);
  end;
end;

function CreateMenuCallbackWnd(const ContextMenu: IContextMenu2): HWND;
const
  IcmCallbackWnd = 'ICMCALLBACKWND';
var
  WndClass: TWndClass;
begin
  FillChar(WndClass, SizeOf(WndClass), #0);
  WndClass.lpszClassName := PChar(IcmCallbackWnd);
  WndClass.lpfnWndProc := @MenuCallback;
  WndClass.hInstance := HInstance;
  Windows.RegisterClass(WndClass);
  Result := CreateWindow(IcmCallbackWnd, IcmCallbackWnd, WS_POPUPWINDOW, 0,
  0, 0, 0, 0, 0, HInstance, Pointer(ContextMenu));
end;

function DisplayContextMenuPidl(const Handle: HWND; const Folder: IShellFolder;
  Item: PItemIdList; Pos: TPoint): Boolean;
var
  Cmd: Cardinal;
  ContextMenu: IContextMenu;
  ContextMenu2: IContextMenu2;
  Menu: HMENU;
  CommandInfo: TCMInvokeCommandInfo;
  CallbackWindow: HWND;
begin
  Result := False;
  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
        FillChar(CommandInfo, SizeOf(CommandInfo), #0);
        CommandInfo.cbSize := SizeOf(TCMInvokeCommandInfo);
        CommandInfo.hwnd := Handle;
        CommandInfo.lpVerb := MakeIntResource(Cmd - 1);
        CommandInfo.nShow := SW_SHOWNORMAL;
        Result := Succeeded(ContextMenu.InvokeCommand(CommandInfo));
      end;
      if CallbackWindow <> 0 then
        DestroyWindow(CallbackWindow);
    end;
    DestroyMenu(Menu);
  end;
  end;
end;

function PathAddSeparator(const Path: string): string;
begin
  Result := Path;
  if (Length(Path) = 0) or (AnsiLastChar(Path) <> ShenPathSeparator) then
  Result := Path + ShenPathSeparator;
end;

关于JDBC连接数据库的常用驱动名和URL格式汇总
  内容提示:本文搜集了一些常用的JDBC连接数据库的URL和驱动名,供大家参考,希望能在实际开发中有所帮助:1、使用JTDS第三方驱…
  • 好学触屏公众号虎力全开、杨帆起航!
  • 四大名著全套小学生版注音版
  • 诗词中的科学全4册
  • 曹文轩系列儿童文学全套画本