• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

Close a window if I know the Title or Part of the Title of the window..

please don't offer the solution that includes:
---------------
If InStr(temp, "Internet
Explorer") <> 0 Then SendKeys "%{F4}",
--------------
On my 500 mhz machine, it processes so fast that ALL my windows close with sendkeys.
Another offer please?
This app must detect the opening of a program's window (ie. Netscape) then close it in 2 seconds.
??
0
rbend
Asked:
rbend
  • 3
  • 2
1 Solution
 
Erick37Commented:
Try this:

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, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10


Private Sub Command1_Click()
    Dim lhWnd As Long
    Dim sApp As String
    'Exact Title of application
    sApp = "Calculator"
    'Find the window handle
    lhWnd = FindWindow(vbNullString, sApp)
    If lhWnd <> 0 Then
        'Post a close message to the application's window
        Call PostMessage(lhWnd, WM_CLOSE, 0&, 0&)
    End If
End Sub
0
 
rbendAuthor Commented:
looks like you're real close to what I need with this Erick37.
Just one adjustment please.
Close the window if I know just part of the titel. Ex. Close Microsoft Internet Explorer if the title is:
Your Visual Basic Question - Microsoft Internet Explorer
??

0
 
rbendAuthor Commented:
looks like you're real close to what I need with this Erick37.
Just one adjustment please.
Close the window if I know just part of the titel. Ex. Close Microsoft Internet Explorer if the title is:
Your Visual Basic Question - Microsoft Internet Explorer
??

0
 
Erick37Commented:
Part of title:
This code uses EnumWindows to retrieve the title of all windows in the system, compares it with the partial name, and stops when a match is made.  Code modified from a sample available at www.thescarms.com


'~~~~~~~FORM CODE~~~~~~~~~~~~~~
Option Explicit
Private Sub Command1_Click()
    Dim sApp As String
    'Find notepad with partial name
    sApp = "notepa"
    glHwnd = SearchWindows(sApp, Me.hwnd)
    'End application if found
    If glHwnd > 0 Then
        PostMessage glHwnd, WM_CLOSE, 0&, 0&
    End If
End Sub

'~~~~~~~MODULE CODE~~~~~~~~~~~~
Option Explicit

Public Const WM_CLOSE = &H10
Public Const MAX_PATH = 260

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    ByVal lParam As Long) As Long
Declare Function EnumWindows Lib "user32" _
    (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private sAppTitle As String
Public glHwnd As Long

Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long
    Dim lResult    As Long
    Dim sWndName   As String
   
    fEnumWindowsCallBack = 1
    sWndName = Space$(MAX_PATH)
   
    lResult = GetWindowText(hwnd, sWndName, MAX_PATH)
    sWndName = Left$(sWndName, lResult)
    'Search Title for our string
    If (InStr(1, sWndName, sAppTitle, vbTextCompare) > 0) Then
        Debug.Print sWndName
        glHwnd = hwnd
        fEnumWindowsCallBack = 0
    End If
End Function

Public Function SearchWindows(sApp As String, hwnd As Long) As Long
    sAppTitle = sApp
    glHwnd = 0
    Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd)
    SearchWindows = glHwnd
End Function
0
 
rbendAuthor Commented:
Exactly it..thanks.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now