visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: Delphi :: Knowledge Base : How to minimize application to system tray (SysTray) ? Delphi


By: VGR France  Date: 31/03/2005 16:36:02  English  Points: 0 Status: Answered
Quality : Good
here's the one for Delphi :

Please note that I also handle a small popup menu having options "close" (exit app) and "restore" (show again) managing the SysTray icon


in main Form's unit :

Uses [...], Menus; // for TNotifyIconData Const UM_NOTIFYICON = WM_USER +1; type TForm1 = class(TForm) // [...] public TrayIcon: TNotifyIconData; procedure UMNotifyIcon(var Msg : TMessage); message UM_NOTIFYICON; procedure MinimizeToTray(Sender: TObject); procedure NormalMinimize(Sender: TObject); procedure RestoreMainForm; procedure ShowThePopup;


in implementation :

procedure TForm1.MinimizeToTray(Sender: TObject); begin //ShowWindow(Application.Handle,SW_HIDE); Shell_notifyIcon(NIM_ADD, @TrayIcon); Form1.WindowState:=wsMinimized; Form1.Visible := False; Application.Minimize; end; procedure TForm1.RestoreMainForm; begin //ShowWindow(Application.Handle,SW_SHOW); Form1.Visible := True; Form1.WindowState:=wsNormal; SetForegroundWindow(Form1.Handle); Application.BringToFront; Application.Restore; SetFocus; Shell_notifyIcon(NIM_DELETE, @TrayIcon); end; procedure TForm1.ShowThePopup; var CurPos: TPoint; begin GetCursorPos(CurPos); PopupMenu1.Popup(CurPos.x, CurPos.y); PostMessage(Self.Handle, WM_NULL, 0, 0); end; procedure TForm1.UMNotifyIcon; // (var Msg : TMessage); message UM_NOTIFYICON; Var uID : integer; uMouseMsg : integer; Begin uID:=Msg.wParam; uMouseMsg:=Msg.lParam; If uID=0 Then // première icône créée Case uMouseMsg Of WM_LBUTTONDOWN : ; // rien WM_LBUTTONDBLCLK : RestoreMainForm; WM_RBUTTONDOWN : ShowThePopup; End // Case End;


-------------- in the form where I change the settings between "yes, hide in SysTray when minimized" and "no, don't" :

// this is in a procedure "change settings" or "apply changes"

// optTray is the Boolean controlling whether or not you want to "hide application" in SysTray. I switch between TRUE/FALSE from a secondary "configuration" form.

If optTray Then With Form1 Do Begin // activation // going to SysTray when Minimized TrayIcon.cbSize := SizeOf(TrayIcon); TrayIcon.Wnd := Form1.Handle; TrayIcon.uID := 0; TrayIcon.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; TrayIcon.uCallbackMessage := UM_NOTIFYICON; TrayIcon.hIcon := Application.Icon.Handle; TrayIcon.szTip := globApp+' - Running'; // globApp is my application's fancy name Application.OnMinimize := MinimizeToTray; End // if optTray Else With Form1 Do Begin Application.OnMinimize := NormalMinimize; TrayIcon.cbSize := SizeOf(TrayIcon); TrayIcon.Wnd := Form1.Handle; TrayIcon.uID := 0; Shell_notifyIcon(NIM_DELETE, @TrayIcon); End; // deactivate


Do register to be able to answer

EContact
browser fav
page generated in 295.007940 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page