开发者

Is there any function called after the OnInitDialog function in MFC?

开发者 https://www.devze.com 2023-02-12 15:35 出处:网络
I want to create a thread after the creation of a dialog box in MFC. Is there any function that Windows has provid开发者_如何学Ced and is automatically called after OnInitDialog so that I can create m

I want to create a thread after the creation of a dialog box in MFC. Is there any function that Windows has provid开发者_如何学Ced and is automatically called after OnInitDialog so that I can create my thread inside it?


You can simply create your thread in the OnInitDialog function. There's no reason to overcomplicate things by going and searching for a different function, or splitting your initialization code up in two pieces. (There also isn't any such function, because there's no corresponding Windows message that is sent.)

If you want to get your dialog box on the screen before you create the thread, you can just show it manually using the ShowWindow function. For example:

ShowWindow(SW_SHOW);
RedrawWindow();

Also see this post by Raymond Chen: Waiting until the dialog box is displayed before doing something


OnInitDialog() is the main function called upon initialization (in reaction to WM_CREATE).

Why can't you create your thread in there?


I have changed the thread priority to below normal and when the thread executes for the first time I set the thread to normal priory. This works fine. Thanks for your response.


After many years of feeling unsatisifed with the OnTimer solution to draw first view graphics in an MFC dialog app (a favorite playground), this seemed like a nice simple solution:-

  1. Add a WM_HSCROLL handler with class wizard.
  2. At the end of OnInitDialog post a hscroll message with a NULL LPARAM
  3. In handler detect the NULL, draw graphics.

a timer meant that the app was alive for some time before graphics happened, and apparently hscroll is prioritized to happen just after the WM_PAINT message which would erase a picture element to its blank state, deleting anything that was drawn during initdialog.

BOOL CSpecDlg::OnInitDialog()
{
    ...

    PostMessage(WM_HSCROLL,0, (LPARAM)NULL);
    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSpecDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    // TODO: Add your message handler code here and/or call default
    if (pScrollBar==NULL)
    {
        plot();
    }
    CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消