fattumsdad
asked on
Disable Close Button (X) in console program
Good afternoon!
How do I disable the min, max, and close buttons in a console program?
Regards,
Tony
How do I disable the min, max, and close buttons in a console program?
Regards,
Tony
Um, MF_BYPOSISTION should of course be MF_BYPOSITION :-(
ASKER
Jk,
This is what I have for code so far....
#include <windows.h>
#include <stdio.h>
#include <conio.h>
int main()
{
TCHAR szOldTitle[MAX_PATH];
TCHAR szNewTitle[MAX_PATH];
if( GetConsoleTitle(szOldTitle , MAX_PATH) )
{
wsprintf(szNewTitle, TEXT("Testing"), szOldTitle);
HMENU hMenu = GetSystemMenu ( hwndConsole, FALSE)
DeleteMenu(hMenu, 6, MF_BYPOSITION);
if( !SetConsoleTitle(szNewTitl e) )
printf("SetConsoleTitle failed (%d)\n", GetLastError());
getch();
}
}
The errors I get:
--------------------Config uration: Test - Debug--------------------
Compiling source file(s)...
Test.cpp
Test.cpp: In function `int main()':
Test.cpp:13: error: `hwndConsole' undeclared (first use this function)
Test.cpp:13: error: (Each undeclared identifier is reported only once for each
function it appears in.)
Test.cpp:14: error: syntax error before `(' token
Test.cpp:13: warning: unused variable `HMENU__*hMenu'
Test.exe - 3 error(s), 1 warning(s)
This is what I have for code so far....
#include <windows.h>
#include <stdio.h>
#include <conio.h>
int main()
{
TCHAR szOldTitle[MAX_PATH];
TCHAR szNewTitle[MAX_PATH];
if( GetConsoleTitle(szOldTitle
{
wsprintf(szNewTitle, TEXT("Testing"), szOldTitle);
HMENU hMenu = GetSystemMenu ( hwndConsole, FALSE)
DeleteMenu(hMenu, 6, MF_BYPOSITION);
if( !SetConsoleTitle(szNewTitl
printf("SetConsoleTitle failed (%d)\n", GetLastError());
getch();
}
}
The errors I get:
--------------------Config
Compiling source file(s)...
Test.cpp
Test.cpp: In function `int main()':
Test.cpp:13: error: `hwndConsole' undeclared (first use this function)
Test.cpp:13: error: (Each undeclared identifier is reported only once for each
function it appears in.)
Test.cpp:14: error: syntax error before `(' token
Test.cpp:13: warning: unused variable `HMENU__*hMenu'
Test.exe - 3 error(s), 1 warning(s)
You need to use the code from the MSKB article above to retrieve the console's HWND, e.g.
#include <windows.h>
#include <stdio.h>
#include <conio.h>
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSI ZE]; // Contains fabricated
// WindowTitle.
char pszOldWindowTitle[MY_BUFSI ZE]; // Contains original
// WindowTitle.
// Fetch current window title.
GetConsoleTitle(pszOldWind owTitle, MY_BUFSIZE);
// Format a "unique" NewWindowTitle.
wsprintf(pszNewWindowTitle ,"%d/%d",
GetTickCount(),
GetCurrentProcessId());
// Change current window title.
SetConsoleTitle(pszNewWind owTitle);
// Ensure window title has been updated.
Sleep(40);
// Look for NewWindowTitle.
hwndFound=FindWindow(NULL, pszNewWindowTitle);
// Restore original window title.
SetConsoleTitle(pszOldWind owTitle);
return(hwndFound);
}
int main ()
{
HWND hwndConsole = GetConsoleHwnd ();
HMENU hMenu = GetSystemMenu ( hwndConsole, FALSE)
DeleteMenu(hMenu, 6, MF_BYPOSITION);
getch ();
return 0;
}
#include <windows.h>
#include <stdio.h>
#include <conio.h>
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSI
// WindowTitle.
char pszOldWindowTitle[MY_BUFSI
// WindowTitle.
// Fetch current window title.
GetConsoleTitle(pszOldWind
// Format a "unique" NewWindowTitle.
wsprintf(pszNewWindowTitle
GetTickCount(),
GetCurrentProcessId());
// Change current window title.
SetConsoleTitle(pszNewWind
// Ensure window title has been updated.
Sleep(40);
// Look for NewWindowTitle.
hwndFound=FindWindow(NULL,
// Restore original window title.
SetConsoleTitle(pszOldWind
return(hwndFound);
}
int main ()
{
HWND hwndConsole = GetConsoleHwnd ();
HMENU hMenu = GetSystemMenu ( hwndConsole, FALSE)
DeleteMenu(hMenu, 6, MF_BYPOSITION);
getch ();
return 0;
}
ASKER
Ok, I put the code in exactly as stated above. Down to one error, one warning:
Compiling source file(s)...
Test.cpp
Test.cpp: In function `int main()':
Test.cpp:50: error: syntax error before `(' token
Test.cpp:49: warning: unused variable `HMENU__*hMenu'
Test.exe - 1 error(s), 1 warning(s)
I appreciate your patience.... I think you've walked me through a good 50 things over the past 2 days :)
Compiling source file(s)...
Test.cpp
Test.cpp: In function `int main()':
Test.cpp:50: error: syntax error before `(' token
Test.cpp:49: warning: unused variable `HMENU__*hMenu'
Test.exe - 1 error(s), 1 warning(s)
I appreciate your patience.... I think you've walked me through a good 50 things over the past 2 days :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Just added that semi, worked great. How do I link with user32.lib? Thanks again for all the help... I really do appreciate it. And sorry about the console LOL.
Well, if the linker does not complain about any missing symbols, you are already linking with that lib. I just compiled on the command line, so I had to add that manually...
ASKER
Just curious.... how long have you been doing this stuff?
Earning my living with coding in C/C++ for well over 10 years now :o)
ASKER
Well, that definitely shows. I can't stress enough how much I appreciate the patience and help you've given! Thanks again! I'm sure I'll have another question in a couple hours or so LOL.
Then, you need to get the system menu handle:
HWND hwndConsole = ...;
HMENU hMenu = GetSystemMenu ( hwndConsole, FALSE);
After that, you need to delete the Close Menu Item from the Console Application System Menu:
DeleteMenu ( hMenu, 6, MF_BYPOSISTION); // 'Close'
Also, the others:
DeleteMenu ( hMenu, 5, MF_BYPOSISTION); // 'Minimize'
DeleteMenu ( hMenu, 4, MF_BYPOSISTION); // 'Maximize'
This will affect the buttons also.
(adapted from http://support.microsoft.com/default.aspx?scid=kb;en-us;818361 -- "HOW TO: Disable the Close Button on the Title Bar of a Console Application by Using Microsoft Visual Basic .NET")