Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > VB6 > Поиск окна по заголовку (и его закрытие)


Автор: cardinal 31.5.2005, 16:06
Код

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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag 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

Private Const WM_CLOSE = &H10

Private Function GetCaption(ByVal lhWnd As Long) As String
Dim sA As String, lLen As Long
lLen& = GetWindowTextLength(lhWnd&)
sA$ = String(lLen&, 0&)
Call GetWindowText(lhWnd&, sA$, lLen& + 1)
GetCaption$ = sA$
End Function

Private Function FindAnyWindow(frm As Form, ByVal WinTitle As String, Optional ByVal CaseSensitive As Boolean = False) As Long
Dim lhWnd As Long, sA As String
lhWnd& = frm.hwnd
    Do Until lhWnd& = 0
        DoEvents
        sA$ = GetCaption(lhWnd&)
        If InStr(IIf(CaseSensitive = False, LCase$(sA$), sA$), IIf(CaseSensitive = False, LCase$(WinTitle$), _
WinTitle$)) Then FindAnyWindow& = lhWnd&: Exit Do Else FindAnyWindow& = 0
        lhWnd& = GetNextWindow(lhWnd&, 2)
    Loop
End Function

Private Function CloseWindowByCaption(strCaption As String)
Dim wcHwnd&
wcHwnd = FindAnyWindow(Form1.hwnd, strCaption)
Call SendMessage(wcHwnd, WM_CLOSE, 0&, ByVal 0&)
End Function

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)