Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Doc/View main window

Posted on 1999-07-07
Medium Priority
Last Modified: 2013-11-20
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.

Question by:Zainal062797
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1309200

Would you like to send a message or call a function in your main window?
LVL 23

Expert Comment

ID: 1309201
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?

Author Comment

ID: 1309202
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.
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.


Expert Comment

ID: 1309203
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();


Author Comment

ID: 1309204
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.

Expert Comment

ID: 1309205
no !
the way to do it is this:
CMyFrame* pFrame=(CMyFrame*)AfxGetMainWnd();

Accepted Solution

Phaedan earned 150 total points
ID: 1309206
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.


Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question