Search code examples
c++user-interfacevisual-c++mfcentry-point

What is the entry point of this basic MFC Dialogue Box Application?


Starting out with GUI programming with C++. So, following some tutorials, I 'wrote' the following code to display a dialogue box. To be honest, the Visual Studio 2015 Wizard did most of the job, but here is the code file. It correctly displays the dialogue box pointed by the identifier, but I really cannot figure out how it works. To begin with, what is the entry point, of the code? There is not even a function, so what exactly executes when I build and run it?

#include<afxwin.h>
#include"resource.h"

class CExampleDlg :public CDialog
{
public:
    CExampleDlg():CDialog(IDD_EXAMPLE_DLG){}
    ~CExampleDlg(){}
};

class CExample:public CWinApp
{public:
    BOOL InitInstance()
    {
        CExampleDlg myDlg;
        m_pMainWnd = &myDlg;
        myDlg.DoModal();
        return TRUE;

    }
};
CExample MyApp;                                                                            

Solution

  • Unlike normal c/c++ application where the entry point is main and you have full control over the flow of execution. MFC applications are event driven. The code you write is executed based on the events that occur due to user interaction with the application like, clicking on the button, entering text in the text box etc. When there is no interaction the application sits idle.

    1) The best place to is OnInitDialog to place your initialization code. You can initialize all the member variables in OnInitDialog. (Remember winMain is the entry point for windows application. But in MFC this is embedded deep down in the boilerplate code.)

    2) Add message handlers to handle the user actions to execute your core logic later. For e.g.: If you have a button on the dialog then you need to add the message handler function for the button which will get invoked when the user clicks on that button. This can be done easily using the class wizard (https://msdn.microsoft.com/en-us/library/ee748520.aspx).