How can I show a modeless dialog and display information in it immediately?(如何显示无模式对话框并立即在其中显示信息?)
问题描述
我想在屏幕上显示一个无模式对话框并在其中显示一些信息.
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.
这篇关于如何显示无模式对话框并立即在其中显示信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何显示无模式对话框并立即在其中显示信息?
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01