Search code examples
c++debuggingeventsmfcribbon

Visual C++ Learning MFC


I'm currently in the process in the learning of MFC, first of all, do anyone of you know know a site where it is explained where each single event is explained, e.g. When you click New in the ribbon, this event is triggered there.

And in fact this is my question, when you click file in the ribbon, and then click new(from the template of mfc), where is it executed, because i can't find it?

Thank You


Solution

  • I recommend Scot Wingo's book MFC Internals. The book describes the message routing mechanism of MFC. It may be a little dated, but it shows where to go looking in the MFC source files that ship with the compiler. In a nutshell,

    1) Clicking the File-New button results in a message getting posted to your application 2) Eventually AfxInternalPumpMessage() calls GetMessage(), calls AfxInternalPreTranslateMessage(), and maybe TranslateMessage() and DispatchMessage() 3) Eventually the message makes its way to AfxWindowProc(), AfxCallWindowProc(), and the virtual WindowPrc() of your CFrame derived window (assuming ribbon, toolbar, or menu generated command). 4) Eventually the window proc figures out it is a WM_COMMAND message and calls OnCmdMsg(). 5) Command routing of MFC allows the view, document, frame window, and application to have a crack at processing the message.

    To handle the message, the CCmdTarget derived object (CView, CDocument, CFrameWnd, CWinApp)-derived have to have message maps BEGIN_MESSAGE_MAP(classname)/END_MESSAGE_MAP() with an entry of ON_COMMAND(ID_FILE_NEW, CommandHandlerFunc) whre CommandHandlerFunc is the name of a function in the derived class which is a void return type with void arguments.

    BEGIN_MESSAGE_MAP(CMyApp)
       ON_COMMAND(ID_FILE_NEW, OnFileNew)
    END_MESSAGE_MAP()