I have this console app on C# code example. This code working fine. But I have a problem when I already get app from the tray. If I press MINIMIZE button the app does not put it in the tray again.
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static void Main(string[] args)
{
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
var notifyIcon = new NotifyIcon();
using (
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Namespace.pngegg.ico"))
{
notifyIcon.Icon = new Icon(stream);
}
notifyIcon.Text = "Some text";
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(5000, "Same text", "Same text", ToolTipIcon.Info);
notifyIcon.MouseClick += NotifyIcon_MouseClick;
Console.CancelKeyPress += Console_CancelKeyPress;
Application.Run();
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Console.WriteLine("Some text...");
Environment.Exit(0);
}
private static void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
{
try
{
var handle = GetConsoleWindow();
ShowWindow(handle, SW_SHOW);
}
catch (Exception ex)
{
}
}
But, I can`t hide the console in the tray the second time (after showing the window from the tray) by pressing MINIMIZE button on the window console. This is possible in console app C#? How I can hide the console app in the tray again?