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
Solved

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

Posted on 2000-03-17
6
480 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modbus - whats the maximum I can store in one register? 4 80
changeXy challenge 13 87
Problem to App 4 97
I could not set window to top 4 20
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. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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