如何在多个表单之间使用 1 个 notificationIcon?

发布于 2024-12-21 20:12:12 字数 164 浏览 2 评论 0原文

我在项目的主窗体中添加了一个notifyIcon。我在项目中有其他表单,我希望能够使用 notificationIcon,尽管事实证明这很困难。在多个表单之间使用 1 notificationIcon 的最佳方法是什么?我读到了一篇关于不将其添加到表单中而是在其自己的类中实例化它的主题,这对我来说毫无意义。想法?

I have a notifyIcon added to my main form of a project. I have other forms in the project that I want to be able to use the notifyIcon though which is proving difficult. What would be the best way to use 1 notifyIcon between multiple forms? I read a thread about not adding it to a form but instantiating it in its own class which made no sense to me. Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煞人兵器 2024-12-28 20:12:12

只需在主窗体上公开一个属性即可返回对 NotifyIcon 的引用。您甚至可以将其设为静态,因为只有一个:

public partial class MainForm : Form {
    public MainForm() {
        InitializeComponent();
        notifier = this.notifyIcon1;
        this.FormClosed += delegate { notifier = null; };
    }

    public static NotifyIcon Notifier { get { return notifier; } }

    private static NotifyIcon notifier;
}

其他类中的代码现在可以简单地使用 MainForm.Notifier。

Just expose a property on your main form to return a reference to the NotifyIcon. You can even make it static since there is only ever one:

public partial class MainForm : Form {
    public MainForm() {
        InitializeComponent();
        notifier = this.notifyIcon1;
        this.FormClosed += delegate { notifier = null; };
    }

    public static NotifyIcon Notifier { get { return notifier; } }

    private static NotifyIcon notifier;
}

Code in other classes can now simply use MainForm.Notifier.

橘香 2024-12-28 20:12:12

在实现 NotifyIcon 的表单中创建一个公共静态变量:

public static NotifyIcon m_objNotifyIcon;

在表单中加载分配表单 NotifyIcon

m_objNotifyIcon = this.notifyIcon1;

全部设置。现在您可以从项目中的任何位置访问通知图标

Forms.MainScreen.m_objNotifyIcon.ShowBalloonTip(2000, "Title", "Message", ToolTipIcon.Info);

Create a public static variable in the Form where NotifyIcon is implemented:

public static NotifyIcon m_objNotifyIcon;

in form load assign forms NotifyIcon:

m_objNotifyIcon = this.notifyIcon1;

all set. Now you can access the notify icon from anywhere in the project

Forms.MainScreen.m_objNotifyIcon.ShowBalloonTip(2000, "Title", "Message", ToolTipIcon.Info);
丿*梦醉红颜 2024-12-28 20:12:12

即使您需要将 MainForm 分配给 NotifyIcon 控件,也不会发现在不同窗体之间共享其对象有任何问题。

让我们说得更清楚一点:

1. MainForm 启动并初始化 NotifyIcon 控件包装类,让我们调用 NotifyControlHolder.

2. NotifyControlHolder 类像公共属性一样存在于您的 UIShared 中。

3. UIShared 单调类可以从应用程序的不同部分访问,可以绕过 MainForm 访问它并更改其状态。

Even if you need to assign MainForm to NotifyIcon control, don't see any problem about sharing it's object between different forms.

Let's say, to try to be more clear:

1. MainForm starts and initializes NotifyIcon control wrapper class, let's call NotifyControlHolder.

2. That NotifyControlHolder class stands in your UIShared like a public property.

3. UIShared singletone class is accesible from differnt parts of your application, which can access it and change it's state, bypassing the MainForm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文