Click here to Skip to main content
12,752,876 members (38,286 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ MFC
I have CMainFrame class for my main window, and CChildView class for my view as generated by AppWizard. I have other handwritten classes, so I want to make my CMainFrame class as my center of operations. I intend to reroute the messages in the CChildView class MessageMap to CMainFrame class MessageMap. I've tried using SubclassWindow() function, but keep getting errors from some library files which i didnt even know exist (I'm new to MFC). Please I need help rerouting these messages. A pointer will be highly appreciated. Thanks.
Posted 26-Feb-13 3:56am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You do need to do anything of this sort. It is all done automatically. The message routing in MFC is designed that when the message received it is first sent to the main window (CMainFrame in your case). The main window asks all the children if they'd like to have a crack on the message (The route is similar to this: Main Window -> Active Child -> Document -> Back to main window).

So, if you don't create the message handlers in your child window, all the messages will come back to the main window.

If you create the message handlers via the Class Wizard in VS, you are able to select the window. Just select CMainFrame for all your messages and you are good.
Anthony Oyovwe 27-Feb-13 1:23am
@Andrew thanks but i've done a deep search and heavy reading on this before asking this question. Its only command messages (from menu and toolbar) that are routed that way. Windows messages (from buttons and edits) are sent to their owners which is the CChildView. I want to reroute this to mainFrm.
Andrew Cherednik 27-Feb-13 18:35pm
I think in this case you just need to create the control message handlers inside your views. Then, just use

CMainFrame* pMainFrame = reinterpret_cast<CMainFrame*>(theApp.GetMainWnd());

Something like this.
Anthony Oyovwe 2-Mar-13 9:13am
I wish i could get the implementation of CButton, maybe i'll be able to figure out how they transfer messages from CButton class to the CChildView class.
Anthony Oyovwe 3-Mar-13 10:00am
Can someone help with info about SubclassWindow() function. That is, where to put the code (MainFrame or CChildView), what to attach to the function (framwndObject.SubclassWindow( childViewObjectHandle )). please help. Thanks
Andrew Cherednik 3-Mar-13 19:06pm
The thing you want to achieve has nothing to do with sub-classing. All you need to do is to "hook" the WindowProc of the child view. To do so, in you mainframe, after you create the child views do the following:
WNDPROC wndproc = ViewProcInMainFrame;
_WndProc = (WNDPROC)SetWindowLongPtr(*pFrame, GWLP_WNDPROC, LONG_PTR(wndproc));
Where ViewProcInMainFrame is the function defined in the mainframe as follows:
LRESULT CALLBACK CMainFrame::ViewProcInMainFrame(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)

But, I think, after you implement this solution, you will realise that it is too much hassle.
Anthony Oyovwe 4-Mar-13 21:20pm
This means messages will be handled in the win32 api native way. Its going to be too much load for my level of knowledge for what i intend to do with it. But they (MSDN) say by using subclasswindow, messages can be rerouted to other classes. Thanks all thesame. Still searching.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100