Solved

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

Posted on 2000-03-17
6
471 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
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 75 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
Comment Utility
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
Comment Utility
>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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now