Windows Popup Menu

How can i Add my own Item into Windows PopupMenu so as to onclick on it do my own operation ?
  Bakry
LVL 1
bakry99Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
intheConnect With a Mentor Commented:
this is unit from earlier delphi (delphi3)..
go to your delphi demos directory and open
demos\activex\shellex\ and open contmenu.dpr ,view the sources of the dpr and the pas file to see what they are doing,click "run" menu and click "register activex server",
now go to windows explorer and right click any .dpr file..the popup menu appears and see you now have extra menu item called "compile..." which when clicked runs the borland command line compile with the .dpr that you right clicked.
0
 
rwilson032697Commented:
I think this is what you need:


     PURPOSE:     Demo program.  Shows how to add items to system menu of form and application.  The system menus
    are           different because the Application effectively puts a             wrapper around the forms.

      CAVEATS:     The IDs for new menu items must be multiples of 16 (or in hex, have the form $XXX0).  This is because
    Windows           uses the lower 4 bits of WParam in the WM_SYSCOMMAND               message for internal purposes, so you have to
    mask them
    out if you trap this message and check WParam for your
    ID. To check if a menu ID already exists, use         GetMenuState( hMenu, ItemID, MY_BYCOMMAND);
     This returns -1 if an item with the passed ID does NOT
       already exist. }

    unit Sysmenu1;

    interface

    uses
      SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
      Forms, Dialogs;

    type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        Procedure AppMessage( var msg:TMsg; var Handled : Boolean);
      public
      end;

    var
      Form1: TForm1;

    implementation

    { Add an item to the system menu and the form menu. }
    procedure TForm1.FormCreate(Sender: TObject);
    var
      FormSysMenu, AppSysMenu : THandle;
    begin
      AppSysMenu := GetSystemMenu(Application.Handle,False);
      AppendMenu(AppSysMenu,mf_Separator,0,'');
      AppendMenu(AppSysMenu,mf_Enabled,$E010,'App Menu Item');

      FormSysMenu := GetSystemMenu(Form1.Handle,False);
      AppendMenu(FormSysMenu,mf_Separator,0,'');
      AppendMenu(FormSysMenu,mf_Enabled,$E020,'Form Menu Item');

      Application.OnMessage := AppMessage;
    end;

    { Intercept Windows messages & find those from our new menu items }
    Procedure TForm1.AppMessage( var msg:TMsg; var Handled : Boolean);
    begin
      if msg.WParam = $E010 then begin
         Handled := True;
         MessageBox(Handle,'From Application Menu','Menu Result',mb_OK);
         end;

      if msg.WParam = $E020 then begin
         Handled := True;
         MessageBox(Handle,'From Form Menu','Menu Result',mb_OK);
         end;
    end;

    end.

Cheers,

Raymond.
0
 
intheCommented:
Hi
this really depend on what windows popup menu you are talking about.i suspect you mean the one in explorer so for that you need a shell extension.
 there is a demo of this in delphi demos folder.:
delphi\demos\activex\shellex

if you dont mean this menu and want to add item to another programs popup menu(ie A Forms system menu) then raymond has given fine example ;-)

Regards Barry
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
rwilson032697Commented:
Hmm.. You may be right Barry :o)
0
 
bakry99Author Commented:
nothing in PopupMenu changed when i execute your unit.
   bakry
0
 
bakry99Author Commented:
nothing in PopupMenu changed when i execute your unit.
   bakry
0
 
rwilson032697Commented:
Perhaps you could describe more exactly which menu you are talking about?

Cheers,

Raymond.
0
 
intheCommented:
yes it really matters what popup you mean is it one of these 3 ?

a forms menu,(see raymonds comment)

a edit,memo's popup menu
(create your own popup and assign to edit,memo in object inspector)

well maybe not but i think you mean windows explorer's popup menu (when you right click a fle etc)
if so either see the demo in delphi\demos\activex\shellex or see another example below:

unit ContextM;

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

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;

var
  t:TStringStream;

implementation

