如何让VB程序防止打开多个的问题?
我想要的效果是:
(如果打开程序在运行,再次打开程序也是打开当前在运行的程序.)
就不是再出显第二个窗口了.是如何做到的.请高手帮帮忙,谢谢了.
谢谢了.
VB程序如何防止打开多个?
答案:6 悬赏:10 手机版
解决时间 2021-02-10 11:31
- 提问者网友:暮烟疏雨之际
- 2021-02-10 03:19
最佳答案
- 五星知识达人网友:野味小生
- 2021-02-10 04:35
复制下面代码,编译成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
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
全部回答
- 1楼网友:不如潦草
- 2021-02-10 08:08
If App.PrevInstance = True Then End
- 2楼网友:零点过十分
- 2021-02-10 07:57
Private Sub Form_Load()
If App.PrevInstance Then
MsgBox "系统不允许程序运行多次,请关闭后再试。", vbInformation, "系统提示"
End If
End Sub
- 3楼网友:山君与见山
- 2021-02-10 07:26
If App.PrevInstance Then
MsgBox ("已经运行了一个程序!")
End
End If
- 4楼网友:北城痞子
- 2021-02-10 06:07
if App.PrevInstance then end
这种要求 App.PrevInstance 就满足不了了,这需要进程和窗口遍历了
c作起来简单,VB就有点麻烦了,c代码要不要
- 5楼网友:拜訪者
- 2021-02-10 05:41
private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
if ubound(diagnostics.process.getprocessesbyname(diagnostics.process.getcurrentprocess.processname)) > 0 then
messagebox.show("程序已经运行了")
end
end if
end sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