This approach is mainly for Delphi2010 or later, the use of their own ttrayicon to minimize the function of the tray, as for the lower version, such as D7, the use of RZ tray control is good
Main modification points:
1.2010 or later, a Ttrayicon control is implemented to implement the tray icon, but the control does not have the ability to hide when minimized
2.2010 or later added the Mainformontaskbar function, it is well known that the Delphi taskbar button has not been the standard Windows style, with this setting, the taskbar button is normal, but also for our implementation of this feature adds some obstacles
Note: Because there are mainformontaskbar differences, it depends on whether the current state needs to hide the main form or hide the application
In addition, in Mainformontaskbar mode, Application.restore cannot trigger the Onrestore event, so it can only be executed with code
The following are form elements:
ObjectForm1:tform1 Left=0Top=0Caption='Form1'clientheight=243clientwidth=472Color=clbtnface Font.charset=Default_charset Font.Color=Clwindowtext font.height= - OneFont.Name='Tahoma'Font.style=[] Oldcreateorder=False onclosequery=formclosequery OnCreate=formcreate PixelsPerInch= theTextHeight= - Objectapplicationevents1:tapplicationevents onminimize=applicationevents1minimize Onrestore=Applicationevents1restore Left= -Top= the End ObjectTrayicon1:ttrayicon PopupMenu=PopupMenu1 Visible=True OnDblClick=Actactionexecute Left=176Top= the End ObjectActionlist1:tactionlist Left= -Top=144 Objectactaction:taction Caption='actaction'OnExecute=ActactionexecuteEnd End ObjectPopupmenu1:tpopupmenu Left=176Top=144 ObjectMivisible:tmenuitem Action=actactionEnd ObjectN1:tmenuitem Caption='-' End ObjectMiclose:tmenuitem Caption= #20851#38381OnClick=MicloseclickEnd EndEnd
The following is the implementation code, the note is written in the comments
UnitUnit1;Interfaceuseswinapi.windows, Winapi.messages, System.sysutils, System.variants, system.classes, Vcl.graphics, Vcl.Controls, Vcl.forms, Vcl.dialogs, System.actions, Vcl.actnlist, Vcl.menus , Vcl.extctrls, vcl.appevnts;typeTForm1=class(tform) applicationevents1:tapplicationevents; Trayicon1:ttrayicon; Actionlist1:tactionlist; Popupmenu1:tpopupmenu; Mivisible:tmenuitem; N1:tmenuitem; Miclose:tmenuitem; Actaction:taction; procedureapplicationevents1minimize (Sender:tobject); procedureActactionexecute (Sender:tobject); procedureMicloseclick (Sender:tobject); procedureApplicationevents1restore (Sender:tobject); procedureFormclosequery (Sender:tobject;varCanclose:boolean); procedureformcreate (Sender:tobject); Private {Private Declarations}Fappclose:boolean;{whether the program is closed}Fappminimized:boolean;{whether the program is minimized} Public {Public Declarations} End;varForm1:tform1;Implementation{$R *.DFM}procedureTform1.actactionexecute (sender:tobject);begin ifFappminimized Then begin {in Mainformontaskbar mode, can not directly execute application.restore, can not trigger Onrestore event, need to trigger manually, personal feeling is Delphi bug, no in-depth study} ifApplication.mainformontaskbar ThenApplicationevents1.onrestore (ApplicationEvents1)ElseApplication.restore; End Elseapplication.minimize;End;proceduretform1.applicationevents1minimize (sender:tobject);varNdohandle:hwnd;beginfappminimized:=True; Actaction.caption:='Show'; {to hide on the taskbar, you need to hide the corresponding form of the taskbar button} ifApplication.mainformontaskbar ThenNdohandle:=Application.MainForm.HandleElseNdohandle:=Application.handle; ShowWindow (Ndohandle, Sw_hide)End;procedureTform1.applicationevents1restore (sender:tobject);varNdohandle:hwnd;beginfappminimized:=False; Actaction.caption:='Hidden'; {Restore the display, bar corresponding to the form display, considering that the MainForm window has a normal and maximize the mode difference, so use Sw_restore to restore the display display after activating and resetting the front form} ifApplication.mainformontaskbar ThenNdohandle:=Application.MainForm.HandleElseNdohandle:=Application.handle; ShowWindow (Ndohandle, Sw_restore); SetForegroundWindow (ndohandle);End;procedureTform1.formclosequery (Sender:tobject;varCanclose:boolean); function_canclose:boolean; varNstr:string; beginNstr:='OK to close'+ Application.title +'?'; Result:= MessageBox (Handle, PChar (NSTR), PChar (Application.title), mb_iconquestion + mb_yesno) =Idyes; End;begin {set the click on the form Close button is also minimized to the function of the tray, so need a fappclose variable to distinguish whether to minimize or close} ifFappclose Then beginCanclose:=_canclose; if notCanclose ThenFappclose:=False; End Else beginapplication.minimize; Canclose:=False; End;End;proceduretform1.formcreate (sender:tobject);beginFappclose:=False; Fappminimized:=False;End;procedureTform1.micloseclick (sender:tobject);beginFappclose:=True; Close;End;End.
Minimized to Pallet