1个回答
展开全部
vb中只允许运行一个程序实例的方法 利用vb中的App.PrevInstance即可,然后利用AppActivate来将前一个程序呼到前台.
Private Sub Main() If App.PrevInstance Then '程序是否已运行 Dim Title As String Title = App.Title App.Title = "The Sec"On Error Resume Next AppActivate Title Exit Sub End If
...... '正常运行时的代码
End Sub
这样写的话,如果前一个程序窗口为Hide,就不会将窗口显示并提前,如果要让窗口为隐藏的时候也可以让窗口提前,则需要获得窗口的hwnd,然后将窗口先显示出来.
先声明两个api,及相关的常量:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongDeclare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Const SW_HIDE = 0Public Const SW_SHOWNORMAL = 1Public Const SW_NORMAL = 1Public Const SW_SHOWMINIMIZED = 2Public Const SW_SHOWMAXIMIZED = 3Public Const SW_MAXIMIZE = 3Public Const SW_SHOWNOACTIVATE = 4Public Const SW_SHOW = 5Public Const SW_MINIMIZE = 6Public Const SW_SHOWMINNOACTIVE = 7Public Const SW_SHOWNA = 8Public Const SW_RESTORE = 9Public Const SW_SHOWDEFAULT = 10Public Const SW_FORCEMINIMIZE = 11Public Const SW_MAX = 11
然后改写代码:
Private Sub Main() If App.PrevInstance Then '程序是否已运行 Dim Title As String Title = App.Title App.Title = "The Sec"On Error GoTo FirstHide AppActivate Title Exit Sub End If
...... '正常运行时的代码
Exit SubFirstHide: Dim hwnd As Long hwnd = FindWindow(vbNullString, Title) If hwnd <> 0 Then ShowWindow hwnd, SW_SHOWNORMAL AppActivate Title End IfEnd Sub
这样,当窗口为Hide的时候,程序将跳转到错误处理语句FirstHide处,获得前一个窗口的句柄并显示窗口,然后再利用AppActivate将窗口提到前台.
退出时弹出提示 退出?
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Dim strExit As String If UnloadMode <> vbAppWindows Then strExit = "您确定要停止运行吗?" If vbNo = MsgBox(strExit, vbQuestion Or vbYesNo, "") Then Cancel = True Exit Sub End If End If End End Sub
Private Sub Main() If App.PrevInstance Then '程序是否已运行 Dim Title As String Title = App.Title App.Title = "The Sec"On Error Resume Next AppActivate Title Exit Sub End If
...... '正常运行时的代码
End Sub
这样写的话,如果前一个程序窗口为Hide,就不会将窗口显示并提前,如果要让窗口为隐藏的时候也可以让窗口提前,则需要获得窗口的hwnd,然后将窗口先显示出来.
先声明两个api,及相关的常量:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongDeclare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Const SW_HIDE = 0Public Const SW_SHOWNORMAL = 1Public Const SW_NORMAL = 1Public Const SW_SHOWMINIMIZED = 2Public Const SW_SHOWMAXIMIZED = 3Public Const SW_MAXIMIZE = 3Public Const SW_SHOWNOACTIVATE = 4Public Const SW_SHOW = 5Public Const SW_MINIMIZE = 6Public Const SW_SHOWMINNOACTIVE = 7Public Const SW_SHOWNA = 8Public Const SW_RESTORE = 9Public Const SW_SHOWDEFAULT = 10Public Const SW_FORCEMINIMIZE = 11Public Const SW_MAX = 11
然后改写代码:
Private Sub Main() If App.PrevInstance Then '程序是否已运行 Dim Title As String Title = App.Title App.Title = "The Sec"On Error GoTo FirstHide AppActivate Title Exit Sub End If
...... '正常运行时的代码
Exit SubFirstHide: Dim hwnd As Long hwnd = FindWindow(vbNullString, Title) If hwnd <> 0 Then ShowWindow hwnd, SW_SHOWNORMAL AppActivate Title End IfEnd Sub
这样,当窗口为Hide的时候,程序将跳转到错误处理语句FirstHide处,获得前一个窗口的句柄并显示窗口,然后再利用AppActivate将窗口提到前台.
退出时弹出提示 退出?
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Dim strExit As String If UnloadMode <> vbAppWindows Then strExit = "您确定要停止运行吗?" If vbNo = MsgBox(strExit, vbQuestion Or vbYesNo, "") Then Cancel = True Exit Sub End If End If End End Sub
追问
菜鸟选手 请见谅!!
Dim Title As String Title = App.Title App.Title = "The Sec"On Error GoTo FirstHide AppActivate Title
能解释下吗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询