• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

Menus (WinAPI and Handles)

I have such situation..
I need to access other application's menu from my program.. get an entire list of it's items, rebuild the menu in tree, and then modify the other application menu's item's text..
  • 4
  • 3
1 Solution
Try the APIs GetMenu, GetMenuItemCount, GetMenuItemInfo and SetMenuItemInfo.

I don't know, though, whether these APIs work for menus of other applications. You'll have to try that. If that doesn't work, you'll have to put it all into a little DLL and load it into the other applications (e.g. by calling SetWindowsHookEx). This would be quite complicated stuff...

Regards, Madshi.
Using the winAPI this is not too dificult here are the steps:

1) get a window handle for the other application.  
2) get the handle for the menu of the window handle you just got.
3) modify the menu items

If you want code for these three steps just let me know.
pbibergalAuthor Commented:
to Calvinrsmith: what is the code for modifying items?
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Use ModifyMenu.  here are the paramaters:

hmenu - the handle to the menu
uposition - what item you want to change
uflags - use MF_STRING for a text menu
uidnewitem - the id for the new menu
lpnewtiem - if a text menu then this is a null-terminated string.

Also don't forget to call DrawMenuBar after you make the change!
pbibergalAuthor Commented:
to Calvinrsmith..
i need a code to fullscan the menu and create a copy in treeview.. how to do it?
OK I'll give complete code.  but first what do you mean by treeview?
pbibergalAuthor Commented:
to Calvintsmith:
treeview = TTreeview object placed in win32 page of component pallete..

I don't have my copy of delphi with me right now so here is complete code as far as my memory goes:

   HWND thewindow;
   HEMNU themenu;
   HMENU thesubmenu;
   MenuItemInfo mii;
   // menu text will go here use
   // a size big enough for longest
   // menu item
   PChar themenutext[100];
   boolean (or is it bool?) done;
   integer i;

// Find the window handle of the other
// application
thewindow := findwindow("class name","window name");

// Find the handle for the menu
themenu := GetMenu(thewindow);
// get what sub menu ie. file, edit...
// the zero is for first menu
// 1 would be for edit etc...
thesubmenu := GetSubMenu(themenu,0);

// loop through each item in the menu
done := false;
i := 0;
while not done do
// scan the menu
mii.cbsize := sizeof(MenuItemInfo);
mii.fmask := MIIM_STRING;
mii.dwTypeData := themenutext;
mii.cch := 100;
if(GetMenuItemInfo(thesubmenu,i,true,mii) = false)
   done := true;
// next time though the loop get
// next item.
i := i + 1;
// now themenutext contains the text
// of the first item in the file menu
// so place it into the treeview control
// i'm guessing you know how to do this

// that's all!!!

This is the basic idea.
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.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now