If App.PrevInstance = True Then Dim cap As String cap = Me.Caption Me.Caption = Me.Caption & "1" MsgBox "程序已经在运行" AppActivate cap, True Unload MeExit Sub End If
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As LongPrivate Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As LongConst SW_RESTORE = 9Private Const OPEN_APPLICATION = 0 Private Const SINGLE_INSTANCE_OPEN = 1Sub Main() Dim MultiInstResult As IntegerMultiInstResult = MultiInstIf MultiInstResult = OPEN_APPLICATION Then Form1.Show ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then End End If End SubPrivate Function MultiInst() As IntegerDim hwndFound As Long Dim strWindowName strWindowName = App.Title App.Title = "temp title" hwndFound = FindWindow(vbNullString, strWindowName)If hwndFound Then MultiInst = SINGLE_INSTANCE_OPEN MsgBox "A instance of the application is already open." & vbCrLf & vbCrLf & "Only one open instance allowed.", vbOKOnly + vbExclamation, "App Name" If IsIconic(hwndFound) Then ShowWindow hwndFound, SW_RESTORE SetForegroundWindow hwndFound Else SetForegroundWindow hwndFound End If ElseIf hwndFound = 0 Then App.Title = strWindowName MultiInst = OPEN_APPLICATION End If End Function
Dim cap As String
cap = Me.Caption
Me.Caption = Me.Caption & "1"
MsgBox "程序已经在运行"
AppActivate cap, True
Unload MeExit Sub
End If
Private Const SINGLE_INSTANCE_OPEN = 1Sub Main()
Dim MultiInstResult As IntegerMultiInstResult = MultiInstIf MultiInstResult = OPEN_APPLICATION Then
Form1.Show
ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then
End
End If
End SubPrivate Function MultiInst() As IntegerDim hwndFound As Long
Dim strWindowName strWindowName = App.Title
App.Title = "temp title"
hwndFound = FindWindow(vbNullString, strWindowName)If hwndFound Then
MultiInst = SINGLE_INSTANCE_OPEN
MsgBox "A instance of the application is already open." & vbCrLf & vbCrLf & "Only one open instance allowed.", vbOKOnly + vbExclamation, "App Name" If IsIconic(hwndFound) Then
ShowWindow hwndFound, SW_RESTORE
SetForegroundWindow hwndFound
Else
SetForegroundWindow hwndFound
End If
ElseIf hwndFound = 0 Then
App.Title = strWindowName
MultiInst = OPEN_APPLICATION
End If
End Function
http://expert.csdn.net/Expert/topic/2689/2689086.xml?temp=.5642053
http://expert.csdn.net/Expert/topic/2683/2683490.xml?temp=.7050135
自己加上对命令行的处理