menu on a button click event
Hi all,
I am having a main exe dialog box from which another dialog box opens up which is having a button and a edit box. On clicking button my menu appears. what i want is when i click on menu item particular text should be written in edit box. when i open class wizard for menu items and choose my dlg class and write OnCommand code over there a debug error is given and when i choose second class as base class nothing happens on click event
Can anybody help me in this
Thanks in advance
# 1 Re: menu on a button click event
To clarify:
Unless you are using some language other than C++, you do not handle button click event, you handle command message that system generated as the result of clicking on the button.
Several things are unclear:
when i open class wizard for menu items and choose my dlg class and write OnCommand code over there . . .Could you explain what do you mean by that? Why do you need OnCommand override if you use wizard to map command message?
a debug error is given and when i choose second class as base class nothing happens on click eventWhat debug error? What do you mean by second class?
JohnCz at 2007-11-11 4:02:10 >

# 3 Re: menu on a button click event
You do not need OnCommand.
Use class wizard to map menu item ID to a message handler in a dialog class you want to handle message.
How you invoke a menu?
Consider posting your project; this will be the fastest way of resolving your problem.
Do not include debug/release directories.
JohnCz at 2007-11-11 4:04:11 >

# 6 Re: menu on a button click event
Your post is misleading, that is why I have asked you to post your project.
You are not using OnCOmmand, wizard is mapping WM_COMMAND message to a handler using ON_COMMAND macro.
You have to read more about MFC message maps.
Also, next time you post your problem try to explain it precisely using exact terms and functions/macros names.
Reading MSDN description of the TrackPopupMenu, would tell you that last parameter is a pointer to a window that owns the menu (will receive command messages).
Never use something like menu.GetSubMenu(0)-> without checking a return value. Code like this is a recipe for disaster. If GetSubMenu returns NULL your application crashes.
To fix your problem: void CDir::OnButton1()
{
CPoint pt;
CRect rect;
GetDlgItem(IDC_BUTTON1)->GetWindowRect(&rect);
CMenu menu;
menu.LoadMenu( IDR_MENU1);
CMenu *pSubMenu = menu.GetSubMenu(0);
ASSERT(pSubMenu);
if(NULL == pSubMenu)
{
return;
}
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
rect.left, rect.bottom + 2, AfxGetMainWnd());
}always check validity of pointers and return function values and handle errors to prevent your app from crashing.
JohnCz at 2007-11-11 4:07:15 >
