{ Create the component. At run-time, automatically add a tray icon with a callback to a hidden window. Use the application icon and title. } constructor TTrayIcon.Create(Owner: TComponent); begin inherited Create(Owner); fIcon := TIcon.Create; fIcon.Assign(Application.Icon); ifnot (csDesigning in ComponentState) then begin FillChar(fData, SizeOf(fData), 0); fData.cbSize := SizeOf(fData); fData.Wnd := AllocateHwnd(OnMessage); // handle to get notification message fData.hIcon := Icon.Handle; // icon to display StrPLCopy(fData.szTip, Application.Title, SizeOf(fData.szTip) - 1); fData.uFlags := Nif_Icon or Nif_Message; if Application.Title <> '''''''' then fData.uFlags := fData.uFlags or Nif_Tip; fData.uCallbackMessage := WM_CALLBACK_MESSAGE; ifnot Shell_NotifyIcon(NIM_ADD, @fData) then// add it raise EOutOfResources.Create(''''Cannot create shell notification icon''''); { Replace the application''''s minimize and restore handlers with special ones for the tray. The TrayIcon component has its own OnMinimize and OnRestore events that the user can set. } Application.OnMinimize := AppMinimize; Application.OnRestore := AppRestore; end; end;
{ Remove the icon from the system tray.} destructor TTrayIcon.Destroy; begin fIcon.Free