Languages :: Delphi :: Knowledge Base : How to minimize application to system tray (SysTray) ? Delphi |
|||
| By: VGR |
Date: 31/03/2005 16:36:02 |
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 |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








