I have a Windows Forms app with Console playing a part. I now run an almost debugged code from an exec that is generated after the Build command. I need to prevent Console from accidentally being closed by clicking on the tiny button located at the right upper corner. I cannot figure out if it is possible to do.
The corresponding button on the form is blocked but it does not prevent the form from being closed by clicking on the button on the Console. I want to immobilize it too. I do not the app being closed accidentally in any event.
Thanks.

Blocking Console closure?
bunkscene
Neo the 1
dcoder_85
Alex, are you using System.Diagnostics.Process to set up and start the console application The only thing I can think of, aside from Figo's suggestion to hook the message loop for cmd.exe, is to create the process with ProcessStartInfo.CreateNoWindow set to true, and redirect stdout back to the spawning application using an anonymous pipe. You would then have the console process running windowless, with the output coming back so you can display it in a textbox or do whatever else you want to with it. This KB article gives an example of how to do it in unmanaged code.
http://support.microsoft.com/default.aspx scid=kb;en-us;190351
Daniel Gary
Mark, thank you. No I am using this statement: Application.Run(new Form1());
Also it is not a Console app. It is a Windows Forms application. I invoke Console in the form_load event.
It is an interesting and appealing idea you are suggesting. I may take a shot at it. I have done similar things before: have some experience.
Larry Menard
Thank you, Figo, for all your suggestions. I will work on them. May come back with new questions.
JediDanny
No, it does not help. I do need the Console. I get some information at runtime, short messages from various threads. I may not need it in the future but I do not know for sure.
Thanks.
Joseph Stalin
What i meant is if you want to hide the close button of the form you'd set the form's controlbox.
And about console, I think you'd get the handler of it, then modify it, but it is likely to be done in C++ instead of C#. That's my idea.
RichardMtl
Using textbox is not an option. It won't work--it is not thread safe.
Jeff Levinson
Hi
is it necessary for the console to be shown
you could just hide the entire window...
Hope this helps, please close the thread if it does
AmirAlis
Figo, setting the form's ControlBox to false had no effect on the close button of the Console. Sorry. I think there is no connection between them.
Thanks.
younger
Hi, Alex
If you want to hide the X button at right top on the form. Just set the form.controlbox to false. And about the console, you can start it in another thread and try to hide it by backgroundworker, here is the reference: http://msdn2.microsoft.com/en-us/library/hybbz6ke(VS.80).aspx
http://www.codeproject.com/csharp/winconsole.asp df=100&forumid=16021&exp=0&select=1458374
Otherwise, you can choose other ways, anyway, it is just my idea.
Thank you
Jim Karr
SteveDee
I do not want to hide the console. I want to see it all the time. I read those messages. It is a very dynamic situation when the app runs. I want to prevent it from accidentally shutting the whole app down.
Jesse Towner
This should go on top of your code in the form:
[
DllImport ( "kernel32.dll", SetLastError = true )][
return: MarshalAs ( UnmanagedType.Bool )] static extern bool AllocConsole ( );[
DllImport ( "kernel32.dll", SetLastError = true )][
return: MarshalAs ( UnmanagedType.Bool )] static extern bool FreeConsole ( );Then in form1_load:
AllocConsole
( );