?
Solved

Add MenuItem to MainMenu from dll

Posted on 2003-03-26
6
Medium Priority
?
544 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

771 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