Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Add MenuItem to MainMenu from dll

Posted on 2003-03-26
6
Medium Priority
?
555 Views
Last Modified: 2010-04-03
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
0
Comment
Question by:MartinVenter
6 Comments
 
LVL 9

Expert Comment

by:mocarts
ID: 8211044
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
 

Author Comment

by:MartinVenter
ID: 8211119
Hi mocarts

Have tried your suggestions before posting with no luck.

Regards,
0
 
LVL 6

Expert Comment

by:DaFox
ID: 8211363
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

by:smurff
ID: 8211977
listening
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 200 total points
ID: 8212426
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
 

Author Comment

by:MartinVenter
ID: 8216304
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question