Hiding pop-up menu

Hi,

I have an MDI application for which I have a menu. In one of the main menu items, I have a drop-down menu. In the drop-down menu, there is a pop-up menu. In certain cases all the items in the pop-up menu need to be removed. There is no problem with this. However, I want to remove the pop-up menu item itself.
i.e. There is an item called Receiver. When user clicks on Receiver, there are three other choices they can choose from. I can remove these choices without a problem, but how to remove the item called "Receiver"? This has no ID so I don't know how to remove it.
Any pointers would be greatly appreciated.

Thanks
[667 byte] By [neha75] at [2007-11-18 1:40:27]
# 1 Re: Hiding pop-up menu
Have you tried using CMenu::RemoveMenu()? From MSDN:
Deletes a menu item with an associated pop-up menu from the menu. It does not destroy the handle for a pop-up menu, so the menu can be reused.

You do not need the ID to use RemoveMenu. Use the MF_BYPOSITION flag with the index of your popup menu.
Mike Harnad at 2007-11-10 8:54:07 >
# 2 Re: Hiding pop-up menu
Hi
Thanks for your reply. Yes I implemented the functionality using RemoveMenu() after I posted the question and it seems to work.

However, when I delete the pop-up menu item when it's not available and without closing the app, open a file for which it is available, the item does not show up. When I exit the app and re-open the file, then it shows up again.

Any ideas?

Thanks.
neha75 at 2007-11-10 8:55:16 >
# 3 Re: Hiding pop-up menu
Originally posted by neha75
However, when I delete the pop-up menu item when it's not available and without closing the app, open a file for which it is available, the item does not show up. When I exit the app and re-open the file, then it shows up again.
:confused: :confused:
Could you please repeat this step by step so everybody will be able to follow?
JohnCz at 2007-11-10 8:56:15 >
# 4 Re: Hiding pop-up menu
Are you using a state variable to track the requirement for the menu item? For example, a boolean variable that would be true if the menu item should be displayed and false if not. If so, you will need to code an ON_UPDATE_COMMAND_UI handler to check the variable and modify the menu before it is displayed.
Mike Harnad at 2007-11-10 8:57:19 >
# 5 Re: Hiding pop-up menu
Mike,
Yes, I already have an ON_UPDATE_COMMAND_UI that checks for the availability of the functionality before displaying the menu item.

I am sorry if I was vague in my explanation. Let me explain what I want to do:

My application allows for two types of files: File1 and File2. The functionality differs in both types. File1 allows for Transmitting, while File2 is only for Receiving.
Both the Transmitting and Receiving menu items are pop-up menus with no IDs. So, in the ON_UPDATE_COMMAND_UI, I check whether the file being opened is File1. If it is, then I want to REMOVE the Receiving pop-up menu. If File2, then REMOVE the Transmitting pop-up menu.
This works fine the first time, meaning, when I execute the app and open File1, then only the Transmitting menu is available. However, the problem is that, if I open File2 after closing File1, then the Transmitting menu shows and the Receiving does not. If I close the app and run it again and open File2, then it is working fine.
I hope I explained the problem better now.

Thanks so much for your help!
neha75 at 2007-11-10 8:58:16 >
# 6 Re: Hiding pop-up menu
From what you described, I would guess that the condition you are checking is incorrect. Can you provide a code snippet of the ON_UPDATE_COMMAND_UI processing? Also, how do you reset the condition when a new file has been opened?
Mike Harnad at 2007-11-10 8:59:21 >
# 7 Re: Hiding pop-up menu
ON_UPDATE_COMMAND_UI can be tricky for modifying menus, reason: if you change the menu it will retain the changes for ever. For example once you remove the Reciever menu you have to plug in that manually later if you want to see it again. We don't notice this when doing simple enable/disable because its always get decided at right time but when you remove a menu item, you will notice its gone for ever! you have to plug that in again.

I have had this problem before and I took a different approach to solve it but if you did this the way you are trying, I would like to see your code if you would post here.
quark at 2007-11-10 9:00:14 >
# 8 Re: Hiding pop-up menu
Ignore
neha75 at 2007-11-10 9:01:16 >
# 9 Re: Hiding pop-up menu
Here is the code snippet:

CMenu *pMenu = ((CMainFrame*)AfxGetMainWnd())->GetMenu();
int MenuCount = pMenu->GetMenuItemCount();

for (int i=0; i < MenuCount; i++)
{
if (pMenu->GetMenuString(i, str, MF_BYPOSITION) &&
(strcmp(str, "Fe&ature") == 0))
{
nPos = i;
break;
}
}

CMenu* pSubMenu = pMenu->GetSubMenu(nPos);
int SubMenuCount = pSubMenu->GetMenuItemCount();

for (int n = 0; n < SubMenuCount; n++)
{
if (pDoc->isReceiveOn() == true)
{
if (pSubMenu->GetMenuString(n, str, MF_BYPOSITION) &&
(strcmp(str, "Receiver Alignments") == 0))
{
pSubMenu->RemoveMenu(n, MF_BYPOSITION);
}
}
}

I don't have a variable that I use to track. The variable that I use to check whether is receive is available is in the file itself, so I don't think I would need to have another one.

Thanks for your help!
neha75 at 2007-11-10 9:02:24 >
# 10 Re: Hiding pop-up menu
That was my same course of action and I have to drop handling it here in UpdateUI because you have to remove and add the menu everytime and this function is called very frequently and quite randomly it was just not looking a good programming practice for me. Besides my requirments were slightly different,I think this may be the right place for you to handle it but again you have to add the code to Insert the deleted menu as well if its not there.

I hope this helps.
quark at 2007-11-10 9:03:24 >
# 11 Re: Hiding pop-up menu
I have to agree with quark. I don't see where you are re-adding the menu once you've removed it.
Mike Harnad at 2007-11-10 9:04:22 >
# 12 Re: Hiding pop-up menu
Where and how do I re-add it?
I looked into InsertMenu() and InsertMenuItem() but couldn't quite figure out where to add it

Thanks.
neha75 at 2007-11-10 9:05:19 >
# 13 Re: Hiding pop-up menu
You should be doing the check during processing of the ON_UPDATE_COMMAND_UI handler. At that time, you would check to see if you need to remove or insert the menu. For example:

if (file_variable == "receive")
RemoveMenu();
else
InsertMenu();

I think the basis for your problem is the method you are using to determine if the menu should be removed or inserted. It appears that you may be relying on the menu itself to tell you instead of the contents of your file.
Mike Harnad at 2007-11-10 9:06:26 >
# 14 Re: Hiding pop-up menu
neha75,

Attached find sample code to do what I think you wanted to do.

Doc menu item is used only to test document type. It has to be removed, as well as part setting document type. You will replace it with your own logic.

You were right, you cannot use Update Command here for popup item.
JohnCz at 2007-11-10 9:07:30 >
# 15 Re: Hiding pop-up menu
In MDI app, you could associate two different menus with different Document Temlates. Then you will not have to modify menus at run time!
VladimirF at 2007-11-10 9:08:23 >
# 16 Re: Hiding pop-up menu
That is correct. You may assign different resources including menu, doc strings and icon by adding additional document templates.

Using this approach you come across a side effect that may be undesirable; a dialog that is displayed when you are trying to create new document.

There is a work around it but my response (sample code) addressed specifically neha75s requirements removing and adding menu on the fly.
JohnCz at 2007-11-10 9:09:25 >