Archived

This topic is now archived and is closed to further replies.

Zipster

Having troubles with a Message Handler in MFC...

Recommended Posts

i create a menu item in the File menu called with an id of ID_LOAD_TEMPLATE, and then added a COMMAND handler using the Add Windows Handler Wizard (in VC++ 5.0). This is the code I have in my OnLoadTemplate handler: CFileDialog dlg(true); if(dlg.DoModal() == IDOK) { m_strFileName = dlg.GetFileName(); } The problem im having is that the program compiles and runs, but when i try to select Load Template from the menu, its grayed out and i can''t select. I checked the grayed flag and its set NOT to gray, but it does anyway. This has stumped me!!

Share this post


Link to post
Share on other sites
Hmm, I''ll probably have to look at it to tell what''s wrong... but a couple things to check are:
Menu selections will be greyed in MDI when the view of the MDI corresponding to the menu is active but the view itself is not in focus (usually at the start)

Occassionally the resource editor screws up and assigns the same ID number to 2 controls. Check your resource.h for that menu item''s ID name and make sure the number isn''t off. If it is, fix it, but be sure to fix the number incremental set at the bottom of the .h file.

Are you sure you used the right method of instantiating the control message for the menu? You might try comparing your menu code to menu code from, say, codeguru.com.

-fel

Share this post


Link to post
Share on other sites
I used AppWizard to make an SDI program, so no MDI involved. This is the first menu item i added (aside from what appwizard gave me, i.e print, save, etc..) so i don''t think it would confilct with anyting, i used WIndows Message Handler wizard to add it, so i don''t know what happening..
BTW, this is also the case with menu items i add after this one also!!

Share this post


Link to post
Share on other sites
Okie, let''s try this... (debugging without seeing the code is hard *smile*)

You should have a message map in your .cpp file for your view, and it should have stuff like this:
ON_COMMAND(ID_BOARDSIZE, OnBoardSize)

which should be grey because it''s in the
BEGIN_MESSAGE_MAP(CPegDlg, CDialog)

area.
You should have one of these for each menu item you have, with the right ID and corresponding function. These functions must exist down below somewhere, declared like this:
void CPegDlg::OnBoardSize()

They should be declared in the header file like this:
afx_msg void OnBoardSize();

after the
//{{AFX_MSG(CPegDlg)

check for all this... if something in here is missing it won''t work.

HTH
-fel

Share this post


Link to post
Share on other sites
I figured out what was wrong, but I don''t know why it would not work. The problem was that I declared the handler in the View class instead of the Main Frame class. I don''t know why this makes a difference, since i''ve made other applications where it doesn''t matter. Also, a programming book i have delcares the handerls in the View class, and it works.

Share this post


Link to post
Share on other sites
Hmm...
I might be shooting in the dark here, but is this an SDI or an MDI? MDI''s require you to declare view-specific menus in the views because the menu on the mainframe has handlers asking for the default menu of that particular view, though the menus themselves are instantiated in the mainframe. SDI''s don''t have any "mainframe go get the menu from the view so you get the right one" instruction stuff, because you have one and only one view, therefore you have one and only one menu.
Anyway I think that''s how it works.

-fel

Share this post


Link to post
Share on other sites