MainWnd->SendMessage(...) to a view doesn't seem to be sent

Posted on 2000-03-17
Medium Priority
Last Modified: 2013-11-20
I am trying to send a message to a CTreeView from the CDocument derived part of my program.

I send the message with:

    // ?? this doesn't work
    CWnd* pMain = AfxGetApp()->m_pMainWnd;
    pMain->SendMessage(WM_TREEVALIDATE, 0);

where WM_TREEVALIDATE is defined in mainapp.h as:


In the CTreeView class I define my OnTreeValidate() handler function outside the Wizard braces:

class CLeftView : public CTreeView {
    // ClassWizard generated virtual function overrides
    virtual void OnInitialUpdate();
    virtual void OnDraw(CDC* pDC);
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    virtual void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint);
    afx_msg void OnTreeValidate(WPARAM, LPARAM);

    virtual ~CLeftView();

In the .cpp file I add the ON_MESSAGE() macro for my message, again  outside the Wizard part as in:


and finally I add an empty handler OnTreeValidate() so that I can set a breakpoint there just to test that everything is working.

void CLeftView::OnTreeValidate(WPARAM, LPARAM)

I can stop at a breakpoint at the SendMessage() line but my breakpoint in the OnTreeValidate() never hits.

Am I doing something silly?
Question by:emitchell
  • 3
  • 2
LVL 23

Accepted Solution

chensu earned 300 total points
ID: 2629969
You are sending the message to the main frame window which is the parent window of the view. You need to either forward the message from the main frame window to the view or find out the view and send it directly.

Expert Comment

ID: 2629976
It looks like you are sending the message to the main frame class, not the tree view class...

maybe what you would like to do is get a pointer to the tree view class rather than the main frame class...

CLeftView* pLeftView = // get a pointer here...perhaps by GetActiveView()

pLeftView->SendMessage(WM_TREE_VALIDATE, 0); // send message to view...

Author Comment

ID: 2633498
I have an MDI app where the child window contains a CTreeView (my CLeftView) and a CListView (my CRightView) in a splitter window, just like Explorer.

The help for GetActiveView() has the following:

The active view in an MDI application can be obtained by first finding the active MDI child window and then finding the active view for that child window. The active MDI child window can be found by calling the function MDIGetActive or GetActiveFrame as demonstrated in the following:

CMDIFrameWnd *pFrame =

// Get the active MDI child window.
CMDIChildWnd *pChild =   (CMDIChildWnd *) pFrame->GetActiveFrame();

// or CMDIChildWnd *pChild = pFrame->MDIGetActive();

// Get the active view attached to the active MDI
// child window.
CMyView *pView = (CMyView *)->GetActiveView();

My question is, if I use the active pView above, is this going to be the CTreeView or the CListView.

I don't want to send the message to the CListView since it can't handle it.

Alternatively, I'm not sure what MFC does as far as presenting the message to a sequence of views in turn.  I would sort of expect that if I sent the message to the CSplitterWnd that owns the CTreeView this would see if the message would be handled in any of its child windows and then pass the message up the line.

Mike Blaszczak in "Professional MFC" (p93) says that:

"After checking the window directly targeted by the message, MFC will then examine some other windows to see if they'd be interested in handling it."

It's this sequence that is confusing.

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

LVL 23

Expert Comment

ID: 2634439
The easiest way is

1. In your CLeftView::OnCreate, record its window handle (GetSafeHwnd()) to a member variable of your CDocument class.

2. Use the window handle to send a message to it directly.

Author Comment

ID: 2634519
Thanks.  This is obviously the way.  If I tell the document where to send the message, there is no debate.

Now when I use the CLeftView remembered in my doc class to send the message, everything works as expected.

I appreciate the help.

As an aside, I found an interesting chapter in Shepherd and Wingo's "MFC Internals" on the message routing - Chapter 3, "Message Handliing in MFC."  I was confusing the routing of the WM_COMMAND messages that are sent various places and the regular Window messages that are sent to a specific window.

LVL 23

Expert Comment

ID: 2634986
>I was confusing the routing of the WM_COMMAND messages that are sent various places and the regular Window messages that are sent to a specific window.

In MFC, the WM_COMMAND messages get routed through a chain of command targets. But standard Windows messages do not. See

How the Framework Calls a Handler

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.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

600 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