在 C# 属性中实现 Minimize-to-Tray 的可能性
属性太棒了但是是否可以创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不这样做:
Why not just do this:
使用表单继承而不是属性可以更容易地实现这一点,但仍然需要在运行时解释属性才能达到所需的效果。
通过继承,您只需将类设置为您喜欢的行为方式,并且本质上具有选择加入或选择退出的能力(甚至使用属性来执行此操作)。
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).
这当然是可能的,但我建议重写表单 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
这对于通常的
Form
类来说是不可能的。但是可以创建一个自己的类,该类继承自Form
并且也支持此类属性。但这只会使事情变得不必要的复杂,最好向该类添加一个 MinimizeToTray 属性。(类似于 PhilipW 提供的示例,但添加了一个属性来控制行为)
It's not possible with the usual
Form
class. But it's possible to create an own class that inherits fromForm
and also supports such an attribute. But that would only make things unnecessary complicated, it would probably be better to add aMinimizeToTray
property to that class.(like the example PhilipW provided but with a property added to control the behavior)