• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Doc/View main window

I am writing a Document/View application. In the Document module, I am trying to send a message to the main window. I am using
   AfxGetMainWnd()->SendMessage(WM_USER, NULL, NULL)
to send the message. But when I use this function, my application crashes. I am wondering what I am doing wrong. Also, I am wondering if there is another way to pass a message to the application, or main window, other than this method.

1 Solution

Would you like to send a message or call a function in your main window?
Make sure that the main window pointer you get after call to AfxGetMainWnd is a valid one. Frame window gets created after document.

CWnd *pWnd = AfxGetMainWnd ();
if (NULL != pWnd) {
CFrameWnd *pFrameWnd = static_cast<CFrameWnd *>(pWnd);
pFrameWnd->SendMessage (WM_USER, NULL, NULL);

What kind of message u r trying to send to aplication?
Zainal062797Author Commented:
I want to do both, send a message and use a function. But I guess it is what naveenkohli   talked about. I am using the call in the document constructor, which menas that the main window is not yet created, and thus I am getting an invalid pointer.
Thank you.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

do this:
u must create new message WM_USER is a constant declaring user messages  you have to use WM_USER+1000 for your message
using registermessage and than u can send the message like this:
CMyFrame* pFrame=(CMyFrame*)AfxGetMainWnd();

Zainal062797Author Commented:
Okay let me give you the big picture. I have functions in the main window which Enable/Disable menu items. I want to invoke these function from within the Document module. The only way of doing it is to send a message to the main window.
Now, back to you Shay, Do I have to register a message or I can simply send a WM_USER + n  and in my message map declare
      ON_MESSAGE ((WM_USER+n), OnEnableMenuItem)
for example?

Thank you.
no !
the way to do it is this:
CMyFrame* pFrame=(CMyFrame*)AfxGetMainWnd();
If you are using the DOc/View model you should write ON_COMMAND_UPDATE handlers for the menu items. In class wizard, if your menu item is IDM_MYMENU

Select Message Maps in the classwizard.  In the list of classes, select the class to handle the event, in this sample CMyDoc. In the the list of resources find IDM_MYMENU.  On the right hand side you will have two options ON_COMMAND, ON_COMMAND_UPDATE.  The ON_COMMAND handler responds when the menu item is clicked.  The ON_COMMAND_UPDATE handler will allow you to set the state of the menu item based on data stored in the document.

For example: if CMyDoc had a boolean member variable m_myMenuChecked, then you could control the menu item with the following

void CMyDoc::OnMyMenuUpdate(CCmdUI* pCmdUI)

CCmdUI also has functions for Enabling/Disabling the menu option.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now