62

将 WinForms 应用程序最小化到系统托盘的正确方法是什么?

注意:最小化到系统托盘;在时钟旁边的任务栏右侧。我不是要最小化到任务栏,这是当你点击窗口上的“减号”按钮时会发生的。

我见过一些骇人听闻的解决方案,例如“最小化,设置 ShowInTaskbar = false,然后显示您的 NotifyIcon。”

像这样的解决方案是骇人听闻的,因为该应用程序似乎不像其他应用程序那样最小化到托盘,代码必须检测何时设置 ShowInTaskbar = true 以及其他问题。

这样做的正确方法是什么?

4

9 回答 9

18

实际上,在本机 winforms 中没有托管方式对托盘执行这种形式的动画,但是您可以 P/Invoke shell32.dll 来执行此操作:

这里有一些很好的信息(在评论中不是帖子):

http://blogs.msdn.com/jfoscoding/archive/2005/10/20/483300.aspx

这是在 C++ 中的:

http://www.codeproject.com/KB/shell/minimizetotray.aspx

您可以使用它来确定要为您的 C# 版本 Pinvoke 的内容。

于 2008-09-06T19:07:50.017 回答
7
this.WindowState = FormWindowState.Minimized  

这是内置的方法,大多数时候对我来说都很好。唯一有点奇怪的是,如果您在启动时调用它,有时会有些奇怪,这就是为什么大多数人也会设置 ShowInTaskbar = false 并隐藏表单。

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate.aspx

于 2008-09-05T21:26:58.617 回答
5

这将有助于:

public partial class Form1 : Form
{
    public static bool Close = false;
    Icon[] images;
    int offset = 0;

    public Form1()
    {
        InitializeComponent();

        notifyIcon1.BalloonTipText = "My application still working...";
        notifyIcon1.BalloonTipTitle = "My Sample Application";
        notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; 
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        if (FormWindowState.Minimized == WindowState)
        {
            this.Hide();
            notifyIcon1.ShowBalloonTip(500);
            //WindowState = FormWindowState.Minimized;
        }
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.Show();
        notifyIcon1.ShowBalloonTip(1000);
        WindowState = FormWindowState.Normal;
    }

    private void maximizeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Show();
        WindowState = FormWindowState.Normal;
    }

    private void closeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close = true;
        this.Close();  
    }

    // Handle Closing of the Form.
    protected override void OnClosing(CancelEventArgs e)
    {
        if (Close)
        {
            e.Cancel = false;
        }
        else
        {
            WindowState = FormWindowState.Minimized;
            e.Cancel = true;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Load the basic set of eight icons.
        images = new Icon[5];
        images[0] = new Icon("C:\\icon1.ico");
        images[1] = new Icon("C:\\icon2.ico");
        images[2] = new Icon("C:\\icon3.ico");
        images[3] = new Icon("C:\\icon4.ico");
        images[4] = new Icon("C:\\icon5.ico");
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // Change the icon.
        // This event handler fires once every second (1000 ms).
        if (offset < 5)
        {
            notifyIcon1.Icon = images[offset];
            offset++;
        }
        else
        {
            offset = 0;
        }
    }
}
于 2010-08-18T08:26:27.677 回答
4

此代码经过测试并支持许多选项。

更多信息:http: //code.msdn.microsoft.com/TheNotifyIconExample

于 2008-12-29T16:28:15.560 回答
3

更新:看来我发布得太早了。我还使用下面的 hack 作为我的工具。等待正确的解决方案............

您可以为此使用 Windows.Forms.NotifyIcon。http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

将 NotifyIcon 添加到表单中,设置一些属性,您就完成了。

        this.ShowIcon = false;//for the main form
        this.ShowInTaskbar = false;//for the main form
        this.notifyIcon1.Visible = true;//for notify icon
        this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));//set an icon for notifyicon

    private void Form1_Shown(object sender, EventArgs e)
    {
        this.Hide();
    }
于 2008-09-05T21:55:23.837 回答
1

和上面类似...

