Closing console in Windows Applications


G

Giojo

Hello guys!
I can't resolve this problem.. I want my programm in c# working with
only console if there are some parameters, but if someone make
double-click on the exe I want to start the graphic GUI without the
black console on background..
Now I check if there are some parameters and if there aren't parameters
I run the GUI (with the black console that remain..). If I choose
Windows application on Visual Studio, when I run the application from
Console (with parameters) I lose all Console.Write messages... Have you
got a suggestion/trick?!
Thank you to all!
Giojo
 
Ad

Advertisements

F

Fitim Skenderi

Hi Gijo,

When you create your application create it as a console app, then on your
Main(..) function you can chec if your app does not have any arguments
create the form and start the app.

check the code below:

static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine("testing App");
Console.ReadLine();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

this works without any probs on my machine.

Fitim Skenderi
 
J

Jon Skeet [C# MVP]

Fitim said:
When you create your application create it as a console app, then on your
Main(..) function you can chec if your app does not have any arguments
create the form and start the app.

check the code below:

static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine("testing App");
Console.ReadLine();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

this works without any probs on my machine.
I've just tested it, and it certainly doesn't solve the OP's problem on
my box:

1) If you run it from the command line, the prompt doesn't return until
the form has been closed.
2) If you double-click on the exe file, you get a console box popping
up, which is exactly what the OP doesn't want.

Jon
 
A

AlexS

You can hide console window but will need PInvoke for this. Just send
relevant message with SendMessage to console window for which you can get
the handle using GetConsoleWindow.
If you want to close console before closing application, you can use Win32
API console allocation / deallocation functions, however this I did not
test - you might want to experiment with this. Check Platform SDK Console
functions sections.

I found so far only one glitch which I can't solve in Net 2.0 - when VS runs
application it leaves orphan cmd.exe after application closes. Problem is
manifesting itself only in VS 2005 environment, not in previous versions.

HTH
Alex
 
F

Fitim Skenderi

Hi,

Sorry I didn't read the message properly (assumed that console windows is
not a problem :).

One way is to use WinApi to hide the Console window (send a message to the
window to hide itself).

Another way would be to have two apps, one windows and one console. Then
another small console app that would call the appropriate app (start a new
process) by checking the arguments and closing itself.


Fitim Skenderi

Jon Skeet said:
Fitim said:
When you create your application create it as a console app, then on your
Main(..) function you can chec if your app does not have any arguments
create the form and start the app.

check the code below:

static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine("testing App");
Console.ReadLine();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

this works without any probs on my machine.
I've just tested it, and it certainly doesn't solve the OP's problem on
my box:

1) If you run it from the command line, the prompt doesn't return until
the form has been closed.
2) If you double-click on the exe file, you get a console box popping
up, which is exactly what the OP doesn't want.

Jon
 
A

Abra

You can hide console window but will need PInvoke for >>this. Just
send relevant message with SendMessage to >>console window for which you
can get the handle using GetConsoleWindow.

Can you please give some details (code sample) how could this be
achieved ?
Thanks in advance,
Abra
 
Ad

Advertisements

F

Francois Beaussier

Abra said:
Can you please give some details (code sample) how could this be
achieved ?
Hello,

I have not tested it but i think you can use the following:

private const Int32 SW_HIDE = 0;

[DllImport("Kernel32.dll")]
private static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

....

IntPtr hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_HIDE);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top