unit ToDoMenu;
interface
uses
Windows, ActiveX, ComObj, ShlObj, ShellApi;
type
TToDoMenu = class(TComObject, IUnknown, IContextMenu, IShellExtInit)
private
fFileName: string;
protected
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;
function IShellExtInit.Initialize = InitShellExt;
function InitShellExt (pidlFolder: PItemIDList; lpdobj: IDataObject;
hKeyProgID: HKEY): HResult; stdcall;
end;
TToDoMenuFactory = class (TComObjectFactory)
public
procedure UpdateRegistry (Register: Boolean); override;
end;
const
Class_ToDoMenuMenu: TGUID =
'{CDF05220-DB84-11D1-B9F1-004845400FAA}';
implementation
uses
ComServ, Messages, SysUtils, Registry;
function TToDoMenu.InitShellExt(pidlFolder: PItemIDList;
lpdobj: IDataObject; hKeyProgID: HKEY): HResult; stdcall;
var
medium: TStgMedium;
fe: TFormatEtc;
begin
Result := E_FAIL;
if Assigned (lpdobj) then
begin
with fe do
begin
cfFormat := CF_HDROP;
ptd := nil;
dwAspect := DVASPECT_CONTENT;
lindex := -1;
tymed := TYMED_HGLOBAL;
end;
Result := lpdobj.GetData(fe, medium);
if not Failed (Result) then
begin
if DragQueryFile (medium.hGlobal, $FFFFFFFF, nil, 0) = 1 then
begin
SetLength (fFileName, 1000);
DragQueryFile (medium.hGlobal, 0, PChar (fFileName), 1000);
fFileName := PChar (fFileName);
Result := NOERROR;
end
else
Result := E_FAIL;
end;
ReleaseStgMedium(medium);
end;
end;
function TToDoMenu.QueryContextMenu(Menu: HMENU;
indexMenu, idCmdFirst, idCmdLast, uFlags: UINT): HResult;
begin
if FindWindow ('TToDoFileForm', nil) <> 0 then
begin
InsertMenu (Menu, indexMenu,
MF_STRING or MF_BYPOSITION, idCmdFirst,
'Send to ToDoFile');
Result := 1;
end
else
Result := 0;
end;
function TToDoMenu.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult;
var
hwnd: THandle;
cds: CopyDataStruct;
begin
Result := NOERROR;
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;
if LoWord(lpici.lpVerb) = 0 then
begin
hwnd := FindWindow ('TToDoFileForm', nil);
if hwnd <> 0 then
begin
cds.dwData := 0;
cds.cbData := length (fFileName);
cds.lpData := PChar (fFileName);
SetForegroundWindow (hwnd);
SendMessage (hwnd, wm_CopyData,
lpici.hWnd, Integer (@cds));
end
else
begin
MessageBox(lpici.hWnd,
'FilesToDo Program not found',
'Error',
MB_ICONERROR or MB_OK);
end;
end;
end;
function TToDoMenu.GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
pszName: LPSTR; cchMax: UINT): HRESULT;
begin
if idCmd = 0 then
begin
strCopy (pszName, 'Add file to the ToDoFile database');
Result := NOERROR;
end
else
Result := E_INVALIDARG;
end;
procedure TToDoMenuFactory.UpdateRegistry(Register: Boolean);
var
Reg: TRegistry;
begin
inherited UpdateRegistry (Register);
Reg := TRegistry.Create;
Reg.RootKey := HKEY_CLASSES_ROOT;
try
if Register then
if Reg.OpenKey('\*\ShellEx\ContextMenuHandlers\ToDo', True) then
Reg.WriteString('', GUIDToString(Class_ToDoMenuMenu))
else
if Reg.OpenKey('\*\ShellEx\ContextMenuHandlers\ToDo', False) then
Reg.DeleteKey ('\*\ShellEx\ContextMenuHandlers\ToDo');
finally
Reg.CloseKey;
Reg.Free;
end;
end;
initialization
TToDoMenuFactory.Create (
ComServer, TToDoMenu, Class_ToDoMenuMenu,
'ToDoMenu', 'ToDoMenu Shell Extension',
ciMultiInstance, tmApartment);
end.
|