tags:

views:

211

answers:

1

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?

+3  A: 

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().

Stu Mackellar