以下是新制作的 Windows 窗体项目中的一些代码,没有其他任何更改:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Bitmap blah = new Bitmap(16, 16);
using (Graphics blah2 = Graphics.FromImage(blah))
{
blah2.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, 16, 16));
}
NotifyIcon2 n = new NotifyIcon2();
n.NotifyIcon = new NotifyIcon();
n.NotifyIcon.Icon = Icon.FromHandle(blah.GetHicon());
n.NotifyIcon.Visible = true;
}
class NotifyIcon2 : IDisposable
{
public NotifyIcon NotifyIcon { get; set; }
private bool disposed;
~NotifyIcon2()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this); // The finalise process no longer needs to be run for this
}
protected virtual void Dispose(bool disposeManagedResources)
{
if (!disposed)
{
try
{
NotifyIcon.Dispose();
}
catch { }
disposed = true;
}
}
}
}
从我所看到的,在 中protected virtual void Dispose
,NotifyIcon
当它被执行时已经被释放(解释了为什么我在那里放了一个 try/catch 块),所以我不能对它的图标做任何事情。
那么如何让它消失呢?