我有一个带系统托盘图标的应用程序。在卸载时,如果它正在运行,我会杀死该进程。因此,由于我没有正常停止应用程序,图标仍然留在系统托盘中,只有当我们将鼠标悬停在其上时才会删除。我编写了一段代码,可以沿着托盘运行光标,并将光标返回到初始位置。以下是我的做法:
[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中无法正常工作。但我没有编写任何特定环境的代码。是否有可能提供一些线索?