Prevent procedures from being repeated

xiaoxiao2021-03-06  14

------------------------------ Prevent procedures are repeated (first method)

The procedures for Windows 95 generally can be repeatedly executed, for example, you press the WIN E key key to start the resource manager, if you press the WIN E key button, the two programs do not interfere with each other. Sometimes you can make such a program: When the program has executed, if the user attempts to execute the program again, only the executed program is activated, not a copy. The core of completing this purpose is to find that the program has been run when the program starts, I have tried many ways, including writing a specific string to the "global atom table", but the easiest way Still here:

Temporarily change the value of the Title feature field of Application when the program starts. Use the Windows API Function FindWindows () Find Window to restore Application's title value

The above steps generally implement in the oncreate event of the main form, as follows:

Procedure TFORM1.FORMCREATE (Sender: TOBJECT); var zappname: array [0..127] of char; hold: string; found: hwnd; bound: hwnd; begin hold: = Application.title; Application.Title: = 'Onlyone ' INTOSTR (Hinstance); // Temporarily modify the window title Strpcopy (ZAPPNAME, HOLD); // Original window Title Found: = FindWindow (nil, zappname); // Find window Application.title: = HOLD; // Restore Window Title if Found <> 0 THEN Begin // If you find the running program and end your own showWindow (Found, SW_RESTORE); Application.Terminate; End;

Related article: Prevent procedures from being repeated (another method)

The key to realizing the single instance operation is to determine if the previous instance exists, the program running in Win3.x can know the handle of the previous instance, so that it is convenient to determine, but Windows 95 is a pre-standard multitasking system, the procedure of the program One example handle is constant to zero, so only other ways are found. The most effective way is to be judged by looking at whether there is a routine that has the same window class name. The method implemented in Delphi is described below.

1. Changes to the main window program:

In the main window (ie, the first window created by the program) Add const cm_restore = WM_USER $ 1000; {Custom "Restore" message} myappname = "My Delphi Program"; and in the Public section defined in Form Add procedure CreateParams (var Params: TCreateParams); override; Procedure RestoreRequest (var message: TMessage); message CM_RESTORE; Add {name} procedure TForm1.CreateParams window specified in the implementation section (var Params: TCreateParams); begin inherited CreateParams (Params Params.winclassName: = myAppname;

{Processing "resume" message} procedure TForm1.RestoreRequest (var message: TMessage); procedure TForm1.RestoreRequest (var message: TMessage); begin if IsIconic (Application.Handle) = TRUE then Application.Restore else Application.BringToFront; end; After modification, the class name of the main window of the program has been specified, which is the basis for judging. It is generally judged when the order is just running, so it is necessary to modify the DPR file.

2. Changes to DPR files

Add Windows in the UseS section, the two units are added to the following statements, pay attention to the definition of constant cm_restore and myAppname in the two files must be consistent const cm_restore = WM_USER $ 1000; {Custom "Restore"} myappname = "My Delphi "VAR RVHANDLE: HWND;

Insert the following statement to the forefront (before Application.initialize)

RvHandle: = FindWindow (MyAppname, NIL); if RvHandle> 0 Then Begin PostMessage (RvHandle, CM_RESTORE, 0, 0); EXIT; END; This program means that if you find a class name, you can find the same window, then you Send a message and exit, and this example is automatically activated or restored from the icon in this example in the original window to achieve the purpose of avoiding secondary operation and automatically calling out the previous routine.

Excerpt from: http://www.delphibbs.com

转载请注明原文地址:https://www.9cbs.com/read-67674.html

New Post(0)