复制下面代码,编译成exe文件,试试看。
Option Explicit
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount 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 Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Function GetWinClass(Hwd As Long) As String
Dim retvalue As Long, TempStr As String * 254
retvalue = GetClassName(Hwd, TempStr, 254)
GetWinClass = StrConv(LeftB(StrConv(TempStr, vbFromUnicode), retvalue), vbUnicode)
End Function
Public Function GetWinText(ByVal hwnd As Long) As String
Dim i As Long, Title As String
Title = String(255, Chr(0))
i = GetWindowText(hwnd, Title, Len(Title) - 1)
GetWinText = StrConv(LeftB(StrConv(Title, vbFromUnicode), i), vbUnicode)
End Function
Private Sub Form_Load()
If App.PrevInstance Then '如果重复启动
Dim Hwd As Long, mhwnd As Long, HwdOld As Long
Dim i As Integer, Title As String, ClassName As String
mhwnd = Me.hwnd
ClassName = GetWinClass(mhwnd)
Title = GetWinText(mhwnd)
HwdOld = mhwnd
mhwnd = 0
Hwd = FindWindowEx(mhwnd, 0, ClassName, Title)
Do Until Hwd = 0
If Hwd <> HwdOld Then '找到先前启动的本程序
ShowWindow Hwd, 1
SetForegroundWindow Hwd
Exit Do
End If
Hwd = FindWindowEx(mhwnd, Hwd, ClassName, Title)
Loop
End
End If
End Sub
Private Sub Form_Load()
If App.PrevInstance Then
MsgBox "系统不允许程序运行多次,请关闭后再试。", vbInformation, "系统提示"
End If
End Sub
if App.PrevInstance then end
这种要求 App.PrevInstance 就满足不了了,这需要进程和窗口遍历了
c作起来简单,VB就有点麻烦了,c代码要不要
If App.PrevInstance Then
MsgBox ("已经运行了一个程序!")
End
End If
If App.PrevInstance = True Then End