Add MenuItem to MainMenu from dll

Hi Everyone

I have a main application that loads some DLL's on start-up.
When the DLL gets loaded it should add a menu item to the application main menu.

Below is the code. It works if I put it in the main application to test but from the DLL nothing appears on the menu. I step through the code and there are no errors.

My Main menu structure is: File | Tools ...

The Dll's need to add an Item under Tools.

procedure THelloWorldPlugIn.AddMenu(HM : HMENU; var MenuID : Integer);
var
  MENUITEMINFO: TMenuItemInfo;
  sCaption: String;
  MenuItem: TMenuItem;
begin
  sCaption := 'Test Item';
  FillChar(MENUITEMINFO, SizeOf(MENUITEMINFO), 0);
  MENUITEMINFO.cbSize := SizeOf(TMenuItemInfo);
  MENUITEMINFO.fMask := MIIM_TYPE and MIIM_ID and MIIM_STATE;
  MENUITEMINFO.fType := MF_BYPOSITION and MFT_STRING;
  MENUITEMINFO.wID := MenuID;//1101; // ID must be < $F000
  MENUITEMINFO.dwTypeData := PChar(sCaption);
  MENUITEMINFO.cch := Length(sCaption);
  MENUITEMINFO.fState := MFS_ENABLED;

  InsertMenuItem(HM, MenuID, TRUE, MENUITEMINFO);
  MenuID := MenuId + 1;
end;

What is missing or is there another way of doing this.

Regards,
Martin
MartinVenterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mocartsCommented:
code seems to be without errors..
when you call this dll proc? try to call it in forms OnShow event (when MainMenu is created surely)
and try to call DrawMenuBar(HM); after InsertMenuItem(..)
wbr, mo.
0
MartinVenterAuthor Commented:
Hi mocarts

Have tried your suggestions before posting with no luck.

Regards,
0
DaFoxCommented:
Hi Martin.

For the moment I can't think of a solution, either.
A few things:

- Handle returned by GetMenu() is correct?
- Watch out that the menu IDs do not collide with
others the application uses!
- You have to subclass the menus owner window to get informed of user activation!
- What happens when you're using AppendMenu() insted of InsertMenuItem() (for test purposes!)?

Markus
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

smurffCommented:
listening
0
Slick812Commented:
hello MartinVenter, You did not quite get the TMenuItemInfo parameters correct, this might work better

procedure THelloWorldPlugIn.AddMenuItem(hWnd, HM: Integer; var MenuID: Integer);
var
 MENUITEMINFO: TMenuItemInfo;
 sCaption: String;
 //MenuItem: TMenuItem;
begin
 sCaption := 'Test Item';
 //FillChar(MENUITEMINFO, SizeOf(MENUITEMINFO), 0);
 MENUITEMINFO.cbSize := SizeOf(TMenuItemInfo);
 MENUITEMINFO.fMask := MIIM_TYPE or MIIM_ID {or MIIM_STATE};
 MENUITEMINFO.fType := {MF_BYPOSITION or} MFT_STRING;
 {Do NOT use MF_ constants in the fType}
 MENUITEMINFO.wID := MenuID;
 MENUITEMINFO.dwTypeData := PChar(sCaption);
 //MENUITEMINFO.cch := Length(sCaption);
 {the system does NOT read this cch for this operation}
 //MENUITEMINFO.fState := MFS_GRAYED;
{You Only need to use fState if it is NOT MFS_ENABLED}

 InsertMenuItem(HM, 0{MenuID}, False, MENUITEMINFO);
 DrawMenuBar(hWnd);
 MenuID := MenuId+1;
end;


- - - - - - - - - - - - - - - - - - - - - -

NOTE - I have tried many times to use API Menu functions to change Delphi Menus, but the Tmenu is DOES NOT Change, with these API alterations. . . And the VCL TMenu will not have any Click event for that menu Item . . . You might want to only use Delphi VCL Menu Changes. . . Use a TMenuItem
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MartinVenterAuthor Commented:
Hi Slick812,

I've decided to create a procedure that adds a TMenuItem to the menu I give as a parameter.

Thank you everyone for your input.

Regards,
Martin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

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.