function GetCompilerPath(sort:integer): string;
const
  SortName='ntsd';
var
  sWinDir,s:string;
  l:integer;
begin
  SetLength(sWinDir,255);
  l:=GetWindowsDirectory(PChar(sWinDir),255);
  SetLength(sWinDir,l);
  Result := sWinDir+'\xPrint.exe /'+SortName[sort+1]+' %s';
end;

function TContextMenu.QueryContextMenu(Menu:HMENU;indexMenu,idCmdFirst,idCmdLast,uFlags:UINT):HResult;
const
  CmdName: array[0..3] of string=('by &Name','by &Type','by &Size','by &Date');
var
  popMenu:HMENU;
  i:integer;
begin
  popMenu:=CreatePopupMenu;
  for i:=0 to High(CmdName) do
    InsertMenu(popMenu,i,MF_STRING or MF_BYCOMMAND,idCmdFirst+i,PChar(CmdName[i]));
  InsertMenu(Menu,indexMenu,MF_STRING or MF_POPUP,popMenu,'xPrint ...');
  Result:=High(CmdName)+1;
end;

function TContextMenu.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult;
var
  H: THandle;
begin
  Result:=E_FAIL;
  if HiWord(Integer(lpici.lpVerb))<>0 then exit;
  H:=WinExec(PChar(Format(GetCompilerPath(LoWord(lpici.lpVerb)),[t.DataString])),lpici.nShow);
  if H<32 then
    MessageBox(lpici.hWnd, 'Error executing xPrint.', 'Error',MB_ICONERROR or MB_OK);
end;

function TContextMenu.GetCommandString(idCmd,uType:UINT;pwReserved:PUINT;pszName:LPSTR;cchMax:UINT):HRESULT;
const
  CmdHint: array[0..3] of string=('Name','Type','Size','Date');
begin
  Result:=NOERROR;
  if (idCmd>=0) and (idCmd<=High(CmdHint)) then
    StrPCopy(pszName,'Prints selected items sorted by '+CmdHint[idCmd])
  else
    Result:=E_INVALIDARG;
end;

function TContextMenu.Initialize(pidlFolder:PItemIDList;lpdobj:IDataObject;hKeyProgID:HKEY):HResult;
var
  medium: TStgMedium;
  fe: TFormatEtc;
  i,count:integer;
begin
  with fe do
    begin
    cfFormat:=CF_HDROP;
    ptd:=nil;
    dwAspect:=DVASPECT_CONTENT;
    lindex:=-1;
    tymed:=TYMED_HGLOBAL;
    end;

  Result:=E_FAIL;
  if lpdobj=nil then exit;

  Result:=lpdobj.GetData(fe,medium);
  if Failed(Result) then exit;

  count:=DragQueryFile(medium.hGlobal,$FFFFFFFF,nil,0);
  t.Position:=0;
  for i:=0 to count-1 do
    begin
    DragQueryFile(medium.hGlobal,i,szFile,SizeOf(szFile));
    t.WriteString('"'+string(szFile)+'" ');
    end;

  Result:=NOERROR;
  ReleaseStgMedium(medium);
end;

initialization
  t:=TStringStream.Create('');
  TComObjectFactory.Create(ComServer, TContextMenu, CLSID_ContextMenuShellExtension,
         '', 'Delphi 3.0 ContextMenu Example', ciMultiInstance);
finalization
  t.Free;
end.
0
 
bakry99Author Commented:
ok frieands.
if you have a WinZip application in your Devices you can see thier own Item into PopUpMenu when you hilight on a file . i want to do so .
i wish you understand my question .
    bakry
0
 
intheCommented:
yes and i have pointed 2 two examples of what you need...

will you please look at the delphi demo\activex\shellex demo.

and the example i pasted above .
these are called shell extensions.
0
 
bakry99Author Commented:
ok inthe
  This is a unit How can i use it. i want a example
0
 
mirghaniCommented:
Listening:-)
0
 
bakry99Author Commented:
thanks inthe
0
All Courses

From novice to tech pro — start learning today.