I. tapplication is used for Delphi applicationsProgramIs declared in unit forms. Tapplication is the type required by Delphi to construct a Windows application. Its methods and attributes include the established basic operations and attributes such as creating, running, and destroying an application in the Windows operating system, therefore, it simplifies the excuse between users and the Windows environment when using Delphi to write Windows applications.
Tapplication encapsulates the following features:
L WINDOWS Message Processing
L context online help
L menu acceleration and keyboard processing
L Exception Handling
L manage the basic parts defined by the Windows operating system for applications, such as the main window and window class.
Each Delphi application automatically declares an application variable as an application instance. If the Delphi application is not a web server application, the variable must be of the tapplication type. Therefore, tappli-cation does not need to appear in the component palette and cannot be visualized in the Form Designer, therefore, tapplication does not have the published properties ). However, the public properties of tapplication can be set or modified during design, such as in the forms and application pages of the Project | Options dialog box.
II. The following describes most of the attributes, methods, and events in tapplication.
1)Attribute active
Attribute active indicates whether the application is active and has focus.
Defined as follows: Property active: Boolean;
Active is a read-only attribute. If the application is active, the value of active is true. Otherwise, the value of false. The constructor of tapplica-tion sets active to true. If a window or application has focus, the application is active. When the windows of other applications become active, the current application is not active. When the application is closed, the destructor of tapplication sets active to false.
In an application, you can use a timer (ttimer) to check the value of the attribute active to determine whether the current application is active for corresponding processing. You can also define the specified operation in event onactive and event ondeactive.
2)Attribute dialoghandle
The dialoghandle attribute provides a mechanism for Delphi applications to use non-Delphi dialogs. The definition is as follows: Property dialoghandle: hwnd; when using the API function createdialog to create a non-modeled dialog box, you need to use dialoghandle, and you need to view the messages in the application message loop for corresponding operations. For example, when a non-modeled dialog box receives an activation message (wm_ncactivate), it can assign its handle value to dialoghandle. When the dialog box receives a deactivating message, set dialoghandle to 0.
3)Attribute exenameAttribute
Exename contains the path of the executable application file name. The definition is as follows: Property exename: string; exename is a read-only property. Use exename to get the executable file name of the application.
This is a useful property. For example, when running the application program F: \ media \ myapp.exe on the CD, you may need to access the files in the directory F: \ media \ data \ or determine the drive letter of the application. In this case, you can use the functions extractfilepath and extractfilename provided by Delphi to parse the attribute exename to obtain the required information.
4)Property handle
The property handle provides access to the main window handle of the application. The definition is as follows: property handle: hwnd; handle attribute must 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 display on the frontend. Application. Handle is used to construct several windows of the application, so that these windows can be minimized, restored, valid, or invalid in the application. Note: When writing a dynamic link library using a VCL form, you should assign the handle of the main window in the main running program to the application. Handle attribute of the dynamic link library. This makes the form of the dynamic link library part of the main application. In particular, never assign a value to application. handle in the EXE application.
5)Property helpfile
The helpfile attribute specifies the file name that the application uses to display help content. The definition is as follows: Property helpfile: string; helpfile is used to provide a Help file for the application to use the standard windows help system. Windows displays the Help file specified by the helpfile attribute. To enable the application to achieve this, you must assign a file name value to the helpfile attribute at runtime, or specify a Help file on the application page of the Project | Options dialog box during design. By default, helpfile is an empty string (''), and the help method of the application ignores all attempts to display help. If helpfile contains any content, the help topic method will call up the Windows Help System Based on the file name to provide online help
Note: If the Help file in the activity window is specified, the file will take precedence over the display of the Help file of the application.
6)Property hint
The hint attribute specifies the text string that appears in the Help hint box. The property hint: string; tapplication. Hint attribute is the hint value of the control or menu item that the mouse is moving. This attribute can also be assigned a string value that provides the user with action, error, or other information. Therefore, you can use the hint attribute to transfer the prompt information from the control to another display area. For example, you can use the onhint event handle to display the prompt to the status bar. In this case, the hint attribute is read. Briefly describe the status of an application when it is performing an action. In this case, the hint attribute is set. The help prompt appears when an onhint event occurs. Therefore, even if the tapplication hint attribute is assigned the title of the status bar, for example, the title of the status bar control shows the current string value of the hint attribute, it should also be attributed to the onhint event
The hint string consists of two parts: Short prompt and long prompt. The short prompt is used for simple pop-up prompts. The long prompt and short prompt are separated by a vertical line "|", which is 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 break down the hint attribute to get short and long prompts.
Note: When an application provides users with information about an event by setting the hint attribute, remember that by default, when you move the mouse over a control, the hint string is reset to the hint property value of the control.
7)Attribute hinthidepause
The hinthidepause attribute specifies the time interval before the mouse is removed from the control or menu item and the help prompt is hidden. The definition is as follows: Property hinthidepause: integer; Use hinthidepause to specify the wait time in milliseconds. In the tapplication constructor, this property is set to 2500 milliseconds (2.5 seconds ). The help prompt of the control or menu item is specified in the hint Property
Note: by default, the value of hinthidepause should be pre-determined to be 3 to 5 times the value of the hintpause attribute.
8)Attribute mainform
The mainform attribute uniquely identifies the main form of the application. The definition is as follows: Property mainform: tform; the form specified by the attribute mainform is not necessarily equivalent to the main window of the application. The value of the mainform attribute must be the first form in all forms created by createform, but this form may not be the main window of the application. When a new project is created, the mainform attribute value is automatically set to form1. You can use the forms page in the project | Options dialog box to specify one of the multiple forms as mainform.
The mainform attribute cannot be modified at runtime because it is read-only.
The main form is the first form created by the application topic. When the main form is closed, the application is terminated.
In an application, you can call the method application. mainform. close to terminate the application program. You can obtain the application. mainform. top, application. mainform. to determine the position and size of the current active window.
9)Property showmainform
The showmainform attribute determines whether the main form is displayed when the application starts. It is defined as follows: Property showmainform: Boolean; the application uses the showmainform attribute to control whether and when to display its main form.
The tapplication constructor sets showmainform to true. The main form is displayed by default. The mainform attribute specifies the main form.
If you need to hide the main form when the application starts, you should call the pre-showmainform of application. Run in the main project file to be false, and make sure that the visible attribute value of the main form is false. This is useful for many OLE Automation Servers. For example, you can hide the main form of the server program when starting the Automation Service.
In addition, if you need to display a flash when the application starts and make some preparations for the application environment, and disable the display of the main form, this can also be achieved using the showmainform attribute. For example, when Delphi is started, the display of the main form is disabled.
10)Attribute terminated
The attribute terminated reports whether the application receives the Windows message wm_quit for terminating the program. The definition is as follows: Property terminated: Boolean; terminated is a read-only property. This attribute is mainly used to call the processmessages method when the application does not have to try to process Windows messages after it is stopped. When the processmessages method receives the message wm_quit, terminated is set to true
The Delphi application will always receive the message wm_quit because the main form or application is closed or the terminate method is called.
When an application executes a high-intensity operation that consumes a large amount of system resources, it should periodically call the Applicati-on.ProcessMessages method and check the properties application. Terminated to determine whether the operation needs to be terminated to terminate the application
11)Attribute updateformatsettings
The updateformatsettings attribute specifies whether the application automatically updates the format setting when the user changes the system configuration. The definition is as follows: Property updateformatsettings: Boolean; The updateformatsettings attribute application can control automatic update format settings. Tapplication constructor sets this attribute to true. When the application receives the message wm_wininichange, The updateformatsett-ings attribute is checked. We recommend that you use the default format, that is, Windows local settings. You can set updateformatse-ttings to false to avoid changing the format setting during execution of the Delphi application.
12)Attribute updatemetricsettings
The updatemetricsettings attribute specifies whether to update settings such as the font of the prompt window and the icon title. Defined as follows: Property updatemetricsettings: Boolean; The updateformatsettings attribute specifies whether changes in settings such as window font and Icon title are reflected in the application.
The tapplication constructor sets the initial value of updatemetricsettings to true.
14)Method bringtofront
Method bringtofront is used to set the frontend of the last active window in the application to all windows on the desktop. Its definition is as follows: Procedure bringtofront; using the bringtofront method, you can find the last activity window of the main form and place it at the front-end.
The bringtofront method can also be used to test and check whether a window is visible and valid before it becomes the frontend window (Enabled)
For example, when an application receives an email, it may need to activate a special processing program and place it at the frontend of the Windows desktop. In this case, you can call application. bringtofront to implement
15)Method createform
The createform method is used to create a new form ). The definition is as follows: Procedure createform (formclass: tformclass; var reference); Delphi applications always call the createform method. Therefore, it is seldom necessary for programmers to directly call the createform method. The main body of a typical Delphi project in the projectCodeThe createform method is called in one or more ways, and the form creation is automatically controlled when the Form Designer is used. 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 defaults the first form created by calling createform to the main form of the project.
16)Method handleexception
The handleexception method provides the default handle for application exceptions. The definition is as follows: Procedure handleexception (Sender: tobject); The handleexception method is very useful for writers who compile specific components, because it can generate an event that does not have to respond to Windows messages. In the application, you can use the onexception event handle to control other abnormal operations in the custom code.
In the application code, if the exception skips all try blocks, the application automatically calls the handleexception method and displays a dialog box Indicating an error. Unless the exception object is eabort, handleexception will call the onexception handle (if any). Otherwise, showexception will be called to display a dialog box Indicating an error.
17)Method unhookmainwindow
The unhookmainwindow method is used to release a program that is hung on the main form by the hookmainwindow method. The definition is as follows: Type tsf-whook = function (VAR message: tmessage): Boolean of object; Procedure unhookmainwindow (HOOK: tsf-whook); Use unhookmainwindow to release the hook window. Specify the Dialog Box Process in parameter hook
The Tsung whook type is a parameter that calls the hookmainwindow method. This parameter is a non-Delphi dialog box that calls the method pointer of the conversation program. The dialog program is similar to the window program. It processes messages for the dialog box, but the syntax is different.
The onactivate event occurs when the application becomes active. The definition is as follows: Type tpolicyevent = procedure (Sender: tobject) of object; property onactivate: tpolicyevent; write an event handle with onactive event to specify special processing when the application becomes active
When a Windows application is initially running or its focus is transferred from another Windows application back to the current application, the application becomes active.
18)Event ondeactivate
The ondeactivate event occurs when the application becomes inactive. The definition is as follows: Type tpolicyevent = procedure (Sender: tobject) of object; property ondeactivate: tpolicyevent; ondeactive event can be triggered immediately before the application becomes inactive, this completes the special processing specified in the event handle. When a user switches from the current application to another application, the ondea-ctive event of the current application occurs.
19)Event onexception
An onexception occurs when a non-handle exception occurs in the application. The definition is as follows: Type texceptionevent = procedure (Sender: tobject; E: exception) of object; property onexception: texceptionevent; the onexception event can be used to change the default action when a non-handle exception occurs in the application. In the tapplication. handleexception method, the onexception event handle is automatically called.
Onexception events are only used to handle exceptions that occur during message processing. The onexception event will not occur if an exception occurs before or after application. Run.
If an exception is ignored in the try block of the application code, the application automatically calls the handleexc-eption method. Unless the exception object is eabort, handleexception will call the onexception handle (if any). Otherwise, showexception will be called to display a dialog box Indicating an error.
The texceptionevent type is the onexception event type, which points to an exception handling method in the application. The parameter sender is the object that causes an exception, and the parameter E is the exception object.
20)Event onhelp
The onhelp event occurs when the application receives a help request. Definition: Type thelpevent = function (command: word; Data: longint; var callhelp: Boo-lean): Boolean of object; property onhelp: thelpevent; write an event handle with onhelp to complete special processing when there is a request for help. The helpcontext method and the help-jump Method automatically trigger the onhelp event.
After an event occurs, set callhelp to true so that VCL can call winhelp. Set callhelp to false to prevent VCL from calling winhelp. All the help-related methods in the Delphi application go through the onhelp event. Winhelp is called only when the callhelp parameter in the onhelp event returns true or the onhelp event is not specified with a valid handle.
21)Event onhint
When the mouse pointer moves over a control or menu item and the control or menu item displays a help prompt, the event onhint occurs. The definition is as follows: Type tpolicyevent = procedure (Sender: tobject) of object; property onhint: tpolicyevent; the event handle written with onhint can perform the specified operation when an onhint event occurs. When the user parks the mouse pointer on a control and the hint property value of the control is not a null string (''), The onhint event will occur.
Generally, the value of the onhint event display control or menu item hint attribute is used as the title of a panel control (such as tstatusbar). Therefore, the Panel is used as the status bar ). When an onhint event occurs, the hint attribute is usually specified as a help hint and a long prompt (longer hint) that is displayed elsewhere)
22)Event onidle
The onidle event occurs when the application becomes idle. Define as follows: Type tidleevent = procedure (Sender: tobject; var done: Boolean) of object; property onidle: tidleevent write an event handle using onidel to complete the specified operation when the application is idle. When the application does not execute any code, it is idle. For example, the application is idle when the application is waiting for user input.
The tidelevent type is the onidel event type, which points to a method that runs when the application is idle. The tidelevent object has a cloth with the default value "true" and the variable "done. When done is true, the Windows API function waitm-essage will be called when onidel returns. Waitmessage enables other applications to get control focus until a new message appears in the message queue of the application. When the parameter done is false and the application is not busy, the application will not give control focus to other applications.
When the application changes to idle state, the onidel event only occurs once, And the next onidel event may not occur until the value of the done parameter is set to true. Setting the value of "done" to "false" in the application eliminates the disordered CPU time count, which may affect the performance of the entire system.
Delphi program example
Interface
Uses
Windows, messages, sysutils, classes, graphics, controls, forms, dialogs, comctrls;
Type
Tform1 = Class (tform)
Statusbar1: tstatusbar;
Procedure formcreate (Sender: tobject );
Private
{Private Declarations}
Public
Procedure customdisplayhint (Sender: tobject );
// Line 16 {public declarations}
End;
Varform1: tform1;
Implementation
{$ R *. DFM}
Procedure tform1.customdisplayhint (Sender: tobject );
Begin
Statusbar1.simpletext: = getlonghint (application. Hint );
// Line 29
End;
Procedure tform1.formcreate (Sender: tobject );
Begin
Application. onhint: = customdisplayhint;
// Line 34
End;
End.
In the code above, Row 3 is the developer-defined customdisplayhint method for the event onhint;
Row 29th is the implementation part of this method. Long prompts in application. Hint are parsed and displayed in the status bar;
Line 2 associates the customdisplayhint method with the handle of the event application. onhint when the form is created.
When the mouse moves and stays on a control (such as a button), if the showhint attribute of the Host object at each level of the control (if any) the value is true, and the showhint attribute value is also true. The short prompt in the hint attribute value of the control will be displayed as a prompt window floating, the corresponding long prompt is displayed in the status bar according to the operation specified in the event handle.