Implementing a form to prevent multiple instances and transfer commands with DDE

zhaozj2021-02-17  46

Use DDE technology to increase your app

Friends who go online must have used network ants (net Ants)? I don't know if you haven't gained it during use, that is if you want to transfer two "ants" for your effect. It will always close the new run. This is easy to implement in VB: private suform_load () if app.previnstance dam "You have run this application" end "Exit the newly running program end if End Sub so if you run this program after running it It pops up a message box and refuses to run again. This is very easy. The "ant" program is that it is not only refused to run when repeated "ants", but also activates the "ants" that has been run, so it is powerless to use the above procedure. But in fact, there are many ways to reject running and activate running procedures:

1. Use the FindWindow function to get the handle (HWnd) that has run the form, and then activate it with the API function such as SetActiveWindow. Its shortcomings are also obvious, that is, it is not possible to pass parameters.

2. Use the FindWindow function to get a custom message (with parameters) with the SENDMESSAGE method, then intercept and process in the form, but do the standard message to modify the form to modify the standard message to modify the form. The handler is OK, in the program written in VC, BC or Delphi, but the workload in VB is too large, and it is easy to "generally protect the line error" so that the VB crashed, not taking advantage of (of course, if you have enough Confidence and the spirit of unfarest collapse, you can also try ^ _ ^).

3, use DDE technology.

The so-called DDE technology is dynamic data exchange technology. Maybe you are strange, this is the same as the content discussed in this article? And listen to me slowly. In order to achieve rejecting and activating and implementing various functions, we can first mention the method you mentioned in this article, detect that the program is running, if not, it is running, if it has been run, Just open the DDE channel with it, pass it (or some) data, and then process the data from the running program, then implement various "unexpected" features, maybe someone is your own The program shouted: "Cool, cool ..." ^ _ ^ is good, ear listening is empty, seeing is true, let us move to true.

Open VB, create a new project, select "Project -> Engineering 1 Properties" in the menu, change the project name to "P1" (I love lazy, it can be short, short ^ _ ^), put existing forms "Linktopic" property is changed to "formdde", change the "linkmode" property to "1 - source", add a PictureBox control as DDE execution control, named Picdde. Then add a TextBox control, named "txtinfo", and set the "multiline" property to "True" to display multiple lines, as a message display control.

Finally, enter the following code in the form code area:

Const commandline = "commandline =" "is still in order to save things, define a constant

Private Sub Form_LinKexecute (cmdstr as string, cancel as integer) static lngcount as longdim info as stringInfo = txtinfo.text 'Reserved original information

SELECT CASE CMDSTR 'CMDSTR is the parameter of the DDE program transferred Case "Max" ME.WindowState = 2 Info = INFO VBNEWLINE "form has been maximized" case "showtime" info = INFO VBNEWLINE "last run this The time of the program is: " Str (now) Case" count "lngcount = lngcount 1 info = info vbnewline " You have already " Str (LNGCOUNT) " times repeat call this program. "_ VbNewline " But I am afraid that you don't give salary, so only one ^ _ ^ "end SELECT

If LEFT (CMDSTR, LEN (CommandLine)) = commandline dam = info vbnewline "new program runs" VBNewline "command behavior in the command line:" _ VBNewline Right (cmdstr, len (cmdstr) - Len (commandline)) End IF

TXTINFO.TEXT = INFO 'Shows the information

Cancel = false End Sub

Private sub LinkandsendMessage (Byval Msg As String) DIM T as longpicdde.linkmode = 0 '--picdde.linktopic = "p1 | formdde" | ______ connect DDE programs and send data / parameters Picdde.LinkMode = 2' | | "For pipe characters, it is the vertical line next to" retreat ", Picdde.LinKexecute Msg '- is not letters or numbers!

T = picdde.linktimeout '--picdde.linktimeout = 1' | ______ Terminate the DDE channel. Of course, you can also use other methods picdde.linkmode = 0 '| This is the timeout mandatory terminated method Picdde.LinkTimeout = T' --END SUB

PRIVATE SUB FORM_LOAD () if app.previnstance The program is running

Me.LinkTopic = "" "These two lines are used to clear the DDE server properties of the newly running program, and Me.LinkMode = 0 'may make a mess when connecting the DDE program.

LinkandsendMessage "Max" '- linkandsendMessage "count" | ----- Connect DDE Accept Procedures and Transfer Data / Parameters LinkandsendMessage "ShowTime"'

If Command <> "" "If there is a command line parameter, pass the LinkAndsendMessage commandline command end if end 'to end the new program End IFend Sub

Test: Compile the project "P1" into an EXE file (set name p1.exe) 1. Open "My Computer" and find P1.exe and execute. It can be seen that the program is running normally. 2. Run again, this new program has no success, and the original running program is maximized, and the following characters in the text box:

The form has been maximized. You have repeatedly called this program. But I am afraid that you don't give salary, so only one ^ _ ^ last runs this program is: 00-2-6 7:11:01

3, open the MS-DOS mode, run the program again with the command line, such as "p1 how are you?" At this time, there are several lines in the program text boxes:

The form has been maximized. You have repeatedly called this program twice. But I am afraid that you don't give salary, so only one ^ _ ^ last run this program is: 00-2-6 7:14:32 New program runs command behavior in the command line: how are you?

OK, run is completely correct, then you can apply it to your program.

Of course, this is just some personal experience, if there is an omission, please refer to prawn.

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

New Post(0)