Discussion:
PostMessage from dialog to View
(too old to reply)
j***@eudoramail.com
2005-07-21 23:30:13 UTC
Permalink
I'm using the standard VC++ MFC MDI app. I have a modeless window that
I launch from the MainFrm and I would like to have an event (say a
button click or a list view item click) fire off an event in the View
class.

Is this possible? I've try using the automatic Add event handler
(choosing the view class to associate the create event to) but it does
not work. I compiles and does not cause an error, but the event is not
fired.

Any help into this would be greatly appreciated.

Joshua
Nishant Sivakumar
2005-07-22 02:01:48 UTC
Permalink
You can use User-Defined Handlers (indexed topic on MSDN). Define a custom
message (WM_APP+n) and have a handler for it in your view class. The dialog
just needs to PostMessage this custom message to the view.
--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com
http://blog.voidnish.com
Post by j***@eudoramail.com
I'm using the standard VC++ MFC MDI app. I have a modeless window that
I launch from the MainFrm and I would like to have an event (say a
button click or a list view item click) fire off an event in the View
class.
Is this possible? I've try using the automatic Add event handler
(choosing the view class to associate the create event to) but it does
not work. I compiles and does not cause an error, but the event is not
fired.
Any help into this would be greatly appreciated.
Joshua
Ajay Kalra
2005-07-22 14:11:05 UTC
Permalink
How do you know that event is not fired? Since you are using MDI, you
need to be specific to which view will catch the event. What do you
mean by event here? Are you using a custom message?

---------
Ajay Kalra
***@yahoo.com
j***@eudoramail.com
2005-07-22 16:21:11 UTC
Permalink
Ok, I tried the PostMessage option. Basically I declared a message in

stdafx.h
---------
#define WM_YOURE_HUNGRY WM_USER + 1

And then in the dialog I had an button click event on a d separate
dialog and in that function I called:

PostMessage(WM_YOURE_HUNGRY);

And in the View I had the recieving code in the message mapping
portion:

ON_MESSAGE( WM_YOURE_HUNGRY, OnHunger)

and the function OnHunger is defined as follows:

LRESULT CDialogTestsView::OnHunger( WPARAM wparam, LPARAM lparam)
{
MessageBox("You made it");
return 1;
}

But it doesn't get to the view (the messagebox is never fired off). So
I'm at a loss.

j
Post by Ajay Kalra
How do you know that event is not fired? Since you are using MDI, you
need to be specific to which view will catch the event. What do you
mean by event here? Are you using a custom message?
---------
Ajay Kalra
Ajay Kalra
2005-07-22 16:40:59 UTC
Permalink
Why would it go to the view? You are posting it to the dialog. You need
to pass the pointer to the specific view (unless you want it to go to
the active view where you can use GetActiveView) to the dialog and then
use

pMyView->PostMessage(WM_YOURMESSAGE);

If you think by posting the message to the dialog will route it to the
view, that is incorrect.

---------
Ajay Kalra
***@yahoo.com
j***@eudoramail.com
2005-07-22 17:11:17 UTC
Permalink
Ok so in the main application class when the mainframe is initialized I
assign it to a global variable that I can access. So in my dialog I'm
able to access the mainframe:

theApp.j_pMainFrame->GetActiveView()->PostMessage(WM_YOURE_HUNGRY);

Ok. So far so good; but when this line is called the application
crashes.

Is this not what you were referring to? Is there something I'm missing
(obviously)?

J
Post by Ajay Kalra
Why would it go to the view? You are posting it to the dialog. You need
to pass the pointer to the specific view (unless you want it to go to
the active view where you can use GetActiveView) to the dialog and then
use
pMyView->PostMessage(WM_YOURMESSAGE);
If you think by posting the message to the dialog will route it to the
view, that is incorrect.
---------
Ajay Kalra
j***@eudoramail.com
2005-07-22 17:22:47 UTC
Permalink
Does it matter that when I'm clicking the button (in the modeless
dialog) the GetActiveView() returns null. Is this because the modeless
dialog is the active window and the view is no longer active?
Ajay Kalra
2005-07-22 17:42:12 UTC
Permalink
Mainframe object can be accessed using GetActiveMainWnd();

