首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

怎样限制一个exe只能打开一次?解决办法

2012-01-28 
怎样限制一个exe只能打开一次?怎样限制一个exe只能打开一次?[解决办法]If App.PrevInstance ThenMsgBox程

怎样限制一个exe只能打开一次?
怎样限制一个exe只能打开一次?

[解决办法]
If App.PrevInstance Then
MsgBox "程序已经运行,不能再次装载。 ", vbExclamation + vbOKOnly, "系统消息 "
Unload Me
Exit Sub
End If
[解决办法]
If App.PrevInstance Then
MsgBox "程序已经运行,不能再次装载。 ", vbExclamation + vbOKOnly, "系统消息 "
Unload Me
End '
End If

以上确实可以,不过如果同一个exe文件在两个目录下都有,以上方法就不行了。
更彻底的方法:用API函数遍历当然所有窗体,然后判断是否已经打开:

'在模块里添加如下代码
Public Declare Function EnumWindows& Lib "user32 " (ByVal lpEnumFunc As Long, ByVal lParam As Long)
Public Declare Function GetWindowText Lib "user32 " Alias "GetWindowTextA " (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Function EnumRunOnce(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean '遍查主窗口,使用程序只运行一次
Dim buf As String * 1024
Dim Length As Long
Dim Title As String
Length = GetWindowText(app_hwnd, buf, Len(buf))
Title = Left$(buf, Length)
If InStr(Title, Form1.Caption) Then '判断是否为执行你想要的操作,Form1是主窗体,标题起的尽可能不会重复
If app_hwnd <> Form1.hwnd Then '程序同时只能打开一次
End
End If
End If
EnumRunOnce = 1
End Function

'在主窗体中中添加如下代码:
Private Sub Form_Load()
EnumWindows AddressOf EnumRunOnce, 0& '限定只能运行一次
End Sub

热点排行