Click here to Skip to main content
15,921,646 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
In a managed C++ console application,
how to simply show or hide the console window ?

Best regards.
Posted

So, I found alone the solution.

In a console C++/CLI application, create a small Utilities class :
Utilities.h :
C#
public ref class Utilities abstract sealed {/* abstract sealed = static */

public:
    static void showConsole(
        bool show );
    };


Utilities.cpp :

XML
//-------------------------------------
/** Utilities.cpp
    */
#include "stdafx.h"
#include "Utilities.h"
#include <windows.h>
void Utilities::showConsole(
	bool show ) {	

	HWND hWnd = GetConsoleWindow();
	if (hWnd != 0) {		
		ShowWindow( hWnd, show ? SW_SHOW : SW_HIDE);
		}
	}


And don't forget to check in link option "Other dependencies" "herit of default parameter of parent project" (and then suppress keyword $(NOINHERIT), in order to access to windows API of ShowWindow.
It works perfectly.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 9-Feb-12 13:16pm    
Great that you found out! 5+
I was almost certain it wouldn't work. I'm glad you were able to correct my view on this. :thumbsup:
I tried the following code :

C++
#include "stdafx.h"
#include
 
void Utilities::showConsole(
    bool show ) {
 
    HWND hWnd = GetConsoleWindow();
    if (hWnd != 0) {
        ShowWindow( hWnd, show ? SW_SHOW : SW_HIDE);
        }
    }


GetConsoleWindow() is resolved at link step, but ShowWindow() is not.
How to solve that ?
 
Share this answer
 
Comments
Richard MacCutchan 9-Feb-12 7:21am    
Correction: you can use ShowWindow() from within a console application, if the correct libraries are include - see my response below.
Foulques NERA 9-Feb-12 7:42am    
Sorry, but I do that perfectly in visual C6 on industrial operating applications.
Richard MacCutchan 9-Feb-12 8:47am    
I stand corrected, it does work. You need to include the correct system libraries in your link phase. I have the following list in my build:
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
comdlg32.lib
advapi32.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
odbc32.lib
odbccp32.lib
I do not think you need them all, so I suggest adding them one at a time until you get a clean link completed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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