📄 0517001.htm
字号:
end;
<p>
<p>
function TContextMenu.QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst,
<p>
idCmdLast, uFlags: UINT): HResult;
<p>
begin
<p>
Result := 0;
<p>
if ((uFlags and $0000000F) = CMF_NORMAL) or
<p>
((uFlags and CMF_EXPLORE) <> 0) then begin
<p>
// 往Context Menu中加入一个菜单项
<p>
InsertMenu(Menu, indexMenu, MF_STRING or MF_BYPOSITION, idCmdFirst,
<p>
PChar('用写字板打开 '+ExtractFileName(FFileName)));
<p>
// 返回增加菜单项的个数
<p>
Result := 1;
<p>
end;
<p>
end;
<p>
<p>
function TContextMenu.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult;
<p>
var
<p>
H: THandle; WordpadPath:String; Reg:TRegistry;
<p>
begin
<p>
// Make sure we are not being called by an application
<p>
if (HiWord(Integer(lpici.lpVerb)) <> 0) then
<p>
begin
<p>
Result := E_FAIL;
<p>
Exit;
<p>
end;
<p>
<p>
// Make sure we aren't being passed an invalid argument number
<p>
if (LoWord(lpici.lpVerb) <> 0) then begin
<p>
Result := E_INVALIDARG;
<p>
Exit;
<p>
end;
<p>
<p>
//当用户选定'用写字板打开XXX'时,执行WordPad.exe
<p>
//先获取WordPad.exe的路径
<p>
Reg:=TRegistry.Create;
<p>
Reg.RootKey := HKEY_LOCAL_MACHINE;
<p>
Reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WORDPAD.EXE');
<p>
WordpadPath:=Reg.ReadString('');//注意:两个撇号(')之间无空格
<p>
Reg.Free;
<p>
<p>
H := WinExec(PChar(WordPadPath+' ' +'"'+FfileName+'"'), lpici.nShow);
<p>
//注意:最前一对撇号(')之间为空格,后两对撇号中为双引号。
<p>
if (H < 32) then
<p>
MessageBox(lpici.hWnd, '执行写字板时出错!', '错误', MB_ICONERROR+MB_OK);
<p>
Result := NOERROR;
<p>
end;
<p>
<p>
function TContextMenu.GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
<p>
pszName: LPSTR; cchMax: UINT): HRESULT;
<p>
begin
<p>
if (idCmd = 0) then begin
<p>
if (uType = GCS_HELPTEXT) then
<p>
{返回该菜单项的帮助信息,此帮助信息将在用户把鼠标移动到该菜单项时出现在状态条上。}
<p>
StrCopy(pszName, PChar('用写字板打开'+FFileName));
<p>
Result := NOERROR;
<p>
end
<p>
else
<p>
Result := E_INVALIDARG;
<p>
end;
<p>
<p>
type
<p>
TContextMenuFactory = class(TComObjectFactory)
<p>
public
<p>
procedure UpdateRegistry(Register: Boolean); override;
<p>
end;
<p>
<p>
procedure TContextMenuFactory.UpdateRegistry(Register: Boolean);
<p>
var
<p>
ClassID: string;
<p>
begin
<p>
if Register then begin
<p>
inherited UpdateRegistry(Register);
<p>
ClassID := GUIDToString(Class_ContextMenu);
<p>
CreateRegKey('*\shellex', '', '');
<p>
CreateRegKey('*\shellex\ContextMenuHandlers', '', '');
<p>
CreateRegKey('*\shellex\ContextMenuHandlers\OpenWithWordPad', '', ClassID);
<p>
//如果操作系统为Windows NT的话
<p>
if (Win32Platform = VER_PLATFORM_WIN32_NT) then
<p>
with TRegistry.Create do
<p>
try
<p>
RootKey := HKEY_LOCAL_MACHINE;
<p>
OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions', True);
<p>
OpenKey('Approved', True);
<p>
WriteString(ClassID, 'Context Menu Shell Extension');
<p>
finally
<p>
Free;
<p>
end;
<p>
end
<p>
else begin
<p>
DeleteRegKey('*\shellex\ContextMenuHandlers\OpenWithWordPad');
<p>
DeleteRegKey('*\shellex\ContextMenuHandlers');
<p>
DeleteRegKey('*\shellex');
<p>
inherited UpdateRegistry(Register);
<p>
end;
<p>
end;
<p>
<p>
initialization
<p>
TContextMenuFactory.Create(ComServer, TContextMenu, Class_ContextMenu,
<p>
'', 'Context Menu Shell Extension', ciMultiInstance,
<p>
tmApartment);
<p>
end.
<p>
<p>
工程文件(*.DPR)的内容如下:
<p>
// This COM server defines a Context Menu shell extension.
<p>
library ContextMenu;
<p>
uses
<p>
ComServ,
<p>
ContextMenuHandler in 'ContextMenuHandler.pas',
<p>
contmenu_TLB in 'contmenu_TLB.pas';
<p>
exports
<p>
DllGetClassObject,
<p>
DllCanUnloadNow,
<p>
DllRegisterServer,
<p>
DllUnregisterServer;
<p>
{$R *.TLB}
<p>
{$R *.RES}
<p>
begin
<p>
end.
</small>
</td>
</tr>
<tr>
<td width="100%" height="12" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="6" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="8" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="17" class="font" colspan="2"></td>
</tr>
</table>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -