Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2000-03-17
6
Medium Priority
?
488 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 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.
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

885 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