Computing.Net > Forums > Programming > C++ disable console close button.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ disable console close button.

Reply to Message Icon

Name: Judago
Date: August 15, 2009 at 06:11:15 Pacific
OS: Windows XP
CPU/Ram: Athlon xp x2 64 5600 / 2gb
Product: Home built / K9a2 cf-f
Subcategory: C/C++
Comment:

I'm writing a win32 (well 2000 and above) console app and want
to disable the close button, the reason being that I want
to be sure all data entered is saved before exit. I'm using
Dev-c++ 4.9.9.2.

The below code is what is giving me trouble, it disables the
x on the top corner but the alt+spacebar/click on top left hand
corner menu reactivates it along with having close on the
menu it's self active:

#define _WIN32_WINNT 0x0500
#include <windows.h>
int main() {
    EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
    DrawMenuBar(GetConsoleWindow());
    system ("pause");
}

Simply changing "EnableMenuItem" to "RemoveMenu" does work but
since it's a console app I want to restore the default menus,
size, colour, ect. on exit.

GetSystemMenu(GetConsoleWindow(), TRUE);

Comes close to restoring it but it's not the default console
menu, just a generic menu.


Can anyone please point me to either a way to disable close
properly or even restore the default console menu when I'm
finished?



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: August 17, 2009 at 20:55:18 Pacific
Reply:

Have you tried just calling FreeConsole() and AllocConsole() to spawn your own console window? That way, you don't have to worry about resetting the menu, nor hooking the console.


0

Response Number 2
Name: Judago
Date: August 18, 2009 at 01:01:14 Pacific
Reply:

Thanks Razor2.3, I didn't even know about FreeConsole() and AllocConsole(). It works perfectly.

Now I can just remove the close command as their will always be some sort of a custom field based on getch() that detects esc or alt+f4.


Thanks again.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ disable console close button.

how to disable close button in vb www.computing.net/answers/programming/how-to-disable-close-button-in-vb/4243.html

Min, Max and Close buttons www.computing.net/answers/programming/min-max-and-close-buttons/5793.html

close button, property sheet in vc+ www.computing.net/answers/programming/close-button-property-sheet-in-vc/3903.html