How can I show a modeless dialogue and display information in it right away?

I want to show a modeless dialog box on the screen and display some information in it.

However, if I use it as follows, it has some problems:

function()
{
showdialog(XXX).
//heavy work.
update the dialog..
//heavy work.
update the dialog...
}

It seems that the dialog is being displayed, but there is no information in it. It only draws all the information when the function is complete.

How do I change the modeling dialog so that it displays information right away?

+5
source share
4 answers

There are a few things you can do.

(1). CDialog:: OnInitDialog, . , , .

(2) - , . , , ProcessMessages, , :

void ProcessMessages()
{
    MSG msg;
    CWinApp* pApp = AfxGetApp();
    while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
    {
        pApp->PumpMessage();
    }
}

: , - , .

, API, PostThreadMessage, , .

:

http://msdn.microsoft.com/en-us/library/ms644946(VS.85).aspx

:

, PostThreadMessage, . , , , . , ( MessageBox DialogBox), . loop, .

Zeus IDE, , , . , .

+5

OnInitDialog . , .

ProcessMessages :

  • , .

  • . ProcessMessages , - .

:

#define WM_NEW_COUNT (WM_USER + 0x101)

BEGIN_MESSAGE_MAP()
    ON_MESSAGE(WM_NEW_COUNT, OnNewCount)
END_MESSAGE_MAP()

BOOL CMyDialog::OnInitDialog()
{
    CWinThread* pThread = AfxBeginThread(MyCountFunc, this->GetSafeHwnd());
    return TRUE;
}

LRESULT CMyDialog::OnNewCount(WPARAM wParam, LPARAM)
{
    int newCount = (int)wParam;

    // m_count is DDX member, e.g. for label
    m_count = newCount;

    UpdateData(FALSE);

    return 0;
}

:

UINT MyCountFunc(LPVOID lParam)
{
    HWND hDialogWnd = (HWND)lParam;

    for (int i=0; i < 100; ++i)
    {
        PostMessage(hDialogWnd, WM_NEW_COUNT, i, NULL);
    }
}
+4

, . , . ProcessMessage() , IMO . : 1) OnInitDialog() 2) , - . , .

, , , .

+2
source

Do not try to do your hard work right away. Have a message in the dialog box in the range WM_APP in OnInitDialog. The WM_APP handler can do some of the hard work, then do another PostMessage message and return. Thus, you allow the message pump to process window messages between your pieces of processing.

+1
source

All Articles