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 = 0Private Const SINGLE_INSTANCE_OPEN = 1Private Function MultiInst() As Integer
Dim 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 IfEnd FunctionSub Main()
Dim MultiInstResult As Integer
MultiInstResult = MultiInst
If MultiInstResult = OPEN_APPLICATION Then
msgbox "运行程序"
ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then'退出
End
End IfEnd Sub |