如何显示无模式对话框并立即在其中显示信息?
我想在屏幕上显示一个无模式对话框并在其中显示一些信息.
I want to show a modeless dialog on the screen and display some information in it.
但是,如果我按照以下方式使用它,它会出现一些问题:
However if I use it the following way, it has some problems:
function()
{
showdialog(XXX).
//heavy work.
update the dialog..
//heavy work.
update the dialog...
}
似乎显示了对话框,但它没有在其中绘制任何信息.仅在函数结束时绘制所有信息.
It seems the dialog displayed, but it does not draw any information in it. It only draw all information when the function is over.
如何修改无模式对话框,使其立即显示信息?
How can I modify the modeless dialog so it will display the information immediately?
推荐答案
你可以做一些事情.
(1) 您可以从 CDialog::OnInitDialog 方法中向对话框发布一条消息,然后在该发布消息的消息处理程序.这样,对话框将首先显示,然后长函数将运行.
(1) You could post the dialog a message from inside the CDialog::OnInitDialog method and then handle the long function in the message handler of that posted message. That way the dialog will first be displayed and then later the long function will get run.
(2) 第二个选项是确保消息循环获得一些处理时间.因此,如果您的长函数是某种循环,只需将偶尔调用添加到 ProcessMessages 以确保消息队列保持为空:
(2) The second option is to make sure the message loop gets some processing time. So if your long function is some sort of loop just add the occasional call to the ProcessMessages to make sure the message queue is kept empty:
void ProcessMessages()
{
MSG msg;
CWinApp* pApp = AfxGetApp();
while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
pApp->PumpMessage();
}
}
在这种情况下当然可以使用线程,但这样做并不总是没有风险和复杂性.
It certainly is possible to use threads is such a situation, but doing so is not always without risk and complexity.
使用带有 GUI 的线程意味着必须处理多个消息队列,这意味着使用像 PostThreadMessage 这样的 API,并且引入了一系列需要警惕的新问题.
Using threads with a GUI means having to deal with multiple message queues which then means using API's like PostThreadMessage and that introduces a new set of issues to be wary of.
有关此类问题的示例,请参阅此链接:
For an example of one such issue refer to this link:
http://msdn.microsoft.com/en-us/library/ms644946(VS.85).aspx
在哪里说:
PostThreadMessage 发送的消息是与窗口无关.作为一个一般规则,不是不能与窗口关联由 DispatchMessage 调度功能.因此,如果收件人线程处于模态循环中(由MessageBox 或 DialogBox),消息将丢失.拦截在模态中线程消息循环,使用线程特定的钩子.
Messages sent by PostThreadMessage are not associated with a window. As a general rule, messages that are not associated with a window cannot be dispatched by the DispatchMessage function. Therefore, if the recipient thread is in a modal loop (as used by MessageBox or DialogBox), the messages will be lost. To intercept thread messages while in a modal loop, use a thread-specific hook.
我在 Zeus IDE 中使用进程消息方法,它可以很好地确保 GUI 保持响应给用户.它还具有非常容易实现的优点.
I use the process message approach in the Zeus IDE and it works very well at making sure the GUI remains responsive to the user. It is also has the advantage of being very easy to implement.
相关文章