【问题标题】:C# trayicon using wpf使用 wpf 的 C# 托盘图标
【发布时间】:2012-09-14 15:55:55
【问题描述】:

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

System.Windows.Forms

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

【问题讨论】:

    标签: c# wpf trayicon


    【解决方案1】:

    您需要添加对 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);
        }
    }
    

    【讨论】:

    • 优秀的参考这个词帮助我找出了如何以及在哪里。我现在有一个托盘图标,谢谢 =)
    • 如果您将它放在带有 System.Windows 引用的窗口中,您将遇到一些模棱两可的问题。我通过在 using 中添加名称来解决它: using WinForms = System.Windows.Forms;然后用 WinForms.NotifyIcon notifyIcon = new WinForms.NotifyIcon(); 调用它
    • 效果很好,赞一个。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 2011-06-25
      相关资源
      最近更新 更多