如何在“传统MFC代码”中的事件处理程序中添加回调?

发布于 2025-02-07 13:35:41 字数 437 浏览 5 评论 0原文

这是使用MFC的旧代码实现的玩具。 OnBnClickedButton是一个事件处理程序,但它包含在其他线程中异步执行的代码(可能是一个坏主意)。声明语法被消息图接受。

//declaration
afx_msg void OnBnClickedButton(); 
//message map
ON_BN_CLICKED(IDC_BUTTON, &CMFCApplicationDlg::OnBnClickedButton)

现在,我想在事件处理程序中添加回调,但是消息映射不接受新的声明语法,该语法在哪里?

afx_msg void OnBnClickedButton(std::optional<std::function<CString(void)>> callback); 

This is a toy implementation of a legacy code using MFC. OnBnClickedButton is an event handler but it contains codes which are executed asynchronously in a different thread ( may be a bad idea). The declaration syntax is accepted by the message map.

//declaration
afx_msg void OnBnClickedButton(); 
//message map
ON_BN_CLICKED(IDC_BUTTON, &CMFCApplicationDlg::OnBnClickedButton)

Now I want to add a callback to the event handler like so, but the message-map won't accept the new declaration syntax, where to go?

afx_msg void OnBnClickedButton(std::optional<std::function<CString(void)>> callback); 

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

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

发布评论

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

评论(1

撞了怀 2025-02-14 13:35:41

MFC message Maps Maps中的函数签名和返回值是固定的。您必须遵循协议;它没有提供任何自定义点。如果on_bn_clicked 原型必须遵守以下签名,

afx_msg void memberFxn();

其不接受或返回任何值。唯一可用的状态是从消息映射条目中暗示的(即onbnClickedButton,每当对话框的子控制器以cmfcapplicationdlg表示ID IDC_BUTTON 单击)。

在实施onbnClickedButton时,您可以随意执行任何您喜欢的事情,例如查询其他信息(例如,来自类实例或线程 - 本地存储中存储的数据),明确或旋转线程 MFC使用C ++ 20 Coroutines等。

特别是,它没有为异步操作提供任何支持。这是您必须实施的事情。

The function signatures and return values for entries in MFC message maps are fixed. You have to follow the protocol; it doesn't offer any customization points. In case of the ON_BN_CLICKED button handler the prototype must abide to the following signature

afx_msg void memberFxn();

It doesn't accept or return any values. The only state available is that implied from the message map entry (i.e. OnBnClickedButton is called whenever the child control of the dialog represented by CMFCApplicationDlg with ID IDC_BUTTON is clicked).

In your implementation of OnBnClickedButton you are free to do whatever you like, such as querying for additional information (e.g. from data stored in the class instance or thread-local storage), spinning up threads, either explicitly or using C++20 coroutines, etc.

MFC doesn't help you with any of that, specifically it doesn't provide any sort of support for asynchronous operations. That's something you will have to implement yourself.

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