为什么将文本添加到 RichEdit 窗口会冻结它
why adding text to RichEdit window freezes it?
直到我用鼠标触摸richedit窗口,它的内容是实时更新的,但是将鼠标悬停在它上面会变成箭头变成沙漏光标。然后,窗口不会对按标题栏移动它的三次或四次后续尝试做出反应。当它最终对鼠标拖动做出反应时,它会正常移动,但停止刷新其内容,标题栏变为空。类似的效果是当我尝试单击窗口的工作区时。这次在没有反应窗口的点击几下后也停止更新,其标题栏变为(无响应)。
当循环最终停止时,程序会返回窗口更新并返回"活动"。在工作区更新时,该怎么做才能操作窗口(并查看它正在更新内容)?
#include <windows.h>
#include <sstream>
int main() {
using namespace std;
LoadLibrary("Msftedit.dll");
HWND richeditWindow = CreateWindowExW (
WS_EX_TOPMOST,
L"RICHEDIT50W",
L"window text",
WS_SYSMENU | WS_VSCROLL | ES_MULTILINE | ES_NOHIDESEL | WS_VISIBLE,
50, 50, 500, 500,
NULL, NULL, NULL, NULL
);
for (int i = 0 ; i<100000; i++) {
wstringstream wss;
wss << i << L", ";
SendMessageW(richeditWindow, EM_REPLACESEL, FALSE, (LPARAM) wss.str().c_str());
}
MSG msg;
while( GetMessageW( &msg, richeditWindow, 0, 0 ) ) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
您正在紧密循环中填充丰富的编辑窗口,而不是为消息队列提供服务。 除非您的进程定期处理其消息队列,否则系统会认为您的应用已停止响应。好吧,它已经停止响应了!
为了保持应用程序的响应速度,必须抽取消息队列。我真的不知道你的真正程序想做什么。如果您想将该文本放入丰富的编辑中,则可以使用一条EM_REPLACESEL
消息来实现。
如果您确实有一个长时间运行的任务,那么它属于不同的线程。然后,您必须处理同步回 GUI 线程的问题。如果您所做的只是调用SendMessage,则系统会负责同步该消息。
底线是必须及时抽取消息队列。
找到答案这是我修改后的代码,看看PeekMessageW
和DispatchMessageW
。
#include <windows.h>
#include <iostream>
#include <sstream>
int main() {
using namespace std;
LoadLibrary("Msftedit.dll");
HWND richeditWindow = CreateWindowExW (
WS_EX_TOPMOST,
L"RICHEDIT50W",
L"window text",
WS_SYSMENU | WS_VSCROLL | ES_MULTILINE | ES_NOHIDESEL | WS_VISIBLE,
50, 50, 500, 500,
NULL, NULL, NULL, NULL
);
MSG msg;
for (int i = 0 ; i<100000; i++) {
wstringstream wss;
wss << i << L", ";
SendMessageW(richeditWindow, EM_REPLACESEL, FALSE, (LPARAM) wss.str().c_str());
if (PeekMessageW(&msg, richeditWindow, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
while( GetMessageW( &msg, richeditWindow, 0, 0 ) ) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
相关文章:
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 问:如何使用C++中的按钮从窗口打开窗口
- SDL 窗口不会弹出
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如何在cpp文件之间切换窗口?在Qt中
- QuadTree只在窗口的右上角绘制
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 如何在C++中找到active directory中禁用和锁定的窗口帐户
- 处理闪烁窗口事件
- 延迟函数使打开的框架窗口冻结,直到指定的时间过去
- SDL:窗口冻结
- QQuickView窗口在调整鼠标大小时冻结
- FLTK 窗口冻结不一致
- 如何解决从客户端接收消息时窗口冻结问题(无响应)
- Win32 API:打开对话框窗口后应用程序冻结
- Qt -主窗口在运行时冻结
- 如果在父窗口调整大小期间打开,子窗口将冻结
- 为什么将文本添加到 RichEdit 窗口会冻结它
- 如何在 MFC 中连续运行不冻结窗口的函数?
- Win32窗口冻结后,第一次绘制(directx 11)