Hide the console.

I just started using WinApi and have a nice blank window, but a console shows up in the background. Is there a way to hide it?


I found this but it's all msvc... I'm using dev
http://www.cplusplus.com/forum/beginner/9452/
Use WinMain() instead of main() for your program's entry point:

1
2
3
4
5
6
7
8
9
#include <windows.h>

int WINAPI WinMain(HINSTANCE inst,HINSTANCE prev,LPSTR cmd,int show)
{
  // program starts here
  return 0;
}

// int main()  <--- do not have a main() anywhere in your program 


That + make sure your project settings are set so that you build a "Win32" program and not a "Console" program. I don't know exactly what the setting looks like in Dev-C++.

Do those two things and you shouldn't get a console window popping up.
or just use the function to remove thje console ;)...
i don't have main anywhere, and i started with a blank page, no project at all.


what function?
AllocConsole() -> FindWindow() -> ShowWindow() ?
or SetConsoleTitle() -> FindWindow() -> ShowWindow() ?
Last edited on
As mentioned here:
http://www.cplusplus.com/forum/beginner/12001/

but no that does not work... It will compile but won't actually hide the console
If you have a console window (and unless you are an experienced programmer who intened to have a console window) then you are almost certainly NOT building a Win32 application. If you are using VS 2008, you need to choose the Win32 interace as Disch showed you above.

Read line 5 of the first post (including whitespace)
secondly I may find a use for the console.


MSVC i kind of evil in my opinion, it does a lot of stuff behind the lines...
and yes to those of you who start everything from the console i know you have even more control
(duoas)
Ahh, you want to ShowWindow( GetConsoleWindow(), SW_HIDE );
See here for more http://www.cplusplus.com/forum/beginner/12001/

The GetConsoleWindow() function only works on Windows 2000 and higher. If you want something that will work anywhere, you can scroll down in the topic I just linked for you to find an alternate version (which I called "MyGetConsoleWindow()").

Hope this helps.
i don't think you're a new programmer (2nd reply)
http://www.cplusplus.com/forum/lounge/12709/

look at the 5th reply (not counting the original question)
i even tried MyGetConsoleWindow() written by you... (and like madmaxsanta GetConsoleWindow()
won't compile for me)
You've lost me.

mms got his GetConsoleWindow() working just fine.
See http://www.cplusplus.com/forum/beginner/12001/page1.html#msg58547

The code in http://www.cplusplus.com/forum/beginner/12001/page1.html#msg58357 works for all versions of windows. Cut and paste entirely then use GetConsoleWindow() normally.

Keep in mind that you must be using C++ for it to try loading the kernel32 version first. If you are using plain C, use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <time.h>
#include <windows.h>

#if _WIN32_WINNT < 0x0501

HWND WINAPI GetConsoleWindow()
  {
  HWND result;
  char save_title[ 1000 ];
  char temp_title[ 50 ];
  time_t t = time( NULL );
  lstrcpyA( temp_title, "hypercube1 " );
  lstrcatA( temp_title, ctime( &t ) );

  GetConsoleTitleA( save_title, sizeof( save_title ) );
  SetConsoleTitleA( temp_title );
  result = FindWindowA( NULL, temp_title );
  SetConsoleTitleA( save_title );

  return result;
  }

#endif 

BTW, when cut-n-pasting this code over and checking it against any C++ constructs, I noticed that I failed to use the ANSI versions of the Windows Console functions... If you are compiling with UNICODE #defined you must make sure to make the same adjustments for it to work. I'll fix my other post too.

Good luck!
I mean besides it working for him. On that I redirected you to you say that users don't read squat and that new programmers are essentially users. When you failed to see that I had already been there I said that weren't new and should have read that (I realize that that post was referring to code and GUI's but...) Just pokin' fun at ya'.
Now for something relevant
you're code compiles and runs fine but the console is still there.
It may be prudent to post my entire code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <time.h>
#include <windows.h>

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,WPARAM wParam, LPARAM lParam);

HWND WINAPI GetConsoleWindow();  

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow)
{
    LPCTSTR ClsName = "BasicApp";/////////
    LPSTR WndName = "My Window";///////
	
    MSG        Msg;
	HWND       hWnd;
	HDC        hdc;
	WNDCLASSEX WndClsEx;

	// Create the application window
	WndClsEx.cbSize        = sizeof(WNDCLASSEX);
	WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
	WndClsEx.lpfnWndProc   = WndProcedure;
	WndClsEx.cbClsExtra    = 0;
	WndClsEx.cbWndExtra    = 0;
	WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
	WndClsEx.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);/////
	WndClsEx.lpszMenuName  = NULL;
	WndClsEx.lpszClassName = ClsName;
	WndClsEx.hInstance     = hInstance;
	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	// Register the application
	RegisterClassEx(&WndClsEx);

	// Create the window object
	hWnd = CreateWindow(ClsName,
			  WndName,
			  WS_OVERLAPPEDWINDOW,
			  100,
			  120,
			  640,
			  480,
			  NULL,
			  NULL,
			  hInstance,
			  NULL);
	
	// Find out if the window was created
	if( !hWnd ) // If the window was not created,
		return 0; // stop the application

	// Display the window to the user
	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);
	
    //put code here
    ShowWindow( GetConsoleWindow(), SW_HIDE );//////

	// Decode and treat the messages
	// as long as the application is running
	while( GetMessage(&Msg, NULL, 0, 0) )
	{
             TranslateMessage(&Msg);
             DispatchMessage(&Msg);
	}

	return Msg.wParam;
	

}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    // If the user wants to close the application
    case WM_DESTROY:
        // then close it
        PostQuitMessage(WM_QUIT);
        break;
    default:
        // Process the left-over messages
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    // If something was not done, let it go
    return 0;
}

