Private Sub Form_ Load If App.Previnstance Then End End Sub
to bfqu_yuj(泡泡满天飞):如果是vb编写的,即使窗口最小化了也是有效的。不知你试了没有?
Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate mlMainForm As Long Private msMainForm As StringPrivate Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const WM_SYSCOMMAND = &H112 Private Const SC_RESTORE = &HF120& '激活窗口 Private Sub ActivateWin(ByVal lHwnd As Long) Call SetForegroundWindow(lHwnd) Call SendMessage(lHwnd, WM_SYSCOMMAND, SC_RESTORE, 0) End Sub'枚举窗口回调函数 Private Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean On Error Resume Next Dim sBuffer As String * 255, sText As String Call GetWindowText(hwnd, sBuffer, 256) sText = StrZToStr(sBuffer) If msMainForm <> "" Then sText = Mid(sText, 1, Len(msMainForm)) If sText = msMainForm Then mlMainForm = hwnd End If EnumChildProc = True End FunctionPrivate Function StrZToStr(s As String) As String If InStr(1, s, Chr$(0)) > 0 Then StrZToStr = Left$(s, InStr(1, s, Chr$(0)) - 1) Else StrZToStr = s End If End FunctionSub Main() On Error Resume Next Dim lDHwnd As Long If App.PrevInstance Then lDHwnd = GetDesktopWindow '打开窗口 msMainForm = "Form1" If lDHwnd <> 0 Then Call EnumChildWindows(lDHwnd, AddressOf EnumChildProc, 0) If mlMainForm <> 0 Then Call ActivateWin(mlMainForm) End Else frmMain.Show End If Else frmMain.Show End If End Sub '修改工程的启动对象为 Sub Main '把msMainForm赋值为窗体的标题属性
If App.Previnstance Then End
End Sub
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate mlMainForm As Long
Private msMainForm As StringPrivate Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SYSCOMMAND = &H112
Private Const SC_RESTORE = &HF120&
'激活窗口
Private Sub ActivateWin(ByVal lHwnd As Long)
Call SetForegroundWindow(lHwnd)
Call SendMessage(lHwnd, WM_SYSCOMMAND, SC_RESTORE, 0)
End Sub'枚举窗口回调函数
Private Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
On Error Resume Next
Dim sBuffer As String * 255, sText As String
Call GetWindowText(hwnd, sBuffer, 256)
sText = StrZToStr(sBuffer)
If msMainForm <> "" Then
sText = Mid(sText, 1, Len(msMainForm))
If sText = msMainForm Then mlMainForm = hwnd
End If
EnumChildProc = True
End FunctionPrivate Function StrZToStr(s As String) As String
If InStr(1, s, Chr$(0)) > 0 Then
StrZToStr = Left$(s, InStr(1, s, Chr$(0)) - 1)
Else
StrZToStr = s
End If
End FunctionSub Main()
On Error Resume Next
Dim lDHwnd As Long
If App.PrevInstance Then
lDHwnd = GetDesktopWindow
'打开窗口
msMainForm = "Form1"
If lDHwnd <> 0 Then Call EnumChildWindows(lDHwnd, AddressOf EnumChildProc, 0)
If mlMainForm <> 0 Then
Call ActivateWin(mlMainForm)
End
Else
frmMain.Show
End If
Else
frmMain.Show
End If
End Sub
'修改工程的启动对象为 Sub Main
'把msMainForm赋值为窗体的标题属性