Does anyone know how to disable the close button on a windows console window with an .exe
executable that was created from a C program?
Asked
Active
Viewed 1,166 times
4

Boann
- 48,794
- 16
- 117
- 146

user1590836
- 51
- 6
-
2You can't. The console window is a separate component. The only way is to build GUI program where you have control of your own windows. – Mysticial Aug 17 '12 at 08:07
-
Also, why would you want to do this? – Mysticial Aug 17 '12 at 08:08
-
i just want to make a prank to my brother. but i have to make sure that he will freak out. dou you know any other way that i can use? by the way the only programming language i konw is c, unfortunately – user1590836 Aug 17 '12 at 08:12
-
@Mysticial it might be separate component, but as long as you can get a window handle to it, you can do plenty to it. – weston Aug 17 '12 at 08:15
-
@Mysticial Aside from pranks, I can think of situations were I have had to run something in a console window at a client and I don't want them to close it accidentally. – weston Aug 17 '12 at 08:19
-
Close button? I don't have such a thing when I run my executables. Perhaps you are missing to provide us with crucial information about your programming environment? – Jens Gustedt Aug 17 '12 at 08:36
-
@JensGustedt He means the Console Window close button. People's edits to the question have made it unclear. – weston Aug 17 '12 at 08:43
-
@weston: What is a "Console Window close button" :) this is a system specific question, but the system is mentioned nowhere. And no, it haven't been the edits. – Jens Gustedt Aug 17 '12 at 09:05
-
@JensGustedt fair point, have added windows to tags for him (awaiting peer review) – weston Aug 17 '12 at 09:12
-
2Can't believe people are still up voting the comment that says you can't do this. See the answer below people! – weston Aug 17 '12 at 14:53
2 Answers
4
From here:
#define _WIN32_WINNT 0x0500
#include <stdio.h>
#include <windows.h>
int main(int argc, _TCHAR* argv[]){
HWND h;
HMENU sm;
int i, j, c;
LPTSTR buf;
// get the handle to the console
h = GetConsoleWindow();
// get handle to the System Menu
sm = GetSystemMenu(h, 0);
// how many items are there?
c = GetMenuItemCount(sm);
j = -1;
buf = (TCHAR*) malloc (256 *sizeof(TCHAR));
for (i=0; i<c; i++) {
// find the one we want
GetMenuString(sm, i, buf, 255, MF_BYPOSITION);
if (!strcmp(buf, "&Close")) {
j = i;
break;
}
}
// if found, remove that menu item
if (j >= 0)
RemoveMenu(sm, j, MF_BYPOSITION);
return 0;
}

weston
- 54,145
- 21
- 145
- 203
-
I'm not setup to test this, so +1 assuming it works. I didn't know you could grab the handle to the console window like that. – Mysticial Aug 17 '12 at 08:17
-
didn't work, try this on online compiler: cmpe150-1.cmpe.boun.edu.tr/ – user1590836 Aug 17 '12 at 08:17
-
1
-
@user1590836 well it worked for me on that online compiler, nice link, thanks. Don't double click the exe, run console window first and then run the exe. – weston Aug 17 '12 at 08:23
-
1@user1590836 If it works, you can accept this as the answer. As you're new take a moment to look at the [faq](http://stackoverflow.com/faq) and welcome to Stack Overflow! – weston Aug 17 '12 at 08:32
0
If you want to disable a button in a running program there are methods to do this.
The principle is to find the window, then find the button inside the window and then send a WM_DISABLE message to the button.

AndersK
- 35,813
- 6
- 60
- 86