chassteel
asked on
How can I add a command to the context menu?
Hi
I need a procedure to add a command to the right click context menu whenever any file is right clicked. I've searched the topics and found out how to add to the context menu for a particular file extension, but I can't figure out how to have something appear for all files right clicked.
Thanks in advance.
Charles
I need a procedure to add a command to the right click context menu whenever any file is right clicked. I've searched the topics and found out how to add to the context menu for a particular file extension, but I can't figure out how to have something appear for all files right clicked.
Thanks in advance.
Charles
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And also, you might want to take a look at Borland's example, found in your delphi folder.
C:\Program Files\Borland\Delphi7\Demo s\ActiveX\ ShellExt
check out "qregister" - no VCL, small size (~45 kb DLL)
C:\Program Files\Borland\Delphi7\Demo
check out "qregister" - no VCL, small size (~45 kb DLL)
hello chassteel, , To add to the context menu like you said in your question, I would think you would use an ActiveX Library with the IContextMemu interface in it. . . This is really not like any of the usuall delphi code methods, and if you have not done an Active X ligrary before, can be confusing even with the code presented. . . I did a question at -
https://www.experts-exchange.com/questions/21518834/400-points-for-create-Context-menu-like-Winzip.html
which give some step by step instructions for this. . .
ask questions if you need more information
https://www.experts-exchange.com/questions/21518834/400-points-for-create-Context-menu-like-Winzip.html
which give some step by step instructions for this. . .
ask questions if you need more information
(* code by Colin Pringle *)
// Open Delphi select dynamic link library
// Copy / paste this into the DLL
// Then compile
// You will have to customize this code. To suite your needs.
// once the dll has been compiled you will now have to register this
// com server.
// Use regsvr32.exe sendtoweb.dll
// now open windows explorer and you will see a new menu item
// which can be accessed by the desktop also..
unit Sendtoweb;
// Author C Pringle Cjpsoftware.com
{ Implementation of the context menu shell extension COM object. This
COM object is responsible for forwarding requests to its partner
TPopupMenu component. The TPopupMenu component must reside on the
MenuComponentForm, and is referred to explicitly in this example.
You can modify this code to make it more flexible and generic in
the future.
The TContextMenu component registers itself as a global context menu
handler. This is accomplished by adding a key to the
HKEY_CLASSES_ROOT\*\ShellE
jfl
}
interface
uses
Classes, ComServ, ComObj, ActiveX, Windows, ShlObj, Interfaces, Menus,
ShellAPI, SysUtils,registry;
type
TContextMenuFactory = class( TComObjectFactory )
public
procedure UpdateRegistry( Register: Boolean ); override;
end;
TContextMenu = class( TComObject, IShellExtInit, IContextMenu )
private
FFileName: String;
function BuildSubMenu( Menu: HMENU; IndexMenu: Integer;
var IDCmdFirst: Integer ): HMENU;
protected
szFile: array[0..MAX_PATH] of Char;
// Required to disambiguate TComObject.Initialize otherwise a compiler
// warning will result.
function IShellExtInit.Initialize = IShellExtInit_Initialize;
public
{ IShellExtInit members }
function IShellExtInit_Initialize(p
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;
var
// Must be set prior to instantiation of TContextMenu!
GFileExtensions: TStringList;
const
MenuCommandStrings: array[ 0..3 ] of String = (
'','&STW Web Upload','&STW FTPClient','&STW Setup'
);
implementation
{ TContextMenuFactory }
{ Public }
Function ReadDefaultPAth: String;
var
path : String;
Reg : TRegistry;
begin
Reg := TRegistry.CReate;
try
With Reg Do
Begin
RootKey := HKEY_LOCAL_MACHINE;
Path := 'SOFTWARE\Microsoft\Window
If KeyExists(Path) Then
Begin
OpenKey(Path+'\sendtoweb.e
Result := ReadString(#0);
closekey;
End;
// Key Added to shell ext.
End;
Finally
Reg.CloseKey;
Reg.Free;
End;
End;// Custom registration code
procedure TContextMenuFactory.Update
begin
inherited UpdateRegistry( Register );
// Register our global context menu handler
if Register then
begin
CreateRegKey( '*\ShellEx\ContextMenuHand
GUIDToString( Class_ContextMenu ) );
CreateRegKey( 'CLSID\' + GUIDToString( ClassID ) + '\' +
ComServer.ServerKey, 'ThreadingModel', 'Apartment' );
end else
begin
DeleteRegKey( '*\ShellEx\ContextMenuHand
end;
end;
{ TContextMenu }
{ Private }
{ Build a context menu using the existing Menu handle. If Menu is nil,
we create a new menu handle and return it in the function's return
value. Note that this function does not handle nested (recursive)
menus. This exercise is left to the reader. }
function TContextMenu.BuildSubMenu(
var IDCmdFirst: Integer ): HMENU;
var
i: Integer;
menuItemInfo: TMenuItemInfo;
begin
if Menu = 0 then
Result := CreateMenu
else
Result := Menu;
// Build the menu items here
with menuitemInfo do
begin
cbSize := SizeOf( TMenuItemInfo );
fMask := MIIM_CHECKMARKS or MIIM_DATA or MIIM_ID or
MIIM_STATE or MIIM_SUBMENU or MIIM_TYPE or MIIM_CHECKMARKS ;
fType := MFT_STRING;
fState := MFS_ENABLED ;
hSubMenu := 0;
hbmpChecked := 0;
hbmpUnchecked := 0;
end;
for i := 0 to High( MenuCommandStrings ) do
begin
if i = 0 then
menuitemInfo.fType := MFT_SEPARATOR
else
menuiteminfo.ftype := MFT_String;
if i = 1 then
menuitemInfo.fstate := MFS_ENABLED OR MFS_DEFAULT
Else
menuitemInfo.fstate := MFS_ENABLED;
menuitemInfo.dwTypeData := PChar(MenuCommandStrings[ i ]);
menuitemInfo.wID := IDCmdFirst;
InsertMenuItem( Result, IndexMenu + i, True, menuItemInfo );
Inc( IDCmdFirst );
end;
end;
{ IShellExtInit }
function TContextMenu.IShellExtInit
lpdobj: IDataObject; hKeyProgID: HKEY ): HResult;
var
medium: TStgMedium;
fe: TFormatEtc;
begin
with fe do
begin
cfFormat := CF_HDROP;
ptd := Nil;
dwAspect := DVASPECT_CONTENT;
lindex := -1;
tymed := TYMED_HGLOBAL;
end;
// Fail the call if lpdobj is Nil.
if lpdobj = Nil then
begin
Result := E_FAIL;
Exit;
end;
// Render the data referenced by the IDataObject pointer to an HGLOBAL
// storage medium in CF_HDROP format.
Result := lpdobj.GetData(fe, medium);
if Failed(Result) then Exit;
// If only one file is selected, retrieve the file name and store it in
// szFile. Otherwise fail the call.
if DragQueryFile(medium.hGlob
begin
DragQueryFile(medium.hGlob
Result := NOERROR;
end
else
Result := E_FAIL;
ReleaseStgMedium(medium);
end;
{ IContextMenu }
function TContextMenu.QueryContextM
indexMenu, idCmdFirst, idCmdLast, uFlags: UINT ): HResult;
var
extension: String;
I: Integer;
idLastCommand: Integer;
begin
Result := E_FAIL;
idLastCommand := idCmdFirst;
// Extract the filename extension from the file dropped, and see if we
// have a handler registered for it
// extension := UpperCase( ( FFileName ) );
//for i := 0 to GFileExtensions.Count - 1 do
// if Pos(Lowercase(GFileExtensi
// begin
BuildSubMenu( Menu, indexMenu, idLastCommand );
// Return value is number of items added to context menu
Result := idLastCommand - idCmdFirst;
// Exit;
// end;
end;
function TContextMenu.InvokeCommand
TCMInvokeCommandInfo ): HResult;
var
idCmd: UINT;
begin
if HIWORD( Integer(lpici.lpVerb) ) <> 0 then
Result := E_FAIL
else
begin
idCmd := LOWORD( lpici.lpVerb );
Result := S_OK;
// Activate the Dialog And prepare to send data to the
// web
case idCmd of
1: Begin
ShellExecute( GetDesktopWindow, nil,Pchar(ExtractFileName(
Pchar('Direct'+'"'+szfile+
End;
3:Begin
ShellExecute( GetDesktopWindow, nil,Pchar(ExtractFileName(
Pchar('Path'), nil, SW_SHOW );
End;
2:
ShellExecute( GetDesktopWindow, nil, Pchar(ExtractFileName(Read
PChar(''), nil, SW_SHOW );
else
Result := E_FAIL;
end;
end;
end;
function TContextMenu.GetCommandStr
pwReserved: PUINT; pszName: LPSTR; cchMax: UINT ): HResult;
begin
// StrCopy( pszName, 'Send To The Web') ;
Result := S_OK;
end;
initialization
{ Note that we create an instance of TContextMenuFactory here rather
than TComObjectFactory. This is necessary so that we can add some
custom registry entries by overriding the UpdateRegistry virtual
function. }
TContextMenuFactory.Create
'ContextMenu', 'Send To The Web', ciMultiInstance );
// Initialize the file extension list
GFileExtensions := TStringList.Create;
// GFileExtensions.Add( 'setup msn' );
finalization
GFileExtensions.Free;
end.
//////////////////////////
Good Luck!