以下是你可能用到的三个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
没想到找窗口这么麻烦!!!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")
最好写到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
If App.PrevInstance Then
DoEvents
End If就行了,何必那么麻烦!
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
也许我记得不准确,但至少前四个字母肯定是正确的,查一下就知
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")
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