Click to See Complete Forum and Search --> : ON_UPDATE_COMMAND_UI question
Matthew Smith
April 1st, 1999, 01:29 PM
Hi:
I have an output window that's a CRichEditCtrl, that I've created a context sensitive menu for. One of the options is 'Copy' to copy the selection in the window to the clipboard. When the user selects 'Copy' from the context menu, it goes and calls Copy(). This works fine, however I'd like to add an ON_UPDATE_COMMAND_UI handler to properly gray out the menu choice when nothing is selected. The problem I'm having is that my ON_UPDATE_COMMAND_UI code seems to get executed after the user makes a menu choice, not before, so 'Copy' is never grayed out. Is there some way around this problem?
thanks,
-Matt
Allen Yuh
April 1st, 1999, 07:19 PM
If you create a context menu for your RichEditCtrl, you can disable the "Copy" menu when user right click mouse, if there is no selection in the control.
For example:
void CMyClass::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menuText;
menuText.LoadMenu(IDR_ID_POPMENU);
// remove the header of menu
CMenu* pMenuPopup = menuText.GetSubMenu(0);
menuText.RemoveMenu(0, MF_BYPOSITION);
// only enable necessary menu items to user
if(m_bTestSelection == FALSE){
// no text is selected
// disable the "Copy" menu item
pMenuPopup->EnableMenuItem(ID_EDIT_COPY,
MF_GRAYED|MF_BYCOMMAND);
}
pMenuPopup->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, pWnd, NULL);
}
//...........
That should disable the "Copy" menu, if the m_bTextSelection is false.
Hope this will help. Good luck.
Allen
April 7th, 1999, 07:21 AM
I have the same problem as the original poster(Matthew Smith). So far I have used a solution similar to the one Allen is proposing, but do it really have to be like that? Isn't this what the ON_UPDATE_COMMAND_UI is meant to solve?
In my case things get quite messy when I have to do all the work manually (I have several levels of submenus on the top level popup menu). It would have been much nicer if I could get the OnCommandUpdateUi scheme to work.
I suspect that my problems are related to the fact that the main menu does not have the menu entries that are used in the popup menu. When the framework is routing the message it discards the message because it is not in the main menu, but I have not been able to verify this.
If anyone knows how to get this to work properly if would be happy to hear about it.
thanks,
Olav
Jim Garrison
April 30th, 1999, 09:49 AM
This is just a guess, but have you tried putting the popup menu in the main menubar
and hiding it (non-visible)? This may prevent the message from being discarded.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.