Appending Desktop Menu for Application

Can I add a menuItem to the popup menu for an application(when
you right-click on the application on the desktop)  I've seen it done
with other applications.  I am using Delphi Desktop 2.0.
StomperAsked:
Who is Participating?
 
LilitaNCommented:
Sources for example

Contmenu.dpr
// You need to merge the ContMenu.reg file into the registry before you can use
// shell extension.   This Context Menu shell extension will allow you to right
// click on DPR files from the Explorer and Compile them using the DCC32.exe
// command line compiler.

library contmenu;

uses
  SysUtils,
  Classes,
  ComServ,
  ComObj,
  ContextM in 'ContextM.pas',
  ShlObj in 'Shlobj.pas';

exports
  DllGetClassObject resident,
  DllCanUnloadNow resident,
  DllRegisterServer resident,
  DllUnregisterServer resident;

begin
end.


unit ContextM;


interface
uses
    Windows, ComObj, ComServ, ShlObj, ActiveX, ShellApi, SysUtils, Registry;

Const
   CLSID_ContextMenuShellExtension: TGUID = (
    D1:$8e3e0f0a; D2:$0fcc; D3:$11ce; D4:($bc, $b0, $b3, $fd, $0e, $25, $38, $1a));

type
    TContextMenu = class(TComObject, IShellExtInit, IContextMenu)
    private
      szFile: array[0..MAX_PATH] of Char;
    public
      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 Initialize(pidlFolder: PItemIDList; lpdobj: IDataObject;
        hKeyProgID: HKEY): HResult; stdcall;
    end;



implementation

function GetCompilerPath: string;
// Returns string containing path to Delphi command line compiler
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    with Reg do
    begin
      RootKey := HKEY_LOCAL_MACHINE;
      OpenKey('\SOFTWARE\Borland\Delphi\3.0', False);
      Result := ReadString('RootDir');
    end;
    Result := Result + '\bin\dcc32.exe %s';
  finally
    Reg.Free;
  end;
end;

function TContextMenu.QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst,
          idCmdLast, uFlags: UINT): HResult;
begin
  // Add one menu item to context menu
  InsertMenu (Menu, indexMenu, MF_STRING or MF_BYPOSITION, idCmdFirst,
    'Compile...');
  // Return number of menu items added
  Result := 1;
end;

function TContextMenu.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult;
var
  H: THandle;
begin
  // Make sure we are not being called by an application
  if HiWord(Integer(lpici.lpVerb)) <> 0 then
  begin
    Result := E_FAIL;
    Exit;
  end;
  // Make sure we aren't being passed an invalid argument number
  if LoWord(lpici.lpVerb) > 0 then
  begin
    Result := E_INVALIDARG;
    Exit;
  end;
  // Execute the command specified by lpici.lpVerb.
  if LoWord(lpici.lpVerb) = 0 then
  begin
    // invoke Delphi command line compiler
    H := WinExec(PChar(Format(GetCompilerPath, [szFile])), lpici.nShow);
    if H < 32 then
      MessageBox(lpici.hWnd, 'Error executing Delphi compiler.', 'Error',
        MB_ICONERROR or MB_OK);
  end;
  Result := NOERROR;
end;

function TContextMenu.GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
  pszName: LPSTR; cchMax: UINT): HRESULT;
begin
  if idCmd = 0 then
  begin
    // return help string for menu item
    strCopy(pszName, 'Compile the selected Delphi project');
    Result := NOERROR;
  end
  else
    Result := E_INVALIDARG;
end;

function TContextMenu.Initialize(pidlFolder: PItemIDList; 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.hGlobal, $FFFFFFFF, Nil, 0) = 1 then
  begin
    DragQueryFile(medium.hGlobal, 0, szFile, SizeOf(szFile));
    Result := NOERROR;
  end
  else
    Result := E_FAIL;
  ReleaseStgMedium(medium);
end;

initialization
    TComObjectFactory.Create(ComServer, TContextMenu, CLSID_ContextMenuShellExtension,
         '', 'Delphi 3.0 ContextMenu Example', ciMultiInstance);

end.

Contmenu.reg
REGEDIT4

