MFC: calling a menu item from another menu item
-
I have two menu items under the File menu - Call My Dialog & Call MessageBox. Call MessageBox will invoke a message box. I am trying to do the same thing when the user hits Call My Dialog menu item.
void CMainFrame::OnFileCallmessagebox()
{
// TODO: Add your command handler code here
MessageBox(_T("hello world!!"), _T("Sample message box"));
}void CMainFrame::OnFileCallmydialog()
{/\*this->SendMessage(, HIBYTE(ID\_APP\_CALL\_MESSAGEBOX), LOBYTE(ID\_APP\_CALL\_MESSAGEBOX));\*/ HWND hwnd = AfxGetMainWnd()->GetSafeHwnd(); ::SendMessage(hwnd, WM\_COMMAND, HIBYTE(ID\_APP\_CALL\_MESSAGEBOX), LOBYTE(ID\_APP\_CALL\_MESSAGEBOX));
}
Whatever I do I keep getting some failure assertion
-
I have two menu items under the File menu - Call My Dialog & Call MessageBox. Call MessageBox will invoke a message box. I am trying to do the same thing when the user hits Call My Dialog menu item.
void CMainFrame::OnFileCallmessagebox()
{
// TODO: Add your command handler code here
MessageBox(_T("hello world!!"), _T("Sample message box"));
}void CMainFrame::OnFileCallmydialog()
{/\*this->SendMessage(, HIBYTE(ID\_APP\_CALL\_MESSAGEBOX), LOBYTE(ID\_APP\_CALL\_MESSAGEBOX));\*/ HWND hwnd = AfxGetMainWnd()->GetSafeHwnd(); ::SendMessage(hwnd, WM\_COMMAND, HIBYTE(ID\_APP\_CALL\_MESSAGEBOX), LOBYTE(ID\_APP\_CALL\_MESSAGEBOX));
}
Whatever I do I keep getting some failure assertion
deostroll wrote:
Whatever I do I keep getting some failure assertion
First thing to do is look at the assertion message and figure out what is going wrong; or post it here so we can help you. Secondly your ::SendMessage[^] syntax is not correct. Thirdly, why not just tie both menu items to the
OnFileCallmessagebox()
function?txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
deostroll wrote:
Whatever I do I keep getting some failure assertion
First thing to do is look at the assertion message and figure out what is going wrong; or post it here so we can help you. Secondly your ::SendMessage[^] syntax is not correct. Thirdly, why not just tie both menu items to the
OnFileCallmessagebox()
function?txtspeak is the realm of 9 year old children, not developers. Christian Graus
suppose I had a modal dialog box. I click a button on it, I'd want to call the same routine (i.e. the Call Message Box click event) via SendMessage(). Is this not possible? I've rewritten and debugged my code...and found out that the values I am sending for wParam and lParam are wrong
this->SendMessage(WM\_COMMAND, HIWORD(ID\_APP\_CALL\_MESSAGEBOX), LOWORD(ID\_APP\_CALL\_MESSAGEBOX));
what are the correct values?
-
suppose I had a modal dialog box. I click a button on it, I'd want to call the same routine (i.e. the Call Message Box click event) via SendMessage(). Is this not possible? I've rewritten and debugged my code...and found out that the values I am sending for wParam and lParam are wrong
this->SendMessage(WM\_COMMAND, HIWORD(ID\_APP\_CALL\_MESSAGEBOX), LOWORD(ID\_APP\_CALL\_MESSAGEBOX));
what are the correct values?
deostroll wrote:
suppose I had a modal dialog box. I click a button on it, I'd want to call the same routine (i.e. the Call Message Box click event) via SendMessage().
I have no idea why you would want to do this. A modal dialog provides a break in an application where the user is asked for some information, or to make a decision, before returning to the main application. If your dialog needs to call another dialog or (terrible idea) itself, then your design is flawed.
deostroll wrote:
what are the correct values(for
SendMessage()
)?I cannot be certain, but I suspect it is something along the lines of
this->SendMessage(WM_COMMAND, ID_APP_CALL_MESSAGEBOX, 0L);
I suggest you take a look at the MSDN Documentation[^] for further information.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
deostroll wrote:
suppose I had a modal dialog box. I click a button on it, I'd want to call the same routine (i.e. the Call Message Box click event) via SendMessage().
I have no idea why you would want to do this. A modal dialog provides a break in an application where the user is asked for some information, or to make a decision, before returning to the main application. If your dialog needs to call another dialog or (terrible idea) itself, then your design is flawed.
deostroll wrote:
what are the correct values(for
SendMessage()
)?I cannot be certain, but I suspect it is something along the lines of
this->SendMessage(WM_COMMAND, ID_APP_CALL_MESSAGEBOX, 0L);
I suggest you take a look at the MSDN Documentation[^] for further information.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
What if the dialog was non-modal? Wouldn't this be a valid scenario? Btw how do u do a modeless dialog box?
deostroll wrote:
What if the dialog was non-modal?
The same considerations apply; it's a question of design and usability. If I am running an app on my PC and select a menu item which pops up a dialog box, and then press a button on that dialog which pops another dialog, I tend not to feel too kindly towards the designer. As I said before you need to understand what dialogs are for and use them sparingly. Programs that just throw dialogs and other clutter at users tend to be consigned fairly quickly to the dustbin of history.
deostroll wrote:
Btw how do u do a modeless dialog box?
You read the MSDN documentation[^] to start with.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
deostroll wrote:
What if the dialog was non-modal?
The same considerations apply; it's a question of design and usability. If I am running an app on my PC and select a menu item which pops up a dialog box, and then press a button on that dialog which pops another dialog, I tend not to feel too kindly towards the designer. As I said before you need to understand what dialogs are for and use them sparingly. Programs that just throw dialogs and other clutter at users tend to be consigned fairly quickly to the dustbin of history.
deostroll wrote:
Btw how do u do a modeless dialog box?
You read the MSDN documentation[^] to start with.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
Hey, I don't mean to call another dialog from another dialog. I just put a messagebox in one of my menu item's event handler. It is just to test if the code executes...which it apparently does not? k, on the topic on non-modal dialogs I don't understand the difference between the following 2 snippets:
/* Snippet 1 */
CMyDialog* dlg = new CMyDialog();
dlg->Create(IDD_MY_DIALOG);
dlg->ShowWindow(SW_SHOW);/* Snippet 2 */
CMyDialog dlg = new CMyDialog();
dlg.Create(IDD_MY_DIALOG);
dlg.ShowWindow(SW_SHOW);Snippet 2 doesn't work! Why is that so?
-
Hey, I don't mean to call another dialog from another dialog. I just put a messagebox in one of my menu item's event handler. It is just to test if the code executes...which it apparently does not? k, on the topic on non-modal dialogs I don't understand the difference between the following 2 snippets:
/* Snippet 1 */
CMyDialog* dlg = new CMyDialog();
dlg->Create(IDD_MY_DIALOG);
dlg->ShowWindow(SW_SHOW);/* Snippet 2 */
CMyDialog dlg = new CMyDialog();
dlg.Create(IDD_MY_DIALOG);
dlg.ShowWindow(SW_SHOW);Snippet 2 doesn't work! Why is that so?
deostroll wrote:
Snippet 2 doesn't work! Why is that so?
I don't really know MFC that well, but I guess the
CMyDialog()
constructor returns a pointer to a new object. Sorry, senior moment there,new
returns a pointer to the object created. You can check what actually happens when you run your app by using the debugger.txtspeak is the realm of 9 year old children, not developers. Christian Graus
modified on Friday, February 12, 2010 12:11 PM
-
deostroll wrote:
What if the dialog was non-modal?
The same considerations apply; it's a question of design and usability. If I am running an app on my PC and select a menu item which pops up a dialog box, and then press a button on that dialog which pops another dialog, I tend not to feel too kindly towards the designer. As I said before you need to understand what dialogs are for and use them sparingly. Programs that just throw dialogs and other clutter at users tend to be consigned fairly quickly to the dustbin of history.
deostroll wrote:
Btw how do u do a modeless dialog box?
You read the MSDN documentation[^] to start with.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
Richard MacCutchan wrote:
If I am running an app on my PC and select a menu item which pops up a dialog box, and then press a button on that dialog which pops another dialog, I tend not to feel too kindly towards the designer.
Really? This sort of thing happens all over Windows (XP and Vista), Office (2003 and 2007), IE (6 and 8), Visual Studio (6 and 2005), etc.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Richard MacCutchan wrote:
If I am running an app on my PC and select a menu item which pops up a dialog box, and then press a button on that dialog which pops another dialog, I tend not to feel too kindly towards the designer.
Really? This sort of thing happens all over Windows (XP and Vista), Office (2003 and 2007), IE (6 and 8), Visual Studio (6 and 2005), etc.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius