for i:=0 to FileNumber-1 do begin
DragQueryFile(StgMedium.hGlobal
i
FFileName
SizeOf(FFileName));
FileList.Add(FFileName);
Result := NOERROR;
end;
ReleaseStgMedium(StgMedium);
end;
function TContextMenu.QueryContextMenu(Menu: HMENU; indexMenu
idCmdFirst
idCmdLast
uFlags: UINT): HResult;
begin
Result := 0;
if ((uFlags and $0000000F) = CMF_NORMAL) or
((uFlags and CMF_EXPLORE) <> 0) then begin
// 往Context Menu中加入一个菜单项
InsertMenu(Menu
indexMenu
MF_STRING or MF_BYPOSITION
idCmdFirst
PChar(''执行文件操作''));
// 返回增加菜单项的个数
Result := 1;
end;
end;
function TContextMenu.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult;
var
// sFile:TFileStream;
charSavePath:array[0..1023]of char;
sSaveFile:String;
i:Integer;
F: TextFile;
FirstLine: string;
begin
// 首先确定该过程是被系统而不是被一个程序所调用
if (HiWord(Integer(lpici.lpVerb)) <> 0) then
begin
Result := E_FAIL;
Exit;
end;
// 确定传递的参数的有效性
if (LoWord(lpici.lpVerb) <> 0) then begin
Result := E_INVALIDARG;
Exit;
end;
file://建立一个临时文件保存用户选中的文件名
GetTempPath(1024
charSavePath);
sSaveFile:=charSavePath+''chen0001.tmp'';
AssignFile(F
sSaveFile); { next file in Files property }
ReWrite(F);
file://将文件名保存到临时文件中
for i:= 0 to FileList.Count -1 do begin
FirstLine:=FileList.Strings[i];
Writeln(F
FirstLine); { Read the first line out of the file }
end;
CloseFile(F);
file://调用文件操作程序对用户选中的文件进行操作
ShellExecute(0
nil
''c:\FileOP.exe''
PChar(sSaveFile)
charSavePath
SW_NORMAL);
Result := NOERROR;
end;
function TContextMenu.GetCommandString(idCmd
uType: UINT; pwReserved: PUINT;
pszName: LPSTR; cchMax: UINT): HRESULT;
begin
if (idCmd = 0) then begin
if (uType = GCS_HELPTEXT) then
{返回该菜单项的帮助信息,此帮助信息将在用户把鼠标移动到该菜单项时出现在状态条上。}
StrCopy(pszName
PChar(''点击该菜单项将执行文件操作''));
Result := NOERROR;
end
else
Result := E_INVALIDARG;
end;
type
TContextMenuFactory = class(TComObjectFactory)
public
procedure UpdateRegistry(Register: Boolean); override;
end;
procedure TContextMenuFactory.UpdateRegistry(Register: Boolean);
var
ClassID: string;
begin
if Register then begin
inherited UpdateRegistry(Register);
ClassID := GUIDToString(Class_ContextMenu);
CreateRegKey(''*\shellex''
''''
'''');
CreateRegKey(''*\shellex\ContextMenuHandlers''
''''
'''');
CreateRegKey(''*\shellex\ContextMenuHandlers\OpenWithWordPad''
''''
ClassID);
file://如果操作系统为Windows NT的话
if (Win32Platform = VER_PLATFORM_WIN32_NT) then
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
OpenKey(''SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions''
True);
OpenKey(''Approved''
True);
WriteString(ClassID
''Context Menu Shell Extension'');
finally
Free;
end;
end
else begin
DeleteRegKey(''*\shellex\ContextMenuHandlers\FileOpreation'');
DeleteRegKey(''*\shellex\ContextMenuHandlers'');
// DeleteRegKey(''*\shellex'');
inherited UpdateRegistry(Register);
end;
end;
initialization
TContextMenuFactory.Create(ComServer
TContextMenu
Class_ContextMenu
''''
''Context Menu Shell Extension''
ciMultiInstance
tmApartment);
end.
将该单位文件保存为unit2.pas,文件同contextmenu.dpr位