views:

192

answers:

1

I have a Windows Forms application which opens a console using AllocConsole. It works fine, but the problem is when I close the Console by clicking the "X"-Button, the entire application exits too, but I want that only the Console Window should close and the Application should continue as usual. Is there any way to do that?

+1  A: 

There is a way to programmatically hide the window that you've allocated - use FreeConsole.

The X button on the alloc'd console works like the X button on your WinForm - it will tickle the Form_Closing() event I guess.

You'd need some logic in that event handler to decide whether to actually close the form or not. One possibility: grab the mouse position and see if it is within the bounding box for the Form. If it is, then conclude that the Form's X was clicked, and close the app. If the mouse is NOT within the form, then conclude that the console X was clicked, and call FreeConsole.

I haven't tried this.

Cheeso
Doesn't work. The Form_Closing event does not get triggered by the console window. I've tried the Application.ApplicationExit event too, but it's not possible to cancel that event.
raphaelr
What do you really want to do, anyway? The console that can be opened this way is pretty rudimentary, I always thought it was mostly useful for diagnostics or debugging. Is this really something you want to expose in your app's UI ?
Cheeso