Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
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
Updated 21-Jan-22 3:05am
Comments
Niklas L 6-May-11 2:34am    
Is this MFC or plain Win32?
Gokulnath007 6-May-11 2:39am    
MFC

Do you have a handler for WM_CLOSE? If you don't call CDialog::OnClose() this will prevent it from closing.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-May-11 7:18am    
Sure it wouldn't close. A 5.
--SA
Hello.
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

CDialog::OnCancel();

You can do as bellow.
First you create button named IDCANCEL.
and from IDCANCEL, define OnCancel()function.
 
Share this answer
 
v2
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.
 
Share this answer
 
It always close the window unless you have artificially overridden this behavior or simply screwed up. I have no information to tell you how.

—SA
 
Share this answer
 
Comments
Niklas L 6-May-11 4:08am    
Nothing wrong with this answer, and it's perfectly valid. Fixing the score.
Sergey Alexandrovich Kryukov 6-May-11 7:15am    
Thank you, Niklas.
--SA
I've had this problem because I disabled the "cancel" button from the dialog window.
 
Share this answer
 

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900