Qt程序运行在托盘,用FindWindow查找该窗口并显示该窗口出现空白的解决方案
·
在外部程序里,先用 FindWindow 拿到句柄,调用 ShowWindow(hwnd, SW_MAXIMIZE)。然后,不要立刻结束,而是向这个句柄发送一个目标 Qt 程序能识别的自定义消息。
在目标 Qt 程序的 nativeEvent 里处理这个消息,在里面调用 update() 或 repaint():
// 目标 Qt 程序 MainWindow 中
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) override {
MSG *msg = static_cast<MSG*>(message);
if (msg->message == WM_APP + 200) { // 约定一个自定义消息号
QTimer::singleShot(0, this, [this]() {
this->update(); // 或 layout()->activate()
});
*result = 0;
return true;
}
return QMainWindow::nativeEvent(eventType, message, result);
}
外部程序在 ShowWindow 之后,调用 PostMessage(hwnd, WM_APP + 200, 0, 0)。这样刷新动作就回到了 Qt 自己的事件循环里。
更多推荐



所有评论(0)