Click here to Skip to main content
15,891,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

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.
 
Share this answer
 
Comments
Tonex247 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.
chaau 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());
pMainFrame->OnView1Button1Clicked();

Something like this.
Tonex247 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.
Tonex247 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
chaau 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900