vb禁止程序多开

[复制链接]
查看11 | 回复1 | 2008-4-28 13:21:44 | 显示全部楼层 |阅读模式
不用那么麻烦的,把这段加入form_load里就可以了。Private Sub Form_Load()If App.PrevInstance Then
MsgBox "程序已在运行。", vbInformation, "系统"
EndEnd IfEnd Sub
回复

使用道具 举报

千问 | 2008-4-28 13:21:44 | 显示全部楼层
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As LongPrivate Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As LongConst SW_RESTORE = 9Private Const OPEN_APPLICATION = 0Private Const SINGLE_INSTANCE_OPEN = 1Private Function MultiInst() As Integer
Dim hwndFound As Long
Dim strWindowName
strWindowName = App.title
App.title = "temp title"
hwndFound = FindWindow(vbNullString, strWindowName)
If hwndFound Then
MultiInst = SINGLE_INSTANCE_OPEN
'MsgBox "A instance of the application is already open." & vbCrLf & vbCrLf & "Only one open instance allowed.", vbOKOnly + vbExclamation, "App Name"
If IsIconic(hwndFound) Then
ShowWindow hwndFound, SW_RESTORE
SetForegroundWindow hwndFound
Else
SetForegroundWindow hwndFound
End If
ElseIf hwndFound = 0 Then
App.title = strWindowName
MultiInst = OPEN_APPLICATION
End IfEnd FunctionSub Main()
Dim MultiInstResult As Integer
MultiInstResult = MultiInst
If MultiInstResult = OPEN_APPLICATION Then
msgbox "运行程序"
ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then'退出
End
End IfEnd Sub
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行