Execute a piece of code immediately after the dialog box is displayed.

Source: Internet
Author: User

 

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;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.