Detailed explanation of Tapplication class in Delphi TApplication is the type of Delphi application. This class inherits from TComponent and is declared in the unit Forms. And there is a public object Application in Forms. Its methods and properties include established basic operations and properties such as creating, running and destroying applications in the Windows operating system, thus simplifying the interface between the user and the Windows environment when writing Windows applications in Delphi. TApplication encapsulates the following four functions: 1. Windows message processing. 2. Menu acceleration and keyboard processing. 3. Exception handling. 4. Contextual online help. The following will introduce to you the commonly used properties, methods and events in TApplication. 1. Attribute 1. 1 attribute Active attribute Active indicates whether the application is active and has focus. The definition is as follows: PRperty Active: Boolean; Active is a read-only property. Active is True when the application is active, False otherwise. The constructor of TApplica-tion sets Active to True. If a window or application has focus, then the application is active. The current application is inactive when another application's window becomes active. When the application is closed, the destructor of TApplication sets Active to False. In the application, you can use a timer (TTimer) to check the value of the attribute Active to determine whether the current application is active so that appropriate processing can be made. Specified operations can also be defined in the event OnActive and the event OnDeactive. 1.2 Property DialogHandle Property DialogHandle provides a mechanism for Delphi applications to use non-Delphi dialog boxes. The definition is as follows: property DialogHandle: HWnd; When using the API function CreateDialog to create a non-modal dialog box, you need to use DialogHandle, and you need to view the messages in the application message loop to perform corresponding operations. For example, when a modeless dialog box receives an activation message (WM_NCACTIVATE), its handle can be assigned to DialogHandle, and when the dialog box receives a deactivation message, the DialogHandle is set to 0. 1.3 Attribute ExeName Attribute ExeName contains the executable application file name and path information. It is defined as follows: property ExeName: string; ExeName is a read-only property. Use ExeName to get the file name of the application executable file. This is a very useful attribute. For example, when running the application f:/media/myapp.exe on the CD, you may need to access files in the directory f:/media/data/ or determine the drive letter where the application is located. At this time, you can use the functions ExtractFilePath and ExtractFileName provided by Delphi to parse the attribute ExeName to obtain the required information. 1.4 Attribute Handle The attribute Handle provides access to the handle of the application's main window. The definition is as follows: property Handle: HWND; The Handle property needs to be used when calling a Windows API function that requires a parent window handle. For example, a dynamic link library (DLL) in an application may need a parent window handle so that it can pop up and be brought to the forefront. Use Application.Handle to form several windows of the application, so that these windows can be minimized, restored, enabled or disabled in the application. Note: When writing a dynamic link library that uses VCL forms, the handle of the main window in the main running program should be assigned to the Application.Handle property of the dynamic link library. This makes the form of the dynamic link library become part of the main application. It is important to point out that you should never assign a value to Application.Handle in an EXE application. 1.5 Attribute HelpFile Attribute HelpFile specifies the file name used by the application to display help content. The definition is as follows: property HelpFile: string; Using HelpFile is for the application to have a help file using the standard Windows help system. Windows displays the Help file specified by the HelpFile property. For your application to achieve this, you must give the HelpFile property a value of a file name at run time, or specify a help file in the Application page of the Project|Options dialog box at design time. By default, HelpFile is an empty string (''), and the application's help methods ignore all attempts to display help. If HelpFile contains any content, the Help topic method will bring up the Windows Help system to provide online help based on the file name. Note: If the active window's Help file is specified, that file will be displayed in preference to the application's Help file. 1.6 Attribute Hint Attribute Hint specifies the text string that appears in the Help Hint box. The definition is as follows: property Hint: string; The TApplication.Hint property is the Hint value of the control or menu item that the mouse is moving through. This attribute can also be assigned a string value that provides action, error, or other information to the user. Therefore, using the Hint property can also: transfer prompt information from the control to another display area, for example, display the prompt in the status bar through the OnHint event handler. At this time, the Hint attribute is read. Briefly describe the state of the application when it is performing an action. At this time, the Hint attribute is set. The help prompt appears when the OnHint event occurs. Therefore, even if the Hint property of TApplication is assigned the title of the status bar, for example, the title of the status bar control displays the current string value of the Hint property, it should be attributed to the OnHint event. Hint string consists of two parts: short hint and long hint. Short prompts are used for concise pop-up prompts; long prompts and short prompts are separated by a vertical bar "|" and are used to display more detailed prompt information in the status bar. You can use the string functions GetShortHint and GetLongHint provided in the unit Controls to get short hints and long hints from the Hint attribute. Note: When an application provides information to the user about the occurrence of an event by setting the Hint property, it should be remembered that by default, when the mouse moves over a control, the Hint string is reset to the value of the Hint property of the control. 1.7 Property HintHidePause Property HintHidePause specifies the time interval before the help prompt is hidden before the mouse has been moved away from the control or menu item. It is defined as follows: property HintHidePause: Integer; Use HintHidePause to specify the waiting time in milliseconds. This property is set to 2500 milliseconds (2.5 seconds) in the constructor of TApplication. The help tip for a control or menu item is specified in the Hint property. Note: By default, the value of HintHidePause should be predetermined to be 3 to 5 times the value of the HintPause attribute. 1.8 Property MainForm The property MainForm uniquely identifies the main form of the application. It is defined as follows: property MainForm: TForm; The form specified by the MainForm property is not necessarily the same as the main window of the application. The value of the MainForm property must be the first form among all forms created by the CreateForm method, but the form may not be the main window of the application. When creating a new project, the MainForm attribute value is automatically set to Form1. At design time, you can specify one of multiple forms as MainForm through the Forms page in the Project|Options dialog box. The MainForm property cannot be modified at runtime because the property is read-only. The main form is the first form created by the application theme. The application terminates when the main form is closed. In an application, you can call the method Application.MainForm.Close to terminate the application. You can obtain the values of properties such as Application.MainForm.Top and Application.MainForm.Left to determine the position and size of the currently active window. 1.9 Property ShowMainForm Property ShowMainForm determines whether to display the main form when the application starts. It is defined as follows: property ShowMainForm: Boolean; The application uses the ShowMainForm property to control whether and when to display its main form. The constructor of TApplication sets ShowMainForm to True. By default the main form will be displayed, as specified in the MainForm property. If you need to hide the main form when the application starts, you should set ShowMainForm to False before calling Application.Run in the main project file, and make sure that the Visible property value of the main form is False. This is useful in many implementations of OLE automation servers, such as hiding the main form of the server program when starting the automation service. In addition, if you need to display a flash picture (Flash) when the application starts and make some preparations for the application environment, and also need to disable the display of the main form, this can also be achieved by using the ShowMainForm attribute. For example, when Delphi starts, the display of the main form is disabled. 1.10 Attribute Terminated The Terminated attribute reports whether the program has received the Windows message WM_QUIT that terminates the program. The definition is as follows: property Terminated: Boolean; Terminated is a read-only property. This property is primarily used when calling the ProcessMessages method so that the application does not have to stop trying to process Windows messages. When the ProcessMessages method receives the message WM_QUIT, Terminated will be set to True. Delphi applications always receive the message WM_QUIT because the main form or application is closed, or because the Terminate method is called. When an application performs a highly intensive operation that takes up a lot of system resources, the Application-on.ProcessMessages method should be called periodically and the property Application.Terminated should be checked to determine whether the operation needs to be terminated to terminate the application. 1.11 Property UpdateFormatSettings Property UpdateFormatSettings specifies whether the application automatically updates the format settings when the user changes the system configuration. Defined as follows: property UpdateFormatSettings: Boolean; Using the UpdateFormatSettings property application can control the automatic update of format settings. The constructor of TApplication sets this property to True. The UpdateFormatSett-ings property will be checked when the application receives the message WM_WININICHANGE. It is recommended to use the default format settings, which are the local Windows settings. You can set UpdateFormatSe-ttings to False to avoid changing format settings during the execution of the Delphi application. 1.12 Attribute UpdateMetricSettings Attribute UpdateMetricSettings attribute indicates whether to update related settings such as prompt window font and icon title. The definition is as follows: property UpdateMetricSettings: Boolean; The UpdateFormatSettings property indicates whether changes in settings such as prompt window font and icon title in the system are reflected in changes in related settings in the application. The constructor of TApplication sets the initial value of UpdateMetricSettings to True. 2. Method 2.1 Method BringToFront Method BringToFront sets the most recently active window in the application to the front of all windows on the desktop. Its definition is as follows: procedure BringToFront; Use the BringToFront method to find the most recent active window belonging to the main form and bring it to the front. The BringToFront method can also test and see whether a window is visible (Visible) and valid (Enabled) before becoming the frontmost window. For example, when an application receives mail, a specialized handler may need to be activated and brought to the forefront of the Windows desktop. At this time, you can call the Application.BringToFront method to achieve this. 2.2 Method CreateForm Method CreateForm method is used to create a new form (form). It is defined as follows: procedure CreateForm(FormClass: TFormClass; var Reference); Delphi applications will always call the CreateForm method. Therefore, it is rarely necessary for programmers to call the CreateForm method directly. A typical Delphi project includes one or more calls to the CreateForm method in the main code part of the project, and automatically controls the creation of the form when using the form designer. You can also call the CreateForm method at runtime to dynamically create a form. The CreateForm method creates a new specified form based on the FormClass parameter and assigns the form to the variable parameter Reference. The owner of the newly created form is the object Application. The application will default to the first form created by calling CreateForm as the project's main form. 2.3 Method HandleException The method HandleException provides a default handle for application exceptions. It is defined as follows: procedure HandleException(Sender: TObject); The method HandleException is useful for authors of specific components because it can generate an event that does not have to respond to Windows messages. You can use the OnException event handler in your application to control other exception operations in custom code. In the application code, if the exception skips all try blocks, the application will automatically call the HandleE-xception method and will display a dialog box indicating that an error occurred. Unless the exception object is EAbort, then Han-dleException will call the OnException handle (if it exists), otherwise ShowException will be called to display a dialog box prompting an error. 2.4 Method UnhookMainWindow The UnhookMainWindow method is used to release the program hung on the main form by the HookMainWindow method. The definition is as follows: type TWindowHook = function(var Message: TMessage): Boolean of object; procedure UnhookMainWindow(Hook: TWindowHook); Use UnhookMainWindow to release the hook window. Specify the dialog box procedure in the parameter Hook. The TwindowHook type is the parameter for calling the HookMainWindow method. This parameter is the method pointer for calling the dialog program in a non-Delphi dialog box. Dialog programs are similar to window programs in that they process messages for dialog boxes, but the syntax is different. 3. Event 3.1 Event OnActivate The OnActivate event occurs when the application becomes active. The definition is as follows: type TNotifyEvent = procedure (Sender: TObject) of object; property OnActivate: TNotifyEvent; Use the OnActive event to write an event handler to specify special processing when the application becomes active. A Windows application becomes active when it initially runs or when its focus is transferred back to the current application from another Windows application. 3.2 Event OnDeactivate The OnDeactivate event occurs when the application becomes inactive. It is defined as follows: type TNotifyEvent = procedure (Sender: TObject) of object; property OnDeactivate: TNotifyEvent; The OnDeactive event can be triggered immediately before the application becomes inactive, thereby completing the special processing specified in the event handler. The current application's OnDeactive event occurs when the user transitions from the current application to another application. 3.3 Event OnException The event OnException occurs when a handleless exception occurs in the application. It is defined as follows: type TExceptionEvent = procedure (Sender: TObject; E: Exception) of object; property OnException: TExceptionEvent; You can use the OnException event to change the default action when an exception without a handler occurs in the application. In the method TApplication.HandleException method, the OnException event handler is automatically called. The OnException event is used only to handle exceptions that occur during message processing. Exceptions that occur before or after Application.Run is executed will not cause the OnException event to occur. If an exception is ignored in the try block of the application code, the application will automatically call the HandleException method. Unless the exception object is an EAbort, HandleException will call the OnException handle (if it exists), otherwise ShowException will be called to display a dialog box prompting an error. The TExceptionEvent type is the type of the OnException event, which points to a method for handling exceptions in the application. The parameter Sender is the object that throws the exception, and parameter E is the exception object. 3.4 Event OnHelp The OnHelp event occurs when the application receives a help request. The definition is as follows: type THelpEvent = function (Command: Word; Data: Longint; var CallHelp: Boolean): Boolean of object; property OnHelp: THelpEvent; Use OnHelp to write an event handler to complete special processing when there is a request for help. The HelpContext method and the HelpJump method automatically raise the OnHelp event. Set CallHelp to True to enable VCL to call WinHelp after the event occurs; set CallHelp to False to prevent VCL from calling WinHelp. All help-related methods in Delphi applications go through the OnHelp event. WinHelp is called only when the CallHelp parameter in the OnHelp event returns True or when the OnHelp event is not assigned to a valid handle. 3.5 Event OnHint When the mouse pointer moves through a control or menu item and the control or menu item can display a help prompt, the event OnHint occurs. The definition is as follows: type TNotifyEvent = procedure (Sender: TObject) of object; property OnHint: TNotifyEvent; The event handler written with OnHint can perform the specified operation when the OnHint event occurs. When the user places the mouse pointer on a control and the value of the Hint property of the control is not an empty string (''), the OnHint event will occur. The OnHint event is usually used to display the value of the Hint property of a control or menu item as the title of a panel control (such as TStatusBar), so the panel is used as the status bar. When the OnHint event occurs, the Hint attribute is usually specified as a Help Hint and a longer hint that is displayed elsewhere. 3.6 Event OnIdle The OnIdle event occurs when the application becomes idle. The definition is as follows: type TIdleEvent = procedure (Sender: TObject; var Done: Boolean) of object; property OnIdle: TIdleEvent Use OnIdel to write an event handler to complete the specified operation when the application is idle. An application is idle when it is not executing any code. For example, an application is idle while it waits for user input. The TIdelEvent type is the type of OnIdel event, which points to a method that runs when the application is idle. The object TIdelEvent has a variable Done that defaults to True. When Done is True, the Windows API function WaitMessage will be called when OnIdel returns. WaitMessage causes other applications to gain control focus until a new message appears in the application's message queue. When the parameter Done is False and the application is not busy, the application will not allow other applications to gain control focus. When the application becomes idle, the OnIdel event occurs only once, and the next OnIdel event may not occur until the parameter Done is set to True. Setting Done to False by the application eliminates erratic CPU time counts that can impact overall system performance. 3.7 Event OnMessage is generated when the application receives a WINDOWS message. It is defined as follows: type TMessageEvent = procedure (var Msg: TMsg; var Handled: Boolean) of object; property OnMessage: TmessageEvent is used to receive WINDOWS messages. This event can receive All messages sent by the program to WINDOWS. This event is generated when the application receives a message. The variable Msg is of WINDOWS message type.