Private Declare Function EnumWindows& Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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 IsWindowVisible& Lib "user32" (ByVal hwnd As Long) Private Declare Function GetParent& Lib "user32" (ByVal hwnd As Long)
Dim sPattern As String, hFind As Long
Private Function FindActivateTitle(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim k As Long, sName As String If IsWindowVisible(hwnd) And GetParent(hwnd) = 0 Then sName = Space$(128) k = GetWindowText(hwnd, sName, 128) If k > 0 Then sName = Left$(sName, k) If lParam = 0 Then sName = UCase(sName) If sName Like sPattern Then hFind = hwnd FindActivateTitle = 0 Exit Function End If End If End If FindActivateTitle = 1 End Function
Function ActivateAppHwnd(sTitle As String, Optional sMatchCase As Boolean = True) As Long sPattern = sTitle: hFind = 0 If Not sMatchCase Then sPattern = UCase(sPattern) EnumWindows AddressOf FindActivateTitle, sMatchCase ActivateAppHwnd = hFind End Function '----------------------------------------- Module1 -----------------------------------------
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 Const WM_CLOSE = &H10
Private Sub Timer1_Timer() 'lukker Microsoft Internet Explore PostMessage ActivateAppHwnd("*Microsoft Internet Explore*", False), WM_CLOSE, 0&, 0& End Sub '----------------------------------------- Form1 -----------------------------------------
Selvom det er det samme program, bliver det tildelt en ny handle hver gang det startes. Det er med det handle du kan lukke programmet. Problemet er bare at nogle programmer har et window handle (hWnd) mens andre kører med en Process ID (PID). Derfor er det vigtigt at vide hvilken type program det er du vil lukke. Er det et windows program eller er det en service?
Private Sub Timer1_Timer() ActivateAppHwnd("*Microsoft Internet Explore*", True),WM_CLOSE, 0&, 0& End Sub
er sat til True. tak for hjælpen sjh.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.