我有一个调整大小事件处理程序,只要窗口调整大小(最大化、最小化等)就会触发...

    public form1()
    {
       Initialize Component();

       this.Resize += new EventHanlder(form1_Resize);
    } 


    private void form1_Resize(object sender, EventArgs e)
    {
       if (this.WindowState == FormWindowState.Minimized && minimizeToTrayToolStripMenuItem.Checked == true)
       {
             NotificationIcon1.Visible = true;
             NotificationIcon1.BalloonTipText = "Tool Tip Text"
             NotificationIcon1.ShowBalloonTip(2);  //show balloon tip for 2 seconds
             NotificationIcon1.Text = "Balloon Text that shows when minimized to tray for 2 seconds";
             this.WindowState = FormWindowState.Minimized;
             this.ShowInTaskbar = false;
       }
    }

这允许用户通过菜单栏选择是否要最小化到托盘。他们可以转到 Windows -> 并单击最小化到托盘。如果选中此项,并且窗口正在调整为最小化,那么它将最小化到托盘。对我来说完美无缺。

于 2009-02-10T16:19:29.550 回答
0

我已经为你们编写了一个快速解决方案,这可能不是最有效的方法(我不确定),但它绝对有效!

下面的解决方案成功地将您的 Winforms 应用程序最小化到系统托盘,并使用通知图标在系统托盘中创建一个带有上述菜单项的小图标。而不是我们使用内置库来获取窗口(在这种情况下为 GUI),然后使用布尔值的适当方法,然后我们将窗口最小化到托盘。

private void TransformWindow()
        {
            ContextMenu Menu = new ContextMenu();

            Menu.MenuItems.Add(RestoreMenu);
            Menu.MenuItems.Add(HideMenu);
            Menu.MenuItems.Add(new MenuItem("Exit", new EventHandler(CleanExit)));

            notifyIcon = new NotifyIcon()
            {
                Icon = Properties.Resources.Microsft_Icon,
                Text = "Folder Watcher",
                Visible = true,
                ContextMenu = Menu
            };
            processHandle = Process.GetCurrentProcess().MainWindowHandle;
            ResizeWindow(false);

        }
        #region Getting Libraries
        [DllImport("user32.dll")]
        public static extern IntPtr GetShellWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();
        public const Int32 SwMINIMIZE = 0;
        public const Int32 SwMaximize = 9;

        [DllImport("Kernel32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern IntPtr GetConsoleWindow();

        [DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ShowWindow([In] IntPtr hWnd, [In] Int32 nCmdShow);
        public static void MinimizeConsoleWindow()
        {
            IntPtr hWndConsole = processHandle;
            ShowWindow(hWndConsole, SwMINIMIZE);

        }
        public static void MaximizeConsoleWindow()
        {
            IntPtr hWndConsole = processHandle;
            ShowWindow(hWndConsole, SwMaximize);

        }
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        public static void ResizeWindow(bool show)
        {
            RestoreMenu.Enabled = !show;
            HideMenu.Enabled = show;
            SetParent(processHandle, WinDesktop);

            if (show)
                MaximizeConsoleWindow();
            else
                MinimizeConsoleWindow();

        }
        public static void MinimizeClick(object sender, EventArgs e) => ResizeWindow(false);
        public static void MaximizeClick(object sender, EventArgs e) => ResizeWindow(true);
        public static IntPtr processHandle;
        public static IntPtr WinShell;
        public static IntPtr WinDesktop;
        public static NotifyIcon notifyIcon;
        public static MenuItem HideMenu = new MenuItem("Hide", new EventHandler(MinimizeClick));
        public static MenuItem RestoreMenu = new MenuItem("Restore", new EventHandler(MaximizeClick));
        public void CleanExit(object sender, EventArgs e)
        {
            notifyIcon.Visible = false;
            this.Close();
        }
于 2020-07-22T15:35:38.930 回答
-1

在表单的构造函数中:

this.Resize += new EventHandler(MainForm_Minimize);

然后使用此事件处理程序方法:

    private void MainForm_Minimize(object sender, EventArgs e)
    {
        if(this.WindowState == FormWindowState.Minimized)
            Hide();
    }
于 2009-02-06T13:31:58.907 回答
-1

如果您在使其正常工作时遇到问题,请检查您是否有

this.Resize += new System.EventHandler(this.Form1_Resize);

在 fom1.designer.cs

于 2010-08-03T08:51:46.517 回答