题
我在FormCreate事件处理程序中使用以下代码来创建系统托盘图标。当我运行程序时,系统托盘图标看起来不错。
我将自己的应用程序设置为在Windows启动时自动启动。重新启动计算机时,我的应用程序的过程已启动,但是系统托盘图标从未出现。
我认为这可能与代码运行时的时机有关,也许系统托盘还没有准备好被填充。
我将我的应用程序通过其NSIS安装程序启动运行:Writeregstr HKLM“ Software Microsoft Windows Windows CurrentVersion run”“ MyApp”“ MyApp”“ $ Instdir MyApp.exe”
创建系统托盘图标的代码:
with TrayIconData do
begin
cbSize := SizeOf(TrayIconData);
Wnd := Handle;
uID := 0;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_ICONTRAY;
hIcon := Application.Icon.Handle;
StrPCopy(szTip, Application.Title);
end;
Shell_NotifyIcon(NIM_ADD, @TrayIconData);
其他提示
The problem was caused because the current directory during start up is not the directory in which the executable lives.
So getCurrentDir was actually returning different directories at startup and when the app was run at any other time.
My application was making the poor assumption that the current dir would be the one in which the executable resides.
This assumption was causing the app to never reach the tray icon adding code at all. Once I fixed up the directory issue the code ran and correctly created the icon.