Sending CChildView a Message :: MFC

kuphryn

Senior member
Jan 7, 2001
400
0
0
Hi.

I am working on a program using the doc/view architecture. There is basically the program. There is a modeless dialog box where the user enters some information. When the user clicks "Apply" the dialog box sends a message to CMainFrame. From there, CMainFrame sends a message to CChildView. CChildView then draws the information as text inside its area.

I do not know how to send CChildView a message within CMainFrame. I am using the same technique Promises uses in Chapter 8 dialog 2 program (example of modeless). The only difference is he did not implement the program using doc/view. I am. In doc/view I do not know how to send a message from CMainFrame to CChildView because there is not CChildView object defined in CMainFrame:

CMyChildView cView;
cView.PostMessage(WM_MY_MESSAGE, 0, 0)

The code above does not work in doc/view.

I am using the same technique that Promises did. I am not sure if that is the prefer technique in the doc/view architecture. Please mention I should send the message directly from the dialog box to CChildView and how.

Thanks,
Kuphryn
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Define a method for your view and just call it from the dialog box.
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
From your main frame, you can get a pointer to your child frame using GetActiveFrame, and then get a pointer to the child view using GetActiveView. Remember to cast the pointers though, because they return the generic CFrameWnd and CView respectively.


Hope that helps,
:)atwl
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
Okay. Thanks.

First, I will consider GetActiveFrame() and GetActiveWindow. Is this similar to it?

CView *pMyView = reinterpret_cast<CView *>(reinterpret_cast<CMainFrame *>(GetActiveFrame())->GetActiveWindow());

Is the code above something like something you mentioned?

Second, what is the *conventional* way of handling the modeless dialog? In other words, should I send the message to MainFrm and then to MyView, or directly to MyView?

Modeless Dialog->MyMainFrm->MyView

or

Modeless Dialog->MyView

I was able to get the program to work according. The way I did it was by passing Create() a pointer to my CView class. So the modeless dialog contains a pointer to the CView at all time. I really do not want to do that because I try to make each class as independent as possible. It would be nice to have a way to "jump" around without having to pass pointers of a class object.

Kuphryn
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0


<< First, I will consider GetActiveFrame() and GetActiveWindow. Is this similar to it?
CView *pMyView = reinterpret_cast<CView *>(reinterpret_cast<CMainFrame *>(GetActiveFrame())->GetActiveWindow());
Is the code above something like something you mentioned?
>>


Not exactly what I had in mind... I was thinking something more in the line of:

CChildFrame* pChildFrm = NULL;
CMyView* pMyView = NULL;

pChildFrm = (CChildFrame*) GetActiveFrame();
if (pChildFrm) {
pMyView = (CMyView*) pChildFrm->GetActiveView();
}

Call me paranoid, but I always make it a point to verify all my pointers before using them. Makes the program more fault-tolerant. Worst thing to happen is for someone to use your program and have it crash because you didn't manage to handle errors gracefully.



<< Second, what is the *conventional* way of handling the modeless dialog? In other words, should I send the message to MainFrm and then to MyView, or directly to MyView? >>


Well, in my opinion there's really no conventional way to handle things. To me, I go with whichever method is most efficient. So, in my case, keeping a pointer to the view is a good idea as I can directly access the resource that I need instead of spending a few process cycles to pass a message around.


Hope that helps,
:)atwl
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
Okay. Thanks.

I will keep the technique I have been using from Richard Jones' Introduction to MFC Programming With Visual C++ (passing Create() a pointer).

Kuphryn