1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
unit ToolsMenu;
interface
uses
Windows, ActiveX, ComObj, ShlObj;
type
TContextTools = class(TComObject, IShellExtInit, IContextMenu)
private
FFileName : array[0..MAX_PATH] of Char;
MenuIndex1, MenuIndex2, MenuIndex3 : UINT;
function Operation(Owner: HWND; ID:Integer;Filename:String):Byte;
protected
{ IShellExtInit }
function IShellExtInit.Initialize = SEIInitialize;
function SEIInitialize(pidlFolder: PItemIDList; lpdobj: IDataObject;
hKeyProgID: HKEY): HResult; stdcall;
{ IContextMenu }
function QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst, idCmdLast,
uFlags: UINT): HResult; stdcall;
function InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult; stdcall;
function GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
pszName: LPSTR; cchMax: UINT): HResult; stdcall;
end;
const
Class_ContextMenu: TGUID = '{F304F362-97F4-11D5-B5D5-006097954E0E}' ;
implementation
uses ComServ, SysUtils, ShellApi, Registry,NameBak,ToolsFichier;
function TContextTools.Operation(Owner: HWND; ID:Integer;Filename:String):Byte;
var PathName,
BackupName,
Extension : String;
sr : TSearchRec;
ShFileOpStruct : TShFileOpStruct;
begin
PathName := ExtractFilePath(FileName);
if PathName='' then
begin
PathName:=ExpandFileName(Filename);
Filename:=PathName; // complete le nom de fichier avec le nom du directory
end;
if FindFirst(Filename, faAnyFile-fadirectory, sr)=0
// retrouve le nom complet si c'est un nom court
then Filename := ExtractFilePath(PathName)+sr.Name;
FindClose(sr);
FileName:= AddNullToStr(FileName);
BackupName := ExtractFileName(FileName);
if GetLastBackupName(Extension,FileName)
then
begin
BackupName := ChangeFileExt(BackupName,Extension );
BackupName:=ExtractFilePath(FileName)+Backupname;
BackupName:= AddNullToStr(BackupName);
if FileExists(Filename) then
begin
{I+}
FillChar(ShFileOpStruct, SizeOf(ShFileOpStruct), 0);
with ShFileOpStruct do
begin
Wnd:=Owner;
pFrom:=PChar(Filename);
pTo:=PChar(BackupName);
end;
case ID OF
ID_BAKNAME : begin
with ShFileOpStruct do
begin
wFunc:=FO_RENAME;
lpszProgressTitle:=PChar('Renommer');
fFlags:= FOF_ALLOWUNDO+FOF_FILESONLY;
// FOF_NOCONFIRMATION gére les acces interdit
end;
{erreur sur cd rom ou disquette retirer aprés sélection du menu}
if FileOperation(ShFileOpStruct)=0
then Result:=lSUCCESS
else Result:=lNORENAME;
end;
ID_COPYBAK :begin
with ShFileOpStruct do
begin
wFunc:=FO_COPY;
lpszProgressTitle:=PChar('Copier');
fFlags:= FOF_ALLOWUNDO+FOF_FILESONLY+FOF_SIMPLEPROGRESS;
end;
if FileOperation(ShFileOpStruct)=0
then Result:=lSUCCESS
else Result:=lNOCOPY;
end;
end {case}
end
{I-}
else Result:=lNOFILE; // not exist
end
// error in bakname Reade Directory
else Result:=lREAD_DIRECTORY_ERROR;
end;
function TContextTools.SEIInitialize(pidlFolder: PItemIDList; lpdobj: IDataObject;
hKeyProgID: HKEY): HResult;
var
StgMedium: TStgMedium;
FormatEtc: TFormatEtc;
begin
// Fail the call if lpdobj is Nil.
if (lpdobj = nil) then begin
Result := E_INVALIDARG;
Exit;
end;
with FormatEtc do begin
cfFormat := CF_HDROP;
ptd := nil;
dwAspect := DVASPECT_CONTENT;
lindex := -1;
tymed := TYMED_HGLOBAL;
end;
// Render the data referenced by the IDataObject pointer to an HGLOBAL
// storage medium in CF_HDROP format.
Result := lpdobj.GetData(FormatEtc, StgMedium);
if Failed(Result)
then Exit;
// If only one file is selected, retrieve the file name and store it in
// FDirectoryName. Otherwise fail the call.
if (DragQueryFile(StgMedium.hGlobal, $FFFFFFFF, nil, 0) = 1) then begin
DragQueryFile(StgMedium.hGlobal, 0, FFileName, SizeOf(FFileName));
Result := NOERROR;
end
else begin
FFileName[0] := #0;
Result := E_FAIL;
end;
ReleaseStgMedium(StgMedium);
end;
function TContextTools.QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst,
idCmdLast, uFlags: UINT): HResult;
Const
Caption:String='Modifier l''extension';
var
Command_ID,
hSub_Menu : HMenu;
MenuInfo : TMenuItemInfo;
begin
Result:= 0;
if ((uFlags and $0000000F) = CMF_NORMAL) or
((uFlags and CMF_EXPLORE) <> 0) then
begin
MenuIndex1 := indexMenu; // index du sous menu
Command_ID := idCmdFirst;
MenuInfo.cbSize := SizeOf(MenuInfo);
hSub_Menu := CreateMenu;
Inc(Command_ID);
MenuIndex2 := MenuIndex1+1; // index de l'entrée du sous menu
InsertMenu(hSub_Menu,MenuIndex2,MF_BYPOSITION or MF_STRING,Command_ID,
'Copier et renommer en 001');
Inc(Command_ID);
MenuIndex3 := MenuIndex2+1;
InsertMenu(hSub_Menu,MenuIndex3,MF_BYPOSITION or MF_STRING,Command_ID,'Renommer en 001');
{
si d'autre entrées :
Inc(Command_ID);
MenuIndex4 := MenuIndex3+1;
InsertMenu(hSub_Menu,MenuIndex4,MF_BYPOSITION or MF_STRING,Command_ID,'Third Memu Itrm');
}
With MenuInfo do
begin
fMask := MIIM_ID or MIIM_SUBMENU or MIIM_TYPE;
fType := MFT_STRING;
fState := 0;
dwTypeData :=Pchar(Caption);
cch := Length(Caption); // 20 :longueur de chaine;
hSubMenu := hSub_Menu;
wID := idCmdFirst;
hbmpChecked := 0;
hbmpUnchecked := 0;
end;
InsertMenuItem(Menu,MenuIndex1,True,MenuInfo);
Result := 3{ 1 sous-Menu +2 entrées};
end;
end;
function TContextTools.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult;
var
lResult : Byte;
FileName,
MessageString : String;
lMenuIndex : Integer;
begin
Result := E_FAIL;
lResult:=lSuccess;
MessageString:='';
lMenuIndex := Integer(lpici.lpVerb);
// Make sure we are not being called by an application
if HiWord(lMenuIndex) <> 0 then
begin
Exit;
end;
Result := NOERROR;
Filename:= string(FFileName);
if LoWord(lMenuIndex) = MenuIndex2
then
begin
lResult:=Operation(lpici.hWnd,ID_COPYBAK,Filename);
if lResult<>lSUCCESS
then Result := E_FAIL;
end
else
if LoWord(lMenuIndex) = MenuIndex3
then
begin
Result:=Operation(lpici.hWnd,ID_BAKNAME,Filename);
if lResult<>lSUCCESS
then Result := E_FAIL;
end
else
begin
Result := E_INVALIDARG;
Exit;
end;
If lResult= lNOFILE
then MessageString:='Fichier introuvable.';
If lResult= lREAD_DIRECTORY_ERROR
then MessageString:='Erreur de lecture du répertoire.';
if MessageString<>''
then MessageBox(lpici.hWnd, Pchar(MessageString),'Erreur',MB_ICONERROR or MB_OK)
end;
function TContextTools.GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
pszName: LPSTR; cchMax: UINT): HRESULT;
const
HelpStringTemplate = '%s un fichier en modifiant son extension (.001)';
var
HelpString : String;
begin
Result := NOERROR;
HelpString := '';
// le Sous menu n'a pas de contexte d'aide
// Determine the help string
if idCmd=MenuIndex2
then
if (uType = GCS_HELPTEXT)
then HelpString := Format(HelpStringTemplate, ['Copie et renomme'])
else
else
if idCmd=MenuIndex3
then
if (uType = GCS_HELPTEXT)
then HelpString := Format(HelpStringTemplate, ['Renomme'])
else
else
Result := E_INVALIDARG;
strCopy(pszName, PChar(HelpString) );
end;
type
TContextToolsFactory = class(TComObjectFactory)
public
procedure UpdateRegistry(Register: Boolean); override;
end;
procedure TContextToolsFactory.UpdateRegistry(Register: Boolean);
var
ClassID: string;
begin
if Register then begin
inherited UpdateRegistry(Register);
ClassID := GUIDToString(Class_ContextMenu);
{HKEY_CLASSES_ROOT;}
CreateRegKey('*\shellex', '', '');
CreateRegKey('*\shellex\ContextMenuHandlers', '', '');
CreateRegKey('*\shellex\ContextMenuHandlers\ToolsMenu', '', ClassID);
(*{if (Win32Platform = VER_PLATFORM_WIN32s {delphi4_NT}) then*)
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions', True);
OpenKey('Approved', True);
WriteString(ClassID, 'Copie ou renomme un fichier avec l''extension .001');
finally
Free;
end;
end
else begin
DeleteRegKey('*\shellex\ContextMenuHandlers\ToolsMenu');
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
OpenKey('Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved',False);
Deletevalue(ClassID);
finally
Free;
end;
inherited UpdateRegistry(Register);
end;
end;
initialization
TContextToolsFactory.Create(ComServer, TContextTools, Class_ContextMenu,
'', 'Tools', ciMultiInstance,tmApartment);
end. |
Partager