当两个按钮映射时 CDialog 未关闭
Visual Studio 2005、C++、Windows XP。
我有一个带有单个按钮的 CDialog,它调用如下函数:
BEGIN_MESSAGE_MAP(Foo, BaseDlg) //BaseDlg inherits from CDialog ON_BN_CLICKED(IDBAR, Bar) END_MESSAGE_MAP()
单击“X”时对话框将关闭。
我将上面的代码更改为:
BEGIN_MESSAGE_MAP(Foo, BaseDlg) //BaseDlg inherits from CDialog ON_BN_CLICKED(IDBAR, Bar) ON_BN_CLICKED(IDBAZ, Baz) END_MESSAGE_MAP()
我的对话框窗口将不再关闭。 每当单击 X 时,就会调用 Baz()。 由于某种原因,第二个 ON_BN_CLICKED() 处理程序正在替换正常的对话框关闭行为。
如何关闭具有两个或多个映射到功能的按钮的对话框?
Visual Studio 2005, C++, Windows XP.
I have a CDialog with a single button, which calls a function like so:
BEGIN_MESSAGE_MAP(Foo, BaseDlg) //BaseDlg inherits from CDialog ON_BN_CLICKED(IDBAR, Bar) END_MESSAGE_MAP()
The dialog box closes when its 'X' is clicked.
I change the above code to:
BEGIN_MESSAGE_MAP(Foo, BaseDlg) //BaseDlg inherits from CDialog ON_BN_CLICKED(IDBAR, Bar) ON_BN_CLICKED(IDBAZ, Baz) END_MESSAGE_MAP()
My dialog window will no longer close. Whenever the X is clicked, Baz() is called. The second ON_BN_CLICKED() handler is replacing the normal dialog close behavior for some reason.
How do I close a dialog box which has two or more buttons mapped to functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
资源文件中分配给 IDBAR 和 IDBAZ 的数值是什么? 一种可能的解释是 IDBAZ == IDCANCEL,它在 MFC 中默认定义为对话框的取消按钮和 X 按钮的 ID。 为该常量定义您自己的处理程序将覆盖默认行为,即关闭窗口。 这仅适用于您以模态方式显示对话框的情况 - 如果它是无模式的,那么您始终必须通过调用 EndDialog() 自行关闭对话框。
What are the numeric values assigned to IDBAR and IDBAZ in the resource file? One possible explanation is that IDBAZ == IDCANCEL, which is defined in MFC by default as the ID for both the dialog's cancel and X buttons. Defining your own handler for this constant will then override the default behaviour, which is to close the window. This only applies though if you're showing the dialog modally - if it's modeless then you always have to close the dialog yourself by calling EndDialog().