在 C# 属性中实现 Minimize-to-Tray 的可能性

发布于 2024-08-11 01:23:21 字数 194 浏览 8 评论 0原文

属性太棒了但是是否可以创建一个 C# 属性类,在标记后使您的应用程序最小化到系统托盘?

从技术上讲,该属性需要放置在主窗体的类上。一旦用户单击 X 按钮,该表单不应终止,而应最小化到任务栏。最小化图标也应该在属性中指定。

这在技术上可行吗?我找不到任何有关从放置在该类上的属性覆盖类代码的信息。 (您至少需要覆盖一种方法以使其最小化到托盘。)

Attributes are awesome. But is it possible to create a C# attribute class that, when tagged, makes your application minimize to the system tray?

Technically, the attribute would need to be placed on the class of the main form. Once the user clicks on the X button, that form shouldn't terminate, but should minimize to the taskbar. The icon for minimizing should also be specified in the attribute.

Is this technically feasible? I can't find any information on overwriting class code from an attribute placed on that class. (You need to at least overwrite one method to make it minimize to tray.)

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

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

发布评论

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

评论(4

善良天后 2024-08-18 01:23:21

为什么不这样做:

class TrayForm : Form
{
    NotifyIcon notifyIcon = new NotifyIcon();
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason != CloseReason.ApplicationExitCall)
        {
            e.Cancel = true;
            this.Hide();
            this.notifyIcon.Visible = true;
        }

        base.OnFormClosing(e);
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        if (WindowState == FormWindowState.Minimized)
        {
            this.Hide();
            this.notifyIcon.Visible = true;
        }

        base.OnSizeChanged(e);
    }
}

Why not just do this:

class TrayForm : Form
{
    NotifyIcon notifyIcon = new NotifyIcon();
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason != CloseReason.ApplicationExitCall)
        {
            e.Cancel = true;
            this.Hide();
            this.notifyIcon.Visible = true;
        }

        base.OnFormClosing(e);
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        if (WindowState == FormWindowState.Minimized)
        {
            this.Hide();
            this.notifyIcon.Visible = true;
        }

        base.OnSizeChanged(e);
    }
}
_畞蕅 2024-08-18 01:23:21

使用表单继承而不是属性可以更容易地实现这一点,但仍然需要在运行时解释属性才能达到所需的效果。

通过继承,您只需将类设置为您喜欢的行为方式,并且本质上具有选择加入或选择退出的能力(甚至使用属性来执行此操作)。

This could be achieved easier with form inheritance rather than attributes, something is still going to have to interpret the attributes at runtime to achieve the desired effect.

With inheritance you can just setup the class to behave the way you like and have essentially an opt-in or opt-out ability (even using an attribute to do so).

十年不长 2024-08-18 01:23:21

这当然是可能的,但我建议重写表单 close() 方法并简单地在其中放置一些最小化代码。它简单、合乎逻辑,并且很容易让未来的开发人员遵循。

有关如何实现此目的的一些示例代码位于:

http://www.dreamincode.net/code/snippet2660.htm

http://www.dreamincode.net/forums/showtopic116283.htm

It's certainly possible but I'd suggest overriding the form close() method and simply put some minimizing code in there. It's simple, logical, and easy for future developers to follow.

Some sample code on how to achieve this is at:

http://www.dreamincode.net/code/snippet2660.htm

and

http://www.dreamincode.net/forums/showtopic116283.htm

雪落纷纷 2024-08-18 01:23:21

这对于通常的 Form 类来说是不可能的。但是可以创建一个自己的类,该类继承自 Form 并且也支持此类属性。但这只会使事情变得不必要的复杂,最好向该类添加一个 MinimizeToTray 属性。

(类似于 PhilipW 提供的示例,但添加了一个属性来控制行为)

It's not possible with the usual Form class. But it's possible to create an own class that inherits from Form and also supports such an attribute. But that would only make things unnecessary complicated, it would probably be better to add a MinimizeToTray property to that class.

(like the example PhilipW provided but with a property added to control the behavior)

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