【问题标题】:.Net Console Application in System tray系统托盘中的 .Net 控制台应用程序
【发布时间】:2010-10-19 14:42:02
【问题描述】:

有没有办法在最小化时将控制台应用程序放在系统托盘中?

【问题讨论】:

  • 不,不是骗子。这里讲的是控制台应用程序,而不是 winform 应用程序!
  • 实际上,我收回了欺骗的建议。这是关于控制台应用程序而不是 winforms 的问题
  • 您好,我也想要上面的解决方案...我已经尝试了“CLaRGe”建议到 FindWindow 功能...但由于 MSDN 缺少“...控制台在屏幕上”的具体示例,因此无法进一步进行,捕获最小化按钮单击并使用它来隐藏控制台窗口并更新通知图标。”任何人都可以建议有关如何捕获控制台最小化按钮单击并隐藏控制台窗口并更新通知图标的代码。任何建议将不胜感激。

标签: c# .net-3.5 console system-tray


【解决方案1】:
using System.Windows.Forms;
using System.Drawing;

static NotifyIcon notifyIcon = new NotifyIcon();
static bool Visible = true;
static void Main(string[] args)
{
    notifyIcon.DoubleClick += (s, e) =>
    {
        Visible = !Visible;
        SetConsoleWindowVisibility(Visible);
    };
    notifyIcon.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
    notifyIcon.Visible = true;
    notifyIcon.Text = Application.ProductName;

    var contextMenu = new ContextMenuStrip();
    contextMenu.Items.Add("Exit", null, (s, e) => { Application.Exit(); });
    notifyIcon.ContextMenuStrip = contextMenu;

    Console.WriteLine("Running!");

    // Standard message loop to catch click-events on notify icon
    // Code after this method will be running only after Application.Exit()
    Application.Run(); 

    notifyIcon.Visible = false;
}

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public static void SetConsoleWindowVisibility(bool visible)
{
    IntPtr hWnd = FindWindow(null, Console.Title);
    if (hWnd != IntPtr.Zero)
    {
        if (visible) ShowWindow(hWnd, 1); //1 = SW_SHOWNORMAL           
        else ShowWindow(hWnd, 0); //0 = SW_HIDE               
    }
}

【讨论】:

    【解决方案2】:

    我使用TrayRunner 正是为了这个目的。本质上,它包装了一个控制台应用程序,用于捕获所有输出。但是当最小化时,它会最小化到系统托盘而不是任务栏。您甚至可以自定义最小化时要显示的图标。我将它用于 Tomcat 或 Apache 之类的东西,以释放任务栏上的空间,而不将它们作为 Windows 服务运行。

    【讨论】:

    • Windows 10 是恶意软件。 :-) 至于 TrayRunner,我认为它只是旧的并且不符合微软的一些新要求。它肯定不是恶意软件。
    • 完成工作的最快方法。非常简单的工具。
    【解决方案3】:
    [DllImport("user32.dll")]
    internal static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);
    static Int32 WM_SYSCOMMAND = 0x0112;
    static Int32 SC_MINIMIZE = 0x0F020;
    
    static void Main(string[] args)
    {
        SendMessage(Process.GetCurrentProcess().MainWindowHandle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
    }
    

    【讨论】:

      【解决方案4】:

      是的,您可以这样做。 创建一个 Windows 窗体应用程序并添加一个NotifyIcon component

      然后使用以下方法(found on MSDN)分配并显示一个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);
      

      当您的控制台在屏幕上时,捕获最小化按钮单击并使用它来隐藏控制台窗口并更新通知图标。您可以使用以下方法 (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);
      

      当您准备好关闭应用程序时,请务必调用 FreeConsole。

      【讨论】:

        【解决方案5】:

        您不能隐藏控制台应用程序,因为它实际上没有要隐藏的窗口,看它是如何在控制台中运行的(控制台本身只是控制台的一个窗口,而不是在其中运行的应用程序)

        【讨论】:

          【解决方案6】:

          控制台没有可自行最小化的窗口。它在命令提示符窗口中运行。您可以挂钩窗口消息并在最小化时隐藏窗口。在您的应用程序中,可以像在 Windows 应用程序中一样添加托盘图标。嗯,不知何故这闻起来...

          但是:我不确定你为什么要这样做。控制台应用程序的设计不同于 Windows 应用程序。因此,也许可以将应用程序更改为 Windows 窗体应用程序?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-20
            • 2013-06-22
            • 1970-01-01
            • 1970-01-01
            • 2012-09-16
            • 1970-01-01
            • 2011-05-22
            • 1970-01-01
            相关资源
            最近更新 更多