COMO SABER SE UM PROGRAMA ESTA ABERTO
Pessoal, acho que já tem algo postado, mas não consegui encontrar!
Como saber se um programa está aberto?
Por exemplo: se o meu programa chamar um outro executável, primeiro verifica se já está aberto. Se tiver, MOSTRA na tela, se não, EXECUTA.
Como saber se um programa está aberto?
Por exemplo: se o meu programa chamar um outro executável, primeiro verifica se já está aberto. Se tiver, MOSTRA na tela, se não, EXECUTA.
para verificar se ja esta aberto:
if app.previnstance then
'ja aberto
endif
agora existem algumas maneiras para fazer este se "MOSTRA"
eu prefiro trabalhar por pooling...
coloca um timer no form principal de seu programa que verifica a existencia de algum arquivo ou um registro nos registros do windows - se nao houver ele simplesmente mostra a tela e cria este arquivo ou este registro...
entao quando vc entrar com o software vc verifica no previntance...
if app.previntance then
kill ARQUIVO
'ou remove registro
unload me
endif
assim que o timer_Time for disparado ele vai ver q nao existe mais o arquivo e vai abrir a tela
if app.previnstance then
'ja aberto
endif
agora existem algumas maneiras para fazer este se "MOSTRA"
eu prefiro trabalhar por pooling...
coloca um timer no form principal de seu programa que verifica a existencia de algum arquivo ou um registro nos registros do windows - se nao houver ele simplesmente mostra a tela e cria este arquivo ou este registro...
entao quando vc entrar com o software vc verifica no previntance...
if app.previntance then
kill ARQUIVO
'ou remove registro
unload me
endif
assim que o timer_Time for disparado ele vai ver q nao existe mais o arquivo e vai abrir a tela
Em um form:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) 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 ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
'Add this code to a form
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'Set the form's graphics mode to persistent
Me.AutoRedraw = True
'call the Enumwindows-function
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub
Private Sub Command1_click()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Ask for a Window title
Ret = List1.Text 'InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
'Search the window
WinWnd = FindWindow(vbNullString, Ret)
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Create a buffer
lpClassName = Space(256)
'retrieve the class name
RetVal = GetClassName(WinWnd, lpClassName, 256)
End Sub
em um modulo
Option Explicit
'Add this code to a module
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sSave As String, Ret As Long
Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1
Form1.List1.AddItem sSave
Form1.List1.ItemData(Form1.List1.NewIndex) = hwnd
'continue enumeration
EnumWindowsProc = True
End Function
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) 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 ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
'Add this code to a form
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'Set the form's graphics mode to persistent
Me.AutoRedraw = True
'call the Enumwindows-function
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub
Private Sub Command1_click()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Ask for a Window title
Ret = List1.Text 'InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
'Search the window
WinWnd = FindWindow(vbNullString, Ret)
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Create a buffer
lpClassName = Space(256)
'retrieve the class name
RetVal = GetClassName(WinWnd, lpClassName, 256)
End Sub
em um modulo
Option Explicit
'Add this code to a module
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sSave As String, Ret As Long
Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1
Form1.List1.AddItem sSave
Form1.List1.ItemData(Form1.List1.NewIndex) = hwnd
'continue enumeration
EnumWindowsProc = True
End Function
Tópico encerrado , respostas não são mais permitidas