为什么将文本添加到 RichEdit 窗口会冻结它

why adding text to RichEdit window freezes it?

本文关键字:窗口 冻结 RichEdit 文本 添加 为什么      更新时间:2023-10-16

直到我用鼠标触摸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,则系统会负责同步该消息。

底线是必须及时抽取消息队列。

找到答案这是我修改后的代码,看看PeekMessageWDispatchMessageW

#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);
  }
}