delphi 调用系统右键菜单

 
  
1 unit PopupMenuShell;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, StrUtils, ComObj, ShlObj, ActiveX;
7
8 function DisplayContextMenu( const Handle: THandle; const FileName: string ; Pos: TPoint): Boolean;
9
10 implementation
11
12 type
13 TUnicodePath = array [ 0 ..MAX_PATH - 1 ] of WideChar;
14
15 const
16 ShenPathSeparator = ' \ ' ;
17
18 Function String2PWideChar( const s: String): PWideChar;
19 begin
20 if s = '' then
21 begin
22 result: = nil ;
23 exit;
24 end ;
25 result: = AllocMem((Length(s) + 1 ) * sizeOf(widechar));
26 StringToWidechar(s, result, Length(s) * sizeOf(widechar) + 1 );
27 end ;
28
29 function PidlFree( var IdList: PItemIdList): Boolean;
30 var
31 Malloc: IMalloc;
32 begin
33 Result : = False;
34 if IdList = nil then
35 Result : = True
36 else
37 begin
38 if Succeeded(SHGetMalloc(Malloc)) and (Malloc.DidAlloc(IdList) > 0 ) then
39 begin
40 Malloc.Free(IdList);
41 IdList : = nil ;
42 Result : = True;
43 end ;
44 end ;
45 end ;
46
47 function MenuCallback(Wnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall ;
48 var
49 ContextMenu2: IContextMenu2;
50 begin
51 case Msg of
52 WM_CREATE:
53 begin
54 ContextMenu2 : = IContextMenu2(PCreateStruct(lParam).lpCreateParams);
55 SetWindowLong(Wnd, GWL_USERDATA, Longint(ContextMenu2));
56 Result : = DefWindowProc(Wnd, Msg, wParam, lParam);
57 end ;
58 WM_INITMENUPOPUP:
59 begin
60 ContextMenu2 : = IContextMenu2(GetWindowLong(Wnd, GWL_USERDATA));
61 ContextMenu2.HandleMenuMsg(Msg, wParam, lParam);
62 Result : = 0 ;
63 end ;
64 WM_DRAWITEM, WM_MEASUREITEM:
65 begin
66 ContextMenu2 : = IContextMenu2(GetWindowLong(Wnd, GWL_USERDATA));
67 ContextMenu2.HandleMenuMsg(Msg, wParam, lParam);
68 Result : = 1 ;
69 end ;
70 else
71 Result : = DefWindowProc(Wnd, Msg, wParam, lParam);
72 end ;
73 end ;
74
75 function CreateMenuCallbackWnd( const ContextMenu: IContextMenu2): HWND;
76 const
77 IcmCallbackWnd = ' ICMCALLBACKWND ' ;
78 var
79 WndClass: TWndClass;
80 begin
81 FillChar(WndClass, SizeOf(WndClass), # 0 );
82 WndClass.lpszClassName : = PChar(IcmCallbackWnd);
83 WndClass.lpfnWndProc : = @MenuCallback;
84 WndClass.hInstance : = HInstance;
85 Windows.RegisterClass(WndClass);
86 Result : = CreateWindow(IcmCallbackWnd, IcmCallbackWnd, WS_POPUPWINDOW, 0 , 0 , 0 , 0 , 0 , 0 , HInstance, Pointer(ContextMenu));
87 end ;
88
89 function DisplayContextMenuPidl( const Handle: HWND; const Folder: IShellFolder; Item: PItemIdList; Pos: TPoint): Boolean;
90 var
91 Cmd: Cardinal;
92 ContextMenu: IContextMenu;
93 ContextMenu2: IContextMenu2;
94 Menu: HMENU;
95 CommandInfo: TCMInvokeCommandInfo;
96 CallbackWindow: HWND;
97 begin
98 Result : = False;
99 if (Item = nil ) or (Folder = nil ) then
100 Exit;
101 Folder.GetUIObjectOf(Handle, 1 , Item, IID_IContextMenu, nil , Pointer(ContextMenu));
102
103 if ContextMenu <> nil then
104 begin
105 Menu : = CreatePopupMenu;
106 if Menu <> 0 then
107 begin
108 if Succeeded(ContextMenu.QueryContextMenu(Menu, 0 , 1 , $7FFF, CMF_EXPLORE)) then
109 begin
110 CallbackWindow : = 0 ;
111
112 if Succeeded(ContextMenu.QueryInterface(IContextMenu2, ContextMenu2)) then
113 CallbackWindow : = CreateMenuCallbackWnd(ContextMenu2);
114
115 ClientToScreen(Handle, Pos);
116 Cmd : = Cardinal(TrackPopupMenu(Menu, TPM_LEFTALIGN or TPM_LEFTBUTTON or
117 TPM_RIGHTBUTTON or TPM_RETURNCMD, Pos.X, Pos.Y, 0 , CallbackWindow,
118 nil ));
119
120 if Cmd <> 0 then
121 begin
122 FillChar(CommandInfo, SizeOf(CommandInfo), # 0 );
123 CommandInfo.cbSize : = SizeOf(TCMInvokeCommandInfo);
124 CommandInfo.hwnd : = Handle;
125 CommandInfo.lpVerb : = MakeIntResource(Cmd - 1 );
126 CommandInfo.nShow : = SW_SHOWNORMAL;
127 Result : = Succeeded(ContextMenu.InvokeCommand(CommandInfo));
128 end ;
129
130 if CallbackWindow <> 0 then
131 DestroyWindow(CallbackWindow);
132 end ;
133
134 DestroyMenu(Menu);
135 end ;
136 end ;
137 end ;
138
139 function PathAddSeparator( const Path: string ): string ;
140 begin
141 Result : = Path;
142 if (Length(Path) = 0 ) or (AnsiLastChar(Path) <> ShenPathSeparator) then
143 Result : = Path + ShenPathSeparator;
144 end ;
145
146 function DriveToPidlBind( const DriveName: string ; out Folder: IShellFolder):
147 PItemIdList;
148 var
149 Attr: ULONG;
150 Eaten: ULONG;
151 DesktopFolder: IShellFolder;
152 Drives: PItemIdList;
153 Path: TUnicodePath;
154 begin
155 Result : = nil ;
156 if Succeeded(SHGetDesktopFolder(DesktopFolder)) then
157 begin
158 if Succeeded(SHGetSpecialFolderLocation( 0 , CSIDL_DRIVES, Drives)) then
159 begin
160 if Succeeded(DesktopFolder.BindToObject(Drives, nil , IID_IShellFolder, Pointer(Folder))) then
161 begin
162 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PChar(PathAddSeparator(DriveName)), - 1 , Path, MAX_PATH);
163
164 if Failed(Folder.ParseDisplayName( 0 , nil , Path, Eaten, Result, Attr)) then
165 Folder : = nil ;
166 end ;
167 end ;
168 PidlFree(Drives);
169 end ;
170 end ;
171
172 function PathToPidlBind( const FileName: string ; out Folder: IShellFolder): PItemIdList;
173 var
174 Attr, Eaten: ULONG;
175 PathIdList: PItemIdList;
176 DesktopFolder: IShellFolder;
177 Path, ItemName: pwidechar;
178 s1,s2: string ;
179 begin
180 Result : = nil ;
181
182 s1: = ExtractFilePath(FileName);
183 s2: = ExtractFileName(FileName);
184 Path: = String2PWideChar(s1);
185 ItemName: = String2PWideChar(s2);
186
187 if Succeeded(SHGetDesktopFolder(DesktopFolder)) then
188 begin
189 if Succeeded(DesktopFolder.ParseDisplayName( 0 , nil , Path, Eaten, PathIdList, Attr)) then
190 begin
191 if Succeeded(DesktopFolder.BindToObject(PathIdList, nil , IID_IShellFolder, Pointer(Folder))) then
192 begin
193 if Failed(Folder.ParseDisplayName( 0 , nil , ItemName, Eaten, Result, Attr)) then
194 begin
195 Folder : = nil ;
196 Result : = DriveToPidlBind(FileName, Folder);
197 end ;
198 end ;
199 PidlFree(PathIdList);
200 end
201 else
202 Result : = DriveToPidlBind(FileName, Folder);
203 end ;
204
205 FreeMem(Path);
206 FreeMem(ItemName);
207 end ;
208
209 function DisplayContextMenu( const Handle: Thandle; const FileName: string ; Pos: TPoint): Boolean;
210 var
211 ItemIdList: PItemIdList;
212 Folder: IShellFolder;
213 begin
214 Result : = False;
215 ItemIdList : = PathToPidlBind(FileName, Folder);
216
217 if ItemIdList <> nil then
218 begin
219 Result : = DisplayContextMenuPidl(Handle, Folder, ItemIdList, Pos);
220 PidlFree(ItemIdList);
221 end ;
222 end ;
223
224 end .

转载于:https://www.cnblogs.com/Lucky2011/archive/2011/04/08/2009486.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目 录 前言 第一部分 Delphi编程基础 第1章 Delphi集成开发环境 1 1.1 集成开发环境简介 1 1.2 集成调试器 5 1.3 开发环境的优化 6 1.3.1 自定义工具栏 6 1.3.2 编程环境设置 7 1.3.3 编辑环境设置 8 1.3.4 工程设置 8 1.4 方法与技巧 9 1.4.1 使用代码浏览器 9 1.4.2 使用代码编辑器 9 1.4.3 使用帮助系统 10 1.4.4 设置IDE桌面 11 第2章 对象Pascal语言 12 2.1 学习对象Pascal语言的一个通用 例程 12 2.2 对象Pascal语言基础 14 2.2.1 标识符 14 2.2.2 保留字和指令字 14 2.2.3 注释 15 2.2.4 数据类型 16 2.2.5 运算符 21 2.2.6 语句 24 2.2.7 过程与函数 27 2.3 对象Pascal语言的关键技术 30 2.3.1 对象和类 31 2.3.2 类的封装与继承 31 2.3.3 构造函数和析构函数 32 2.3.4 多态性 35 2.3.5 抽象类 38 2.3.6 运行时类型信息 39 2.3.7 类方法 41 2.3.8 类引用 42 2.3.9 单元文件与工程文件 43 2.4 异常处理 45 2.4.1 raise语句 45 2.4.2 try...except语句 45 2.4.3 try...finally语句 47 2.5 方法与技巧 48 2.5.1 命名规则 48 2.5.2 在编译时自由设置是否获得提示 信息 48 2.5.3 使用代码模板 49 2.5.4 使用动态多维数组 49 2.5.5 定义两个相互包含的类 50 2.5.6 获取和使用命令行参数 51 2.5.7 引用参数传递 51 第二部分 Delphi编程的核心技能 第3章 窗体和组件 53 3.1 窗体和组件 53 3.1.1 窗体 54 3.1.2 组件 56 3.1.3 在窗体中使用组件 57 3.2 文本组件 60 3.3 特殊输入组件 61 3.4 按钮及其分类组件 63 3.5 列表组件 64 3.6 分组组件 67 3.7 信息反馈组件 69 3.8 表格显示组件 70 3.9 图形显示组件 71 3.10 开发MDI应用程序 73 3.11 常用组件的用法 74 3.12 方法与技巧 84 3.12.1 窗体、组件的使用原则 84 3.12.2 使用信息对话框 85 3.12.3 使用组件的Hint属性 88 3.12.4 使用Edit组件的IME属性 89 3.12.5 将系统字体添加到ComboBox 组件中 89 3.12.6 在RichEdit组件中存取文件 90 3.12.7 设置模态对话框的返回值 90 3.12.8 使用事件处理过程中的Sender 参数 91 3.12.9 为控件生成多行提示信息 91 3.12.10 生成非矩形窗口 92 3.12.11 移动无标题栏窗口 94 3.12.12 制作动态字幕 95 3.12.13 在窗体上动态地设置背景 画面 96 3.12.14 Owner和Parent的区别 97 第4章 菜单、工具栏和状态栏 98 4.1 菜单 98 4.1.1 主菜单 99 4.1.2 鼠标右键弹出式菜单 99 4.1.3 使用菜单模板 99 4.2 工具栏 100 4.2.1 ToolBar 100 4.2.2 CoolBar 100 4.2.3 ControlBar 101 4.3 状态栏 101 4.4 方法与技巧 106 4.4.1 动态创建菜单 106 4.4.2 为菜单动态定义快捷键 108 4.4.3 动态改变菜单 108 4.4.4 定制系统菜单 109 4.4.5 制作图形菜单项 110 4.4.6 在状态栏中添加进度条等其他 组件 111 4.4.7 MDI应用程序中的菜单融合 113 4.4.8 制作可随处拖放的工具栏 116 4.4.9 将菜单项移到菜单栏的最右边 117 4.4.10 运行时控件的移动 117 第5章 键盘和鼠标 119 5.1 键盘事件 119 5.2 鼠标事件 120 5.2.1 常用鼠标事件 120 5.2.2 拖曳事件 120 5.3
制作固定大小的Form   固定的Form像一个对话框,何不试试下面的语句   巧用Delphi制作溅射屏幕   精心编写的WINDOWS程序显示启动注意事项,称之为溅射屏幕(splash screen)。利用一点儿小小的内容,即可给程序的显示添加不少色彩   LED数码管仿真显示程序   在电子设备上广泛地使用LED数码管显示数据,在许多应用软件中也经常模拟LED数码管显示数据,使程序画面看起来很有特色   菜单设计   DELPHI中自适应表单的实现   我们知道,屏幕分辨率的设置影响着表单布局,假设你的机器上屏幕分辨率是800*600,而最终要分发应用的机器分辨率为640*480,或1024*768,这样你原先设计的表单在新机器上势必会走样   作非常规程序菜单掌握delphi高级秘籍   大家可能见过诸如金山毒霸,瑞星杀毒,以及五笔输入法等等在系统托盘(即右下角有时间和输入法图标的地方)在的控制菜单,而在正常的任务栏(即屏幕最下方的“开始”按钮的右边的各式各样)中却不出现按钮的程序,即我们常说的在后台运行的程序   用Delphi制作动态菜单   所谓动态菜单是指菜单项随着程序的操作变化而变化。现在,我们用Delphi来实现这一功能,具体步骤如下   工具栏和状态条   为Windows窗口标题栏添加新按钮   对于我们熟悉的标准windows窗口来讲,标题栏上一般包含有3个按钮,即最大化按钮,最小化按钮和关闭按钮。你想不想在Windows的窗口标题栏上添加一个新的自定义按钮   用Delphi4实现风Word97格的工具栏   用过Word97的人对它的工具栏印象很深刻,因为它的风格很“酷”,同样IE4.0的工具栏也有类似的风格,Win98的出现,使这种风格的工具栏得到了推广   如何隐藏和显示Windows的任务条   如果隐藏和显示Windows的任务条?仅仅调用以下的函数就可以.   其他技巧   Delphi利用Windows GDI实现文字倾斜   在Delphi开发环境中,文字的输出效果一般都是头上脚下的"正统"字符,如何输出带有一定倾斜角度的文字以达到特殊的显示效果呢   Delphi之三十六计之界面篇   设置状态栏面板对象的Style为OwnerDraw,并在状态栏对象的DrawPanel事件中书写以下代码   利用COM技术实现外壳扩展的属性页   当用户在资源管理器中调用右键菜单时,会显示一个"属性"菜单项,点击属性菜单项会显示一个属性页,用户可以获得甚至修改文件信息

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值