我有一个具有系统托盘图标的应用程序。卸载时,如果该进程正在运行,我将终止该进程。因此,由于没有优雅地停止应用程序,该图标仍保留在系统托盘中,并且只有当我们将鼠标悬停在其上时才会移除。我编写了一个代码,它将沿着托盘运行光标并将光标返回到其初始位置。这就是我所做的:
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr parent, IntPtr child, string className, string windowName);
[DllImport("user32.dll")]
static extern bool GetWindowRect(HandleRef handle, out RECT rct);
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
void RefreshTray()
{
IntPtr taskbar_Handle = FindWindow("Shell_Traywnd", "");
IntPtr tray_Handle = FindWindowEx(taskbar_Handle, IntPtr.Zero, "TrayNotifyWnd", "");
RECT rct;
if (!(GetWindowRect(new HandleRef(null, tray_Handle), out rct)))
{
}
System.Drawing.Point init = Control.MousePosition;
for (int i = rct.Left; i < rct.Right-20; i++)
{
Cursor.Position = new System.Drawing.Point(i, (rct.Bottom + rct.Top) / 2);
}
Cursor.Position = init;
}
这在所有情况下都很好,除非启用了“不显示通知图标”选项。在这种情况下,有什么方法可以刷新托盘吗?
编辑 正如评论所建议的,我改变了我的方法。我没有杀死托盘应用程序,而是在我的应用程序服务(是的,忘了提,我也有一个服务与应用程序一起运行)和托盘应用程序之间建立了通信。在卸载时,我会停止服务,从服务停止方法中,我会向托盘应用程序发送一个特定格式的套接字消息并要求它关闭,我会将通知图标可见性设置为 false。这将使托盘应用程序在后台运行,因此我使用“taskkill”来删除该应用程序。它在 Win7 和 Vista 中运行良好,但在 Win XP 中无法正常运行。但是我还没有编写任何特定于环境的代码。有什么可能的线索吗?