Private Sub Form_Load()
If App.PrevInstance = True Then
MsgBox "程序已经在运行", , "工程一"
Unload Me
End
End If
End Sub
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const WinTxt = "Form1" '修改成自己程序窗口的标题
Private Sub Form_Load()
Dim ChildHwnd As Long, Tmp As String * 255
If App.PrevInstance Then
ChildHwnd = GetWindow(GetDesktopWindow, 5)
Do While ChildHwnd <> 0&
If IsWindowVisible(ChildHwnd) Then
GetWindowText ChildHwnd, Tmp, 255
If InStr(Tmp, WinTxt) > 0 Then
ShowWindow ChildHwnd, 9
SetForegroundWindow ChildHwnd
End If
End If
ChildHwnd = GetWindow(ChildHwnd, 2)
Loop
End
End If
End Sub
vb.net中就可以直接设置选项,只运行一个实例吧。