?
Solved

Disable Close Button (X) in console program

Posted on 2004-09-01
11
Medium Priority
?
1,163 Views
Last Modified: 2007-12-19
Good afternoon!

How do I disable the min, max, and close buttons in a console program?  

Regards,
Tony
0
Comment
Question by:fattumsdad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11956108
You first need to obtain a window handle for your console: http://support.microsoft.com/default.aspx?scid=kb;en-us;124103 ("How To Obtain a Console Window Handle (HWND)")

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")

0
 
LVL 86

Expert Comment

by:jkr
ID: 11956131
Um, MF_BYPOSISTION should of course be MF_BYPOSITION :-(
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 11956389
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(szNewTitle) )
         printf("SetConsoleTitle failed (%d)\n", GetLastError());

      getch();
   }
}

The errors I get:
--------------------Configuration: 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)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:jkr
ID: 11956438
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_BUFSIZE]; // Contains fabricated
                                           // WindowTitle.
       char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
                                           // WindowTitle.

       // Fetch current window title.

       GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

       // Format a "unique" NewWindowTitle.

       wsprintf(pszNewWindowTitle,"%d/%d",
                   GetTickCount(),
                   GetCurrentProcessId());

       // Change current window title.

       SetConsoleTitle(pszNewWindowTitle);

       // Ensure window title has been updated.

       Sleep(40);

       // Look for NewWindowTitle.

       hwndFound=FindWindow(NULL, pszNewWindowTitle);

       // Restore original window title.

       SetConsoleTitle(pszOldWindowTitle);

       return(hwndFound);
   }

int main ()
{

    HWND hwndConsole = GetConsoleHwnd ();

      HMENU hMenu = GetSystemMenu ( hwndConsole, FALSE)
      DeleteMenu(hMenu, 6, MF_BYPOSITION);

    getch ();

    return 0;
}
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 11956503
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 :)
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 11956572
Sorry, there's a semicolon missing, that should be

    HMENU hMenu = GetSystemMenu ( hwndConsole, FALSE); // <---

BTW, you need to link with user32.lib

PS: That test just ruined one of my consoles :o)
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 11956588
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11956625
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...
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 11956729
Just curious....  how long have you been doing this stuff?
0
 
LVL 86

Expert Comment

by:jkr
ID: 11956773
Earning my living with coding in C/C++ for well over 10 years now :o)
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 11956796
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.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question