From: Tom Serface on
I only post messages to "main" if they have to do with something going in
the main window. Otherwise, I'd be more likely to post to a view or a
non-modal dialog or whoever is supposed to do something with the message
(assuming it's GUI related).

Tom

"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
news:v1c3f5t5j75350lvp64u3hpdm5c8h06848(a)4ax.com...
>I find that the main window is convenient but only under certain restricted
>scenarios. I
> have also discovered that many people post messages to the main window in
> inappropriate
> situations and end up spending a lot of effort figuring out what to do
> next, and often
> this involves having the main window call methods of a document or view,
> which is really
> bad structure. So it is necessary to be careful about how the messaging
> is structured.
> joe
>
> On Wed, 4 Nov 2009 07:56:21 -0800, "Tom Serface" <tom(a)camaswood.com>
> wrote:
>
>>I typically send messages to my mainframe since it *is* a window. You can
>>call functions in the main app code by using AfxGetApp().
>>
>>Tom
>>

From: David Ching on
"Tom Serface" <tom(a)camaswood.com> wrote in message
news:F01B3457-0771-4C81-B69B-A69870932EB7(a)microsoft.com...
> I only post messages to "main" if they have to do with something going in
> the main window. Otherwise, I'd be more likely to post to a view or a
> non-modal dialog or whoever is supposed to do something with the message
> (assuming it's GUI related).
>

The problem with posting/sending to either the MainFrame or a View is that
it is hard for the posting app to find the window using the classname, since
MFC names these unpredictably. So I create my own top-level hidden window
with a well known classname such as MYAPP_HIDDEN_WINDOW_GUID_xxxxx which
other apps can easily do a FindWindow() to get. Of course, they could
always broadcast a registered window message to all windows and not worry
about finding mine, but that has always seemed wasteful when only my own app
needs to be notified (and not every window on the system).

-- David


From: Joseph M. Newcomer on
Generally, I never use FindWindow to do this for the reason you point out: you don't know
its class name.

You can always find the main window from
AfxGetApp()->m_pMainWnd
(note that in the main GUI thread, AfxGetMainWnd() [I think that's the name of the
function] does this, but it is not reliable in a secondary thread, while
AfxGetApp()->m_pMainWnd is always reliable.

For views, I explicitly pass in the CWnd* of the view to whatever is supposed to be
sending/posting the message, so there's never a question.

For cross-process messaging, your solution is a real good idea; I always tell my students
that if you broadcast more that one message per run of your program, you are costing
serious system resources. What I usually do is
::PostMessage(HWND_BROADCAST, UWM_HERE_I_AM, (WPARAM)m_hWnd, 0);
or something remarkably similar; yes, it does end up going to every window, but only the
window(s) that recognize the message can respond. Note that the WPARAM in this case is
the window that I want to use to establish communication, so the recipient(s) will respond
with a
::PostMessage((HWND)wParam, UWM_I_SAW_YOU, (WPARAM)m_hWnd, 0);
that is, post back their actual window handle. Now both sender and receiver know each
other's actual window handles, and nobody ever needs to broadcast again. This is
particularly useful for multi-client or multi-server situations where I can have multiple
instances running.

It was not clear from the context of the question if cross-process messaging is involved
at all. Within an app, you should always be told what window to communicate with as part
of the protocol of establishing the "connection" (e.g., passing a window handle in to a
DLL, or as part of the input to a thread).
joe

On Sun, 8 Nov 2009 11:18:27 -0800, "David Ching" <dc(a)remove-this.dcsoft.com> wrote:

>"Tom Serface" <tom(a)camaswood.com> wrote in message
>news:F01B3457-0771-4C81-B69B-A69870932EB7(a)microsoft.com...
>> I only post messages to "main" if they have to do with something going in
>> the main window. Otherwise, I'd be more likely to post to a view or a
>> non-modal dialog or whoever is supposed to do something with the message
>> (assuming it's GUI related).
>>
>
>The problem with posting/sending to either the MainFrame or a View is that
>it is hard for the posting app to find the window using the classname, since
>MFC names these unpredictably. So I create my own top-level hidden window
>with a well known classname such as MYAPP_HIDDEN_WINDOW_GUID_xxxxx which
>other apps can easily do a FindWindow() to get. Of course, they could
>always broadcast a registered window message to all windows and not worry
>about finding mine, but that has always seemed wasteful when only my own app
>needs to be notified (and not every window on the system).
>
>-- David
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm