拦截CDialog创建
我有一个相当大的应用程序,它显示许多不同的 MFC CDialog 派生对话框窗口。所有对话框都是从与此类似的中心函数显示的:
void ShowDialog(CDialog& dlg)
{
dlg.DoModal();
}
现在我基本上需要在每个对话框的 OnInitDialog 方法中调用一个函数。从技术上讲,它不需要位于 OnInitDialog 内,但最好在对话框可见之前。
强力方法是遍历代码并找到最后一个对话框,并将函数调用添加到 OnInitDialog 方法(如果有,则添加一个)。但似乎必须有一种更优雅的方法...
请注意,dlg 实际上并不是 CDialog,而是从它派生的东西。
有什么想法、技巧或窍门吗?我不打算修补消息映射,但希望找到更干净/更安全的东西。
I have a rather large app that displays many different MFC CDialog-derived dialog windows. All of the dialogs are displayed from a central function that is similar to this:
void ShowDialog(CDialog& dlg)
{
dlg.DoModal();
}
Now I need to essentially call a function in every dialog's OnInitDialog method. It doesn't technically need to be within OnInitDialog, but preferably before the dialog is visible.
The brute force method would be to go through the code and find every last dialog and add the function call to the OnInitDialog method (if it has one, and if it doesn't, add one). But it seems like there must be a more elegant way...
Note that dlg is not actually a CDialog, but something that derives from it.
Any thoughts, tricks or hacks? I'm not above patching the message map, but hope to find something cleaner/safer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的所有对话框都有一个共同的祖先(您似乎暗示您拥有),那么您可以简单地将代码放在该共同祖先中您选择的合适位置。例如,
OnInitDialog()
是虚拟的。If you've got a common ancestor for all your dialogs, which you seem to imply you have, then you can simply put the code in that common ancestor in a suitable location of your choice. For example
OnInitDialog()
is virtual.事实证明这很容易做到:
可能不是高性能应用程序要做的事情,但对于简单的 GUI 来说它工作得很好。无需更改其他代码。
Turns out it is quite easy to do:
Probably not the thing to do for a high performance app, but for something that is a simple GUI it works perfectly. No other code changes required.