Adding a menu item to the main menu items

Hello All,

I would like my wizards (which currently reside in the Tools menu) to appear in their own menu item (just preceding the Help menu item). I am surprised; that what I thought was straightforward has proven to be rather elusive. Please don't launch into a long discourse about TIMenuItemIntf, I just want the line(s) of code to create a stand-alone separate menu item.

Who is Participating?
JimBob091197Connect With a Mentor Commented:
Hi all

Edo: I am posting an answer now!

Zif: Nice speech, man!  :)


P.S.  There was once a big debate on this site about whether code is better or a reference to another web site.  I still prefer a few short lines of code rather than wading through a lot of stuff on another web site, but as Zif says in #6 - you usually get more code than you wanted...
I'm guessing you talk of Delphi mainmenu or do you talk of your own programs mainmenu?
Hi Edo,

I start with my first guess, the Delphi mainmenu :

ever read these pages?

another, more appropriate, because you ask for code is this :

especially from 8. TAddInMenuListExpert

Hope, that you now have the code you need and some more code you never asked for, but my guess is that you don't mind this

Regards, Zif.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Edo082297Author Commented:
Hello ZifNab

Your paltry attempt at an aswer should have been posted as a comment. I digested both Dr.Bob's article on Delphi Wizards and the OpenTools API, the GExpert article and the source code to GExpert's long ago. (I cannot obtain Gerald Nunn's method of adding a main menu item since he uses a function call that is part of Ray Lischener's _Hidden Paths_ companion source (or is it _Secrets ...?) source, and is thus not reproducible).

You are getting lazy in your lofty position, try again.

Hi Edo

I'm posting this as a comment so I don't get blasted like ZifNab!!  ;-)

In my expert's constructor, I declare 2 variables:
  MainMenu: TIMainMenuIntf;
  MenuItems: TIMenuItemIntf;

Also, NewMainMenuItem and NewMenuItem are declared in the expert as TIMenuItemIntf.  They are freed in the expert's destructor.

I insert the menu as follows:
  MainMenu := ToolServices.GetMainMenu;
  if (MainMenu <> nil) then
      MenuItems := MainMenu.GetMenuItems;
      if (MenuItems <> nil) then
          // Add item to Delphi's main menu bar.
          NewMainMenuItem := MenuItems.InsertItem(8, '&Test', 'TestMainMenuItem', '', 0, 0, 0, [mfVisible, mfEnabled], nil);

          // Add a sub menu to the new menu item.
          NewMenuItem := NewMainMenuItem.InsertItem(0, '&Sub Item', 'NewSubMenuItem', '', 0, 0, 0, [mfVisible, mfEnabled], OnSubClick);

Hope this helps you.
Edo082297Author Commented:
Hello JB

Thankyou for your correct posting. I am now wondering what I wrote at work, it seems eerily similar, however I think I was trying an insert without the GetMenuItems call. Hmmm....

I wouldn't have blasted Zifnab had the section he refers to in Dr.Bob's article actually added a main menu item. It does not (unless there is a new version of the article, in which case I apologize profusely, and feel rather silly). The article adds a sub menu item to the Tools menu.

Post an answer...

Baie danke


From the bloody mess and still smoking body parts, you see on the floor right before you (it used to be ZifNab) I can only say :

1. I can't smell you already read these articles
2. You've to admit that this is rich information for the unknown
3. You knew how to enter SubMenu's? Correct?
4. Looking at the simple proposed code you can see the GetMainMenu method?
5. Knowing 3 and seeing 4, little step to put your wizards in your own 'mainmenu'.
6. And wouldn't it be great to get more code than you wanted? With full explenation? (but offcourse I didn't knew 1)
7. If you already had some code, you could also have posted it here. That's easier to see the problem.

But heck, who am I. Just somebody who wants to help people on a freely non-profit basis. Who is offcourse not always right (I always say that there are far more better 'experts' on this site than I am, I'm even not a programmer from education or job) and is most of the time talking out of his head.

Now, I'll take my escape through the sewer so my blood will be mixed by wather and I'll be invisible. And then I'm gone...

** poof **

Only few words in the sky will remember you that I was here :

Both, have a nice 1999.

Zif (R.I.P. 29 december 1998)
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.