Recently I was working on a project. When creating a subdialog box, the initialization was slow due to the large amount of code to be executed during OnInitDialog () initialization, it takes about 2 seconds for the dialog box to be displayed... so I want to display some code in the dialog box and then execute it. I found a lot of information on the Internet and summarized the following two methods. One is to set a timer in OnInitDilalog, in OnInitDialog (), PostMessage () reads the message Execution Code from the Message Queue after adding a custom message in the message queue waiting for the initialization of the dialog box. ,,,,
1. Create a timer only once before OnInitDialog returns, and execute code in OnTimer
BOOLMyAppDlg: OnInitDialog ()
{
..............................
// TODO: Add extra initialization here
SetTimer (m_nTimerID, 100, NULL );
ReturnTRUE; // return TRUE unless you set the focus to a control
}
VoidMyAppDlg: OnTimer (UINT_PTRnIDEvent)
{
If (nIDEvent = m_nTimerID)
{
// Execute the command only once, so Kill the command at the first time.
KillTimer (m_nTimerID );
M_nTimerID = 0;
AfxMessageBox (_ T ("Hello MFC "));
Return;
}
CDialog: OnTimer (nIDEvent );
}
2. post a custom message before OnInitDialog is returned, and execute the code in the corresponding function of the message.
# Define WM_MY_PRIVATE wm_user+ 1551
BOOLMyAppDlg: OnInitDialog ()
{
..............................
// TODO: Add extra initialization here
PostMessage (WM_MY_PRIVATE );
ReturnTRUE; // return TRUE unless you set the focus to a control
}
// Of course, you must add one in the message ing table
// ON_MESSAGE (WM_MY_PRIVATE, & CdummyDlg: OnPrivateMessage)
LRESULTMyAppDlg: OnPrivateMessage (WPARAM, LPARAM)
{
AfxMessageBox (_ T ("Hello MFC "));
Return0;
}