Solved

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

Posted on 2000-03-17
6
483 Views
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:

#define WM_TREEVALIDATE WM_APP + 1

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

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

protected:
    virtual ~CLeftView();
};


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

...
IMPLEMENT_DYNCREATE(CLeftView, CTreeView)
...
BEGIN_MESSAGE_MAP(CLeftView, CTreeView)
    //{{AFX_MSG_MAP(CLeftView)
    ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelChanged)
    ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemExpanded)
    //}}AFX_MSG_MAP
    ON_MESSAGE(WM_TREEVALIDATE, OnTreeValidate)
END_MESSAGE_MAP()

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?
0
Comment
Question by:emitchell
[X]
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
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 75 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.
0
 
LVL 4

Expert Comment

by:captainkirk
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...
0
 

Author Comment

by:emitchell
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 =
    (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

// 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.




0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:chensu
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.
0
 

Author Comment

by:emitchell
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.

0
 
LVL 23

Expert Comment

by:chensu
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
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/devprods/vs6/visualc/vccore/_core_how_the_framework_calls_a_handler.htm
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

615 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