Click here to Skip to main content
13,455,264 members (54,795 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Close(X) button is not working in the dialog? When pressing the X button in the dialog, it is not closing the dialog and also added command button in the dialog, added codes for cancel, still the dialog is not getting closed.
Posted 5-May-11 20:25pm
Updated 5-May-11 21:09pm
Niklas Lindquist 6-May-11 2:34am
Is this MFC or plain Win32?
Gokulnath007 6-May-11 2:39am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Do you have a handler for WM_CLOSE? If you don't call CDialog::OnClose() this will prevent it from closing.
SAKryukov 6-May-11 7:18am
Sure it wouldn't close. A 5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You don't need to handle WM_CLOSE,
Did you overload a OnCancel()fuction?
If there isn't, you have to overload OnCancel() function and add this code


You can do as bellow.
First you create button named IDCANCEL.
and from IDCANCEL, define OnCancel()function.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Above mentioned solutions are answers to your question, i am just adding to it..

Whenever you start with a MFC Dialog based application.
You are given a dialog with two buttons OK & CANCEL.
Clicking either of them when your program is executed will exit the application.
Many times we don't need them & so we delete them, then the problem would be that your application will exit even on clicking a ENTER or ESC key from keyboard.

So you can do the following
1. Double click both the OK & CANCEL button, you will have two function created for ID_OK & ID_CANCEL (assumed to be Ids of your OK & CANCEL button).
2. Commenting OnOK() in the function created for ID_OK will prevent your application from exiting when an ENTER key is hit when your application is running.
3. In the function created for ID_CANCEL you can call a message box asking user whether to exit the application or not & based on the input act accordingly.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It always close the window unless you have artificially overridden this behavior or simply screwed up. I have no information to tell you how.

Niklas Lindquist 6-May-11 4:08am
Nothing wrong with this answer, and it's perfectly valid. Fixing the score.
SAKryukov 6-May-11 7:15am
Thank you, Niklas.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04-2016 | 2.8.180321.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100