Jump to content


- - - - -

Fechar programa










Post sem respostas

#1 Edilson Souza

    GoKu

  • (*)Insigne
  • 4478 posts
  • Sexo:Masculino
  • Estado:Pernambuco

Adicionado 26 March 2003 - 12:25 AM

Aqui esta um exemplo de como programar no VB para que ao pressionar um botão ele feche um determinando programa aberto. Chamamos de destroi programas.

Crie um módulo:

Quote

Option Explicit

Private Declare Function SetFocusAPI Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowLW Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) 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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Const GWL_ID = (-12)
Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5

#Const WinVar = True
#If WinVar Then
    Public Function FindWindowLike(hWndArray() As Variant, ByVal hWndStart As Variant, WindowText As String, Classname As String, ID) As Integer
        Dim hwnd
        Dim r
        Static level
        Static iFound
#ElseIf Win32 Then
    Public Function FindWindowLike(hWndArray() As Long, ByVal hWndStart As Long, WindowText As String, Classname As String, ID) As Long
        Dim hwnd As Long
        Dim r As Long
        Static level As Long
        Static iFound As Long
#ElseIf Win16 Then
    Public Function FindWindowLike(hWndArray() As Integer, ByVal hWndStart As Integer, WindowText As String, Classname As String, ID) As Integer
        Dim hwnd As Integer
        Dim r As Integer
        Static level As Integer
        Static iFound As Integer
#End If

Dim sWindowText As String
Dim sClassname As String
Dim sID

If level = 0 Then
    iFound = 0
    ReDim hWndArray(0 To 0)
    If hWndStart = 0 Then hWndStart = GetDesktopWindow()
End If

level = level + 1
hwnd = GetWindow(hWndStart, GW_CHILD)

Do Until hwnd = 0
    DoEvents
    r = FindWindowLike(hWndArray(), hwnd, WindowText, Classname, ID)
    sWindowText = Space(255)

    r = GetWindowText(hwnd, sWindowText, 255)
    sWindowText = Left(sWindowText, r)
    sClassname = Space(255)
    r = GetClassName(hwnd, sClassname, 255)
    sClassname = Left(sClassname, r)
        If GetParent(hwnd) <> 0 Then
            r = GetWindowLW(hwnd, GWL_ID)
            sID = CLng("&H" & Hex®)
        Else
            sID = Null
        End If

    If sWindowText Like WindowText And sClassname Like Classname Then
        If IsNull(ID) Then
            iFound = iFound + 1
            ReDim Preserve hWndArray(0 To iFound)
            hWndArray(iFound) = hwnd
        ElseIf Not IsNull(sID) Then
            If CLng(sID) = CLng(ID) Then
                iFound = iFound + 1
                ReDim Preserve hWndArray(0 To iFound)
                hWndArray(iFound) = hwnd
            End If
        End If
    End If

    hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop
    level = level - 1
    FindWindowLike = iFound
End Function

Public Function DestroyAllWindows(capt As String)
Dim buff As String * 100
Dim ret As Long
Dim i As Integer
Static hWnds() As Variant
Dim X As Long
Dim r
Dim capt2
capt2 = capt
r = FindWindowLike(hWnds(), 0, "*" & capt2 & "*", "*", Null)
If r > 0 Then
    For i = 1 To UBound(hWnds())
        X = SendMessage(CLng(hWnds(i)), &H112, &HF060&, 0&)
    Next
End If
End Function


No comando do botão:

Quote

MsgBox "Abra por Exmplo o Outlook"
DestroyAllWindows "Outlook"






1 usuário(s) esta(ão) lendo este tópico

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)