使用。net 4使程序最小化到系统托盘最简单的方法

时间:2022-04-25 07:16:42

I'm making a new WPF application and I need to be able to minimize the application and have nice and snug in the system tray, right beside the clock (or in that general area).

我正在做一个新的WPF应用程序,我需要能够最小化应用程序,并且在系统托盘中,在时钟旁边(或者在那个通用区域)中,有漂亮的和舒适的。

This has to work on Windows XP, Vista and 7. I don't have to support older versions of Windows.

这必须适用于Windows XP、Vista和7。我不需要支持旧版本的Windows。

What's the simplest way to achieve this if I'm using .NET 4?

如果我使用。net 4,最简单的方法是什么?

3 个解决方案

#1


51  

Example in MSDN forum

例子在MSDN论坛

Here's a quick example to show how to minimise to the notification area. You need to add references to the System.Window.Forms and System.Drawing assemblies.

这里有一个简单的例子来说明如何最小化通知区域。您需要向System.Window添加引用。形式和系统。绘图程序集。

public partial class Window1 : System.Windows.Window
{

    public Window1()
    {
        InitializeComponent();

        System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
        ni.Icon = new System.Drawing.Icon("Main.ico");
        ni.Visible = true;
        ni.DoubleClick += 
            delegate(object sender, EventArgs args)
            {
                this.Show();
                this.WindowState = WindowState.Normal;
            };
    }

    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == System.Windows.WindowState.Minimized)
            this.Hide();

        base.OnStateChanged(e);
    }
}

#2


10  

I've had success using this free notify-icon implementation in WPF.

我已经成功地使用了WPF中的这个免费的通知图标实现。

http://www.hardcodet.net/projects/wpf-notifyicon

http://www.hardcodet.net/projects/wpf-notifyicon

It's pretty simple to setup and the source code is provided. It doesn't rely on Windows Forms, so it's 'pure' WPF and very customizable.

它的设置非常简单,并且提供了源代码。它不依赖于Windows窗体,所以它是“纯”的WPF,非常可定制。

You can find a tutorial on how to use it on CodeProject.

您可以找到关于如何在CodeProject上使用它的教程。

#3


5  

Add notifyIcon to your App from Toolbox.
Select your main form >> go to the Properties >> select Events icon >> under FromClosing event type MainForm_FormClosing >> hit enter.

将notifyIcon从工具箱中添加到应用程序中。选择您的主要形式>>到属性>>选择事件图标>>在fromclose事件类型mainform_form关闭>>点击进入。

使用。net 4使程序最小化到系统托盘最简单的方法

In opened .cs file enter following event action:

在打开的.cs文件中输入以下事件动作:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
     this.Hide();
     notifyIcon.Visible = true;
     ShowInTaskbar = false;
     e.Cancel = true;
}

Now your main FORM window will be minimized to the system tray when you click on X button. Next step is to get FORM back to normal state.
Go to the Properties of your notifyIcon >> find DoubleClick event >> type NotifyIcon_DoubleClick and hit enter to get event function created for you.

现在,当您单击X按钮时,您的主窗体窗口将最小化到系统托盘中。下一步是恢复到正常状态。转到notifyIcon >>的属性找到DoubleClick事件>>类型NotifyIcon_DoubleClick,然后按enter获取为您创建的事件函数。

使用。net 4使程序最小化到系统托盘最简单的方法

Put this code to your event:

将此代码放到您的事件中:

private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
    this.Show();
    notifyIcon.Visible = false;
}

Now, if you want to make the notify icon in fancy style you can add context menu and link it to your notify icon, so you get something like that:

现在,如果你想制作一个样式奇特的通知图标你可以添加上下文菜单并将它链接到你的通知图标,你会得到这样的东西:

使用。net 4使程序最小化到系统托盘最简单的方法

Here is where you link contextMenuStrip to NotifyIcon:

这里是你链接contextMenuStrip到NotifyIcon的地方:

使用。net 4使程序最小化到系统托盘最简单的方法

Good luck!

好运!

#1


51  

Example in MSDN forum

例子在MSDN论坛

Here's a quick example to show how to minimise to the notification area. You need to add references to the System.Window.Forms and System.Drawing assemblies.

这里有一个简单的例子来说明如何最小化通知区域。您需要向System.Window添加引用。形式和系统。绘图程序集。

public partial class Window1 : System.Windows.Window
{

    public Window1()
    {
        InitializeComponent();

        System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
        ni.Icon = new System.Drawing.Icon("Main.ico");
        ni.Visible = true;
        ni.DoubleClick += 
            delegate(object sender, EventArgs args)
            {
                this.Show();
                this.WindowState = WindowState.Normal;
            };
    }

    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == System.Windows.WindowState.Minimized)
            this.Hide();

        base.OnStateChanged(e);
    }
}

#2


10  

I've had success using this free notify-icon implementation in WPF.

我已经成功地使用了WPF中的这个免费的通知图标实现。

http://www.hardcodet.net/projects/wpf-notifyicon

http://www.hardcodet.net/projects/wpf-notifyicon

It's pretty simple to setup and the source code is provided. It doesn't rely on Windows Forms, so it's 'pure' WPF and very customizable.

它的设置非常简单,并且提供了源代码。它不依赖于Windows窗体,所以它是“纯”的WPF,非常可定制。

You can find a tutorial on how to use it on CodeProject.

您可以找到关于如何在CodeProject上使用它的教程。

#3


5  

Add notifyIcon to your App from Toolbox.
Select your main form >> go to the Properties >> select Events icon >> under FromClosing event type MainForm_FormClosing >> hit enter.

将notifyIcon从工具箱中添加到应用程序中。选择您的主要形式>>到属性>>选择事件图标>>在fromclose事件类型mainform_form关闭>>点击进入。

使用。net 4使程序最小化到系统托盘最简单的方法

In opened .cs file enter following event action:

在打开的.cs文件中输入以下事件动作:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
     this.Hide();
     notifyIcon.Visible = true;
     ShowInTaskbar = false;
     e.Cancel = true;
}

Now your main FORM window will be minimized to the system tray when you click on X button. Next step is to get FORM back to normal state.
Go to the Properties of your notifyIcon >> find DoubleClick event >> type NotifyIcon_DoubleClick and hit enter to get event function created for you.

现在,当您单击X按钮时,您的主窗体窗口将最小化到系统托盘中。下一步是恢复到正常状态。转到notifyIcon >>的属性找到DoubleClick事件>>类型NotifyIcon_DoubleClick,然后按enter获取为您创建的事件函数。

使用。net 4使程序最小化到系统托盘最简单的方法

Put this code to your event:

将此代码放到您的事件中:

private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
    this.Show();
    notifyIcon.Visible = false;
}

Now, if you want to make the notify icon in fancy style you can add context menu and link it to your notify icon, so you get something like that:

现在,如果你想制作一个样式奇特的通知图标你可以添加上下文菜单并将它链接到你的通知图标,你会得到这样的东西:

使用。net 4使程序最小化到系统托盘最简单的方法

Here is where you link contextMenuStrip to NotifyIcon:

这里是你链接contextMenuStrip到NotifyIcon的地方:

使用。net 4使程序最小化到系统托盘最简单的方法

Good luck!

好运!