如何在“传统MFC代码”中的事件处理程序中添加回调?
这是使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MFC message Maps Maps中的函数签名和返回值是固定的。您必须遵循协议;它没有提供任何自定义点。如果
on_bn_clicked
原型必须遵守以下签名,其不接受或返回任何值。唯一可用的状态是从消息映射条目中暗示的(即
onbnClickedButton
,每当对话框的子控制器以cmfcapplicationdlg
表示IDIDC_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 signatureIt 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 byCMFCApplicationDlg
with IDIDC_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.