Option Explicit
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 = 9'Conatants used for the return of the MultiInst function
Private Const OPEN_APPLICATION = 0
Private Const SINGLE_INSTANCE_OPEN = 1Sub Main()
Dim MultiInstResult As Integer'Call procedure to determine if an instance of
'the application is already loaded
MultiInstResult = MultiInst'Handle the result from the MultiInst function
If MultiInstResult = OPEN_APPLICATION Then
     Form1.Show  
     'No instance of the application is already open,
     'continue to load the login form
ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then
   'An instance already exists cancel the 
   'current application load
    End
End If
End SubPrivate Function MultiInst() As Integer
'This function determines if a single instance of the 
'application is already running.Dim hwndFound As Long   'The window handle
Dim strWindowName       'The Caption on the window'Set the caption of the application form
strWindowName = App.Title
App.Title = "temp title"  'set application title as temporary string'Get the handle of the application if it is open
hwndFound = FindWindow(vbNullString, strWindowName)If hwndFound Then
     'Set the function return
     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 application minimized, restore, show it on top
     If IsIconic(hwndFound) Then
          ShowWindow hwndFound, SW_RESTORE
          'Show the window infront of all other windows
          SetForegroundWindow hwndFound
     Else
          'Bring the application top most on the screen
          SetForegroundWindow hwndFound
    End If
ElseIf hwndFound = 0 Then
    'Set the function return so it will continue loading
    App.Title = strWindowName    'restore application title
    MultiInst = OPEN_APPLICATION
End If
End Function

解决方案 »

  1.   

    Option Explicit
    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 = 9'Conatants used for the return of the MultiInst function
    Private Const OPEN_APPLICATION = 0
    Private Const SINGLE_INSTANCE_OPEN = 1Sub Main()
    Dim MultiInstResult As Integer'Call procedure to determine if an instance of
    'the application is already loaded
    MultiInstResult = MultiInst'Handle the result from the MultiInst function
    If MultiInstResult = OPEN_APPLICATION Then
         Form1.Show  
         'No instance of the application is already open,
         'continue to load the login form
    ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then
       'An instance already exists cancel the 
       'current application load
        End
    End If
    End SubPrivate Function MultiInst() As Integer
    'This function determines if a single instance of the 
    'application is already running.Dim hwndFound As Long   'The window handle
    Dim strWindowName       'The Caption on the window'Set the caption of the application form
    strWindowName = App.Title
    App.Title = "temp title"  'set application title as temporary string'Get the handle of the application if it is open
    hwndFound = FindWindow(vbNullString, strWindowName)If hwndFound Then
         'Set the function return
         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 application minimized, restore, show it on top
         If IsIconic(hwndFound) Then
              ShowWindow hwndFound, SW_RESTORE
              'Show the window infront of all other windows
              SetForegroundWindow hwndFound
         Else
              'Bring the application top most on the screen
              SetForegroundWindow hwndFound
        End If
    ElseIf hwndFound = 0 Then
        'Set the function return so it will continue loading
        App.Title = strWindowName    'restore application title
        MultiInst = OPEN_APPLICATION
    End If
    End Function
      

  2.   

    Public Sub Main()
        
        If App.PrevInstance = True Then
            MsgBox "Your application is already runing "
            Exit Sub
        End If
        
    End Sub
    不知道你要的是不是这种效果
      

  3.   

    自己是没有办法把自己提起来的,所以通过程序自身好像没有办法。
    可以考虑别的手段,如注册表之类的,不过好像也不能保证仅执行嗯,期待ing...
      

  4.   

    上面的例子能够只运行一个实例,还能把前一个实例激活,如果只想运行一个实例,可以简单如下
    sub main
          if app.previnstance then
               msgbox "quit"
               end
          end if
    end sub
      

  5.   

    If IIf(App.PrevInstance, True, False) then 
        msgbox "该程序已经运行"
     End if