System Tray Icon not appearing on startup

醉酒当歌 提交于 2019-12-04 14:34:57

问题


I use the following code in the FormCreate event handler to create a system tray icon. When I run my program the system tray icon appears fine.

I set my application be started automatically on windows start up. When I restart the computer my application's process is started but the system tray icon never appears.

I think it might be something to do with the timing of when the code is run, that perhaps the system tray isn't ready to be populated.

I get my app to run at start up via it's NSIS installer : WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "MyApp" "$INSTDIR\ MyApp.exe"

Code to create system tray icon :

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);

回答1:


You're attempting to create the icon before Explorer has fully started. You should handle errors gracefully (scroll down to "Handling Shell_NotifyIcon failure").

You should also handle the TaskbarCreated notification -- it enables you to recreate your icons after Explorer crashes and restarts.




回答2:


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.



来源:https://stackoverflow.com/questions/1858552/system-tray-icon-not-appearing-on-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!