使用 wpf 的 C#trayicon

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12428006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:21:40  来源:igfitidea点击:

C# trayicon using wpf

c#wpftrayicon

提问by Logan

I'm very new to programming C#, though I've scripted C# in unity3D for a few years. I'm currently trying to make a WPF tray icon, all the sources I've found on the net tell me to use

我对 C# 编程非常陌生,尽管我已经在 unity3D 中编写 C# 脚本几年了。我目前正在尝试制作 WPF 托盘图标,我在网上找到的所有来源都告诉我使用

System.Windows.Forms

However .Forms is not available in System.Windows for me, and I have no idea why not. Can anyone help me with this?

但是 .Forms 在 System.Windows 中对我来说不可用,我不知道为什么不可用。谁能帮我这个?

回答by Erre Efe

You need to add references to the System.Window.Forms and System.Drawing assemblies and then you use it like this. Suppose you try to minimize the Window to tray icon and show it again when user click that icon:

您需要添加对 System.Window.Forms 和 System.Drawing 程序集的引用,然后像这样使用它。假设您尝试最小化窗口到托盘图标,并在用户单击该图标时再次显示它:

public partial class Window : System.Windows.Window
{

    public Window()
    {
        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 == WindowState.Minimized)
            this.Hide();

        base.OnStateChanged(e);
    }
}

回答by Chris Dunaway

You need to add a reference to the System.Windows.Forms.dll and then use the NotifyIcon class.

您需要添加对 System.Windows.Forms.dll 的引用,然后使用 NotifyIcon 类。

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

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