[HKEY_CLASSES_ROOT\CLSID\{8e3e0f0a-0fcc-11ce-bcb0-b3fd0e25381a}]
@= "Delphi 3.0 Context Menu Shell Extension"
[HKEY_CLASSES_ROOT\CLSID\{8e3e0f0a-0fcc-11ce-bcb0-b3fd0e25381a}\InProcServer32]
@= "contmenu.dll"
"ThreadingModel" = "Apartment"

[HKEY_CLASSES_ROOT\DelphiProject\shellex\ContextMenuHandlers\{8e3e0f0a-0fcc-11ce-bcb0-b3fd0e25381a}]
@= ""

0
 
d003303Commented:
Do you mean for an application or a shortcut to an application ?
0
 
ZifNabCommented:
You have to add a shortcut to your program in the registery  
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
MatveyCommented:
I think Stomper means the window system menu (at the top left corner). The same menue is displayed also when right-clicking the application's button on task bar.
-I'm very curious too... :)
0
 
ZifNabCommented:
Mmm, I think he means this :

When you are on your desktop and click on the Right mouse button, you get a popup menu. Well, I checked this out once and in my opinion you can easely add your program to it, by changing a registry entry. If you want that I search this entry, just give a sign and I'll try to do my best. But maybe there is an other way (without changing the registry, but I doubt that).

Zif.
0
 
JaccoCommented:
Normally the exe's are not on your desktop. Only shortcuts.

Maybe Stomper still wants something else:

Maybe his application works with files of a certain extension, and he wants to add an item to the popup when the user right clicks a file of that type.

That should be easy to accomplish by adding a file type to your browsers registry. (This can be done without the registry-editor). From the options menu in the browser choose file-type add)

Let me know if that is what you're looking for.

Regards Jacco

Zif-d003303-Matvey: Sometimes a question can be hard to understand.
0
 
GreedyCommented:
I think he is talking about a property sheet...because he talks of right clicking the application's icon on the desktop...

003: don't you have a wizard for this?

0
 
StomperAuthor Commented:
I see I caused quite a discussion.  I hope that I can clearify what I need.  I would like to add a menuitem to the popup menu that you get when you right click on an application either on the desktop or in a window.  

Stomper
0
 
bomeCommented:
I think he wants to add a menu item to explorer's context menu. Is it the menu that contains entries like Send To, Cut, Copy Paste, etc ?
0
 
ZifNabCommented:
yes, bome that's the right name explorer's context menu. I'm talking about the same thing like you do, but your is much clearer.
0
 
MatveyCommented:
Hehehehe... :D
0
 
ZifNabCommented:
Matvey?
0
 
d003303Commented:
OK, so we all know that (s)he wants to have a menut item added to the context menu. Now, this should work for EXE files and for shortcut to EXE files (I think).
The EXE part is easy, the shortcut part a bit more difficult. But let'S first be sure if I misinterpreted the question.

Stomper, so you mean if you right-click on an icon on the desktop that starts an application without any documents in it, or when you right-click on an application icon in Explorer, you want a menu item added ?

Next thing is, what information do you need when this new item will be selected ?

Slash/d003303
0
 
MatveyCommented:
No, no ;) I don't know what it is, but it's just funny that there are 5-6 very very wild guesses don't you think? ;-)
0
 
ZifNabCommented:
:-) It is really funny indeed, maybe we should make a component which combines all 5-6 guesses and send it to the net. Then we always can refer to this component ;-) No offens to anybody offcourse. ZiF.
0
 
LilitaNCommented:
Look Inprise (Borland) example at DEMOS\Shellext\ContMenu.dpr
0
 
d003303Commented:
Zif, good idea ! Let's make a context menu that does everything that's useful:
Shutting down windows, formatting C:, repair the registry, make coffee, pays your ISP, ... ;-))

I think it's Stompers turn to clarify his needs. But any comments of the experts are welcome, of course. (I gotta get a coffee, first action for that component...)

Slash/d003303
0
 
StomperAuthor Commented:
LilitaN, I can't find that file.

Bome, that is the menu that I want.  I just didn't what to call it.

Stomper
0
 
MatveyCommented:
Just to say the example is only available from D3. I see you're using D2...
0
 
LilitaNCommented:
Sorry
0
 
bomeCommented:
hehe, but you should at least change the CLSID...
bome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.