If App.PrevInstance Then
   这里可能要用API找窗口,得到窗口句柄,然后用SetWindowPos ...
End If

解决方案 »

  1.   

    实际上
    If App.PrevInstance Then
      DoEvents
    End If就行了,何必那么麻烦!
      

  2.   

    注:我想让先前运行的程序跳到前台,不知可用什么方法?通过窗体句柄吗?可是在MDI窗体中我也无法确定究竟哪个子窗体处于最前面,可有什么好方法解决?
      

  3.   

    以下是你可能用到的三个API函数
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As LongPrivate Sub Form_Load()
        Dim prevhWnd As Long, lRet
        If App.PrevInstance Then
            prevhWnd = FindWindow("ClassName(这里用什么不太清楚)", "Your App Title")
            lRet = ShowWindow(prevhWnd, 9)
            End
        End If
    End Sub
      

  4.   

    ClassName好像用vbNullString就可以了
      

  5.   

    取最前窗体和设置最前窗体好像是getfrontwindow和setfrontwindow两个api,
    也许我记得不准确,但至少前四个字母肯定是正确的,查一下就知
      

  6.   

    没想到找窗口这么麻烦!!!Option ExplicitPublic Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As LongPublic Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
    Public Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
    Public Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As LongDim sPattern As String, hFind As LongPublic Function EnumWinProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
            Dim k As Long, sName As String
        
        
            If IsWindowVisible(hWnd) And GetParent(hWnd) = 0 Then
                sName = Space$(128)
                k = GetWindowText(hWnd, sName, 128)
        
        
                If k > 0 Then
                    sName = Left$(sName, k)
                    If lParam = 0 Then sName = UCase(sName)
                        If sName Like sPattern Then
                            hFind = hWnd
                            EnumWinProc = 0
                            Exit Function
                        End If
                    End If
                End If    EnumWinProc = 1
    End Function
    Public Function FindWindowWild(sWild As String, Optional bMatchCase As Boolean = True) As Long
        sPattern = sWild
        If Not bMatchCase Then sPattern = UCase(sPattern)
        Dim lRet As Long
        lRet = EnumWindows(AddressOf EnumWinProc, bMatchCase)
        FindWindowWild = hFind
    End Function'调用方式
    'prevhWnd=FindWindowWild("You Window Caption")
      

  7.   

    我对切换到前台的方法没有歧意,但是,都是已知窗口,用SaveSetting 保存窗口信息,在下次运行时读取,不是更好吗?
      

  8.   

    最好写到Sub Main里面,如下
    Sub Main()
        Dim prevhWnd As Long, lRet
        If App.PrevInstance Then
            prevhWnd = FindWindowWild("You Window Caption")
            lRet = ShowWindow(prevhWnd, 9)
            lRet = SetForegroundWindow(prevhWnd)
            End
        End If
        Load frmMain
        frmMain.Show
    End Sub其中
    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long