If you want to post the message to the active view, you can do the
folowing:

CMainFrame* pMainFrame = (CMainFrame *) GetMainWnd();
ASSERT_VALID(pMainFrame);
ASSERT_VALID(pMainFrame->GetActiveView());
pMainFrame->GetActiveView()->PostMessage(..);

-------
Ajay Kalra
***@yahoo.com
j***@eudoramail.com
2005-07-22 17:53:15 UTC
Permalink
On this line:

CMainFrame* pMainFrame = (CMainFrame *) GetMainWnd();

I get the following error:

error C3861: 'GetMainWnd': identifier not found, even with
argument-dependent lookup

??
Post by Ajay Kalra
Mainframe object can be accessed using GetActiveMainWnd();
If you want to post the message to the active view, you can do the
CMainFrame* pMainFrame = (CMainFrame *) GetMainWnd();
ASSERT_VALID(pMainFrame);
ASSERT_VALID(pMainFrame->GetActiveView());
pMainFrame->GetActiveView()->PostMessage(..);
-------
Ajay Kalra
Ajay Kalra
2005-07-22 18:13:32 UTC
Permalink
I am sorry, it should be AfxGetMainWnd().

-----------
Ajay Kalra
***@yahoo.com
j***@eudoramail.com
2005-07-22 18:19:12 UTC
Permalink
Thank you for the clarification. But the same thing is happening.
When I get to this line:

pMainFrame->GetActiveView()->PostMessage(..);

The application crashes. It crashes because:

pMainFrame->GetActiveView() == NULL

Because I'm trying to do this from a modeless dialog. So I'm not sure
if that nullifies the ActiveView?

???

Thank you,

J
Post by Ajay Kalra
I am sorry, it should be AfxGetMainWnd().
-----------
Ajay Kalra
Ajay Kalra
2005-07-22 18:23:23 UTC
Permalink
See my earlier post about using GetActiveFrame() as this is MDI.

---------
Ajay Kalra
***@yahoo.com
j***@eudoramail.com
2005-07-22 19:26:20 UTC
Permalink
Yeah, so I figured this out. Because I'm trying to do this from a
dialog in modeless form; whenever I try and do the PostMessage() to the
ActiveView it doesn't work because the ActiveView is null.

So the solution is to declare a global variable

CView* m_vwCurrentView;

Every time the view is changed I modify this global variable. So now
from my dialog I do the PostMessate from m_vwCurrentView and it works
fine.

Thank you very much,

j
Ajay Kalra
2005-07-22 19:34:16 UTC
Permalink
You shouldnt have to keep track of the view if all you want is the
active view. As mentioned in the link above,
GetActiveFrame()->GetActiveView() would do exactly what you want. In
addition, global variables is not the way to go.

----------
Ajay Kalra
***@yahoo.com
j***@eudoramail.com
2005-07-22 19:48:39 UTC
Permalink
Ok. I see what you mean know. I don't know why that wasn't working
before. I shut my project down and then opened it back up and tried
this and it worked fine. So no more global variable. Thank you

J
Post by Ajay Kalra
You shouldnt have to keep track of the view if all you want is the
active view. As mentioned in the link above,
GetActiveFrame()->GetActiveView() would do exactly what you want. In
addition, global variables is not the way to go.
----------
Ajay Kalra
Ajay Kalra
2005-07-22 18:15:29 UTC
Permalink
In addition, since this is MDI, you will need to call GetActiveFrame on
mainframe as shown here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cframewnd.3a3a.getactiveview.asp

---------------
Ajay Kalra
***@yahoo.com
Loading...