如何在 WinApi/MFC 中设计更清晰的 PostMessage 链

发布于 2024-10-02 17:04:39 字数 552 浏览 0 评论 0原文

我有一个具有多个框架的 MFC GUI 应用程序(有点像 Outlook 的主窗口行为,以及在新框架中创建的消息/约会窗口,或者像 Skype 那样的多框架同步),并且我需要 PostMessage malloc'ed 数据通过窗口层次结构。

比方说,我得到了字符串,_wcsdup 它,PostMessage(WM_NEWSTRING, ...),并且层次结构深处的控件对其进行处理,或者如果没有订阅者,则消息将被清除。

我现在正在研究的是,所有消息都发布到应用程序线程,线程找到活动框架或最适合的框架,将消息传递给它,框架将消息传递给它的视图,视图将消息传递给子视图等等,如果没有视图来处理该消息,则该消息将被释放。

问题是这些链接命令写起来相当累人,因为我必须在每个 CWnd 类中复制消息转发代码。同时,资源清理是相当令人不愉快的,因为如果没有窗口来捕获消息,就必须有人调用 free。因此,简单地将消息发布到主消息泵而不进行额外处理,希望有人总是能捕获它,并不是一种有效的方法。 PostMessage 返回 S_OK,没有人认为该消息是可处理的,并且它处于悬空状态。

对于我正在寻找的东西,是否有更好、正确的方法?

I have a MFC GUI app that has multiple frames (sort of like Outlook'ish behavior with main window, and message/appointment windows being created in new frames, or Skype like multi frame syncronization), and I need to PostMessage malloc'ed data through the window hierarchy.

Say, I get the string, _wcsdup it, PostMessage(WM_NEWSTRING, ...), and, the control somewhere deep down the hierarchy processes it, or if there are no subscribers, the message get's cleaned.

What I am looking into now, is that all messages are posted to application thread, thread finds the active frame or best fit frame, passes the message to it, frame passes the message to it's view, the view passes message to subview and so on, if there is no view to process the message, it get's free'd.

The problem is that these chaining commands are pretty tiring to write, as I have to duplicate the message forwarding code in each CWnd class I have. At the same time, resource cleanup is pretty unpleasant, because if there is no window to catch the message, someone has to call the free. Therefore simply posting the message to the main message pump without extra handling, hoping that someone will always catch it, is not a valid approach. PostMessage returns S_OK, no one sees the message as processable, and it's left dangling.

Is there a better, correct approach for what I'm looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度° 2024-10-09 17:04:39

我永远不会像你描述的那样使用 PostMessage 。我的解决方案通常涉及握手:

// From CNotifierBlah
PostMesssage(hWnd, UWM_NEW_DATA, 0, 0);
//
LRESULT CDestinationWnd::OnNewData(WPARAM wParam, LPARAM lParam)
{
    CNewData newData = GetNotifierBlah().GetNewData(); // Thread-safe getter!
}

观察者模式几乎相同。

I would never use PostMessage as you describe. My solution often involves a hand-shake:

// From CNotifierBlah
PostMesssage(hWnd, UWM_NEW_DATA, 0, 0);
//
LRESULT CDestinationWnd::OnNewData(WPARAM wParam, LPARAM lParam)
{
    CNewData newData = GetNotifierBlah().GetNewData(); // Thread-safe getter!
}

Pretty much the same as Observer pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文