HWND WINAPI GetConsoleWindow()
  {
  HWND result;
  char save_title[ 1000 ];
  char temp_title[ 50 ];
  time_t t = time( NULL );
  lstrcpyA( temp_title, "hypercube1 " );
  lstrcatA( temp_title, ctime( &t ) );

  GetConsoleTitleA( save_title, sizeof( save_title ) );
  SetConsoleTitleA( temp_title );
  result = FindWindowA( NULL, temp_title );
  SetConsoleTitleA( save_title );

  return result;
}
Syntax Fixes

Line 12 should read

12 LPCSTR WndName = "My Window";///////

Line 16 declares an unused variable.


Console Confusion

Line 58 isn't really necessary -- you've written a GUI application and there shouldn't be any console window at all. As it is, your application is a console-less application -- it doesn't have a console.

If you are actually launching the program from a console window -- the program itself will not destroy the initial console -- it is a separate process and should not affect the originating console window.


Adding a Conole

You are using C++ style comments so I compiled with g++:

g++ -Wall -ansi -pedantic -mwindows a.cpp

It compiles just as well in C mode (using gcc and C-style comments).

However, since you're using C++, the following link is the one for you.

Adding Console I/O to a Win32 GUI App
http://www.halcyon.com/~ast/dload/guicon.htm

Unfortunately, the code on that page is horribly unformatted, so you may want to run it through the nearest pretty printer to make it more readable. However, in all essentials it is correct and will give your GUI application a console window that you can hide and show and cout and cin text with.


Hope this helps.

PS. Fun is good. :-) I often enough don't pay much attention to whom I am actually responding... alas.
I am starting it from the dev ide, which tells me the program or ide is creating the console. Closing either of the two windows automatically closes the other, they affect each other. I will look at some the dev settings and see if it is making the console.
I was going to use HDC but have not added it's code yet.
I haven't used Dev-C++, but many here have -- and may be able to tell you better than what I am about to say.

I suspect that it is the IDE that is starting the console. I don't know how the processes are linked from the Dev-C++ processes... but again I suspect that the IDE is simply waiting on either process to terminate and simply telling the other to quit.

But at least you know it is nothing you have to do to get rid of the console window... Your program works just fine as-is.

:-)
Did I missunderstood or do u still get an Conosle while executing the Code above? O_o...

As for me i did create an normal windows application creating my standard window and then did AllocConsole to Create an COnosle for working with status messages and background server controls bla blubb...
Yes Duoas you are right, and Incubbus that's fine but I'd like to be able to get rid of it whenever I want so...

For other dev user's
Tools-> Compiler Options-> Settings-> Linker
then change the fourth box to yes.

But you have to change it back for you're normal console apps.
Topic archived. No new replies allowed.