views:

1911

answers:

4

Is there a way I can put a console application in the system tray when minimizing ?

I use .Net 3.5 with c#

+5  A: 

A console has no window to minimize by itself. It runs in a command prompt window. You might hook the window messages and hide the window on minimize. In your application it's possible to add a tray icon just the same as you would do it in a windows application. Well, somehow this smells...

But: I'm not sure why you want to do this. A console application is by design different to a windows application. Hence, maybe it's an option to change the app to be a windows form application?

Sascha
A: 

You can't hide a console application because it does not actually have a window to hide, seeing as how it is running in the console (the console itself is just a window of the console, not the app running in it)

TheTXI
+6  A: 

Yes, you can do this. Create a Windows Forms application and add a NotifyIcon component.

Then use the following methods (found on MSDN) to allocate and display a Console

[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();

[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();

[DllImport("kernel32.dll")]
public static extern Boolean AttachConsole(Int32 ProcessId);

When your console is onscreen, capture the minimize button click and use it to hide the console window and update the Notify icon. You can find your window using the following methods (found on MSDN):

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

// Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
// Also consider whether you're being lazy or not.
[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

Be sure to call FreeConsole whenever you're ready to close the app.

CLaRGe
A: 

Hello,

I too want the solution for above...i have tried "CLaRGe" suggestion upto FindWindow function...but couldnt proceed further as MSDN lacks specific examples on "... console is onscreen, capture the minimize button click and use it to hide the console window and update the Notify icon.."

Could anyone suggest with code on how to capture the console minimize button click and hide the console window and update the notify icon. Any suggestions would be greatly appreciated.