[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 1999-11-27
14
Medium Priority
?
220 Views
Last Modified: 2011-09-20
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
Comment
Question by:rbend
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 2238071
1.
 first - the SendKeys will close only that window if it's second parameter will be TRUE.
2.
since you already have the window HWND -you can use the API DestroyWindow(hwnd)
to close it .
 
Private Declare Function DestroyWindow Lib "user32" Alias "DestroyWindow" (ByVal hwnd As Long) As Long

0
 

Author Comment

by:rbend
ID: 2238091
I don't want to have to have the HWND of the window, I just have the title.
This quesition was asked twice by mistake.
pLease answer the other one.
thanks..
0
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 2238104
what an ignorance !!!

no way to read the window title or part of it without the window HWND.
in the other similar Q - you got a solution that mentions HWND or Window HANDLE (same thing) at least 5 times...

if you don't want to have the HWND of the window (as you say) - you don't want to close it as well....and what's worse - you don't want to learn a thing
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 1

Expert Comment

by:omran
ID: 2238515
if you increase the points i'll send for you a code to control any opened window using API ,, it's very useful ..
0
 
LVL 14

Accepted Solution

by:
mcrider earned 150 total points
ID: 2239052
This example shows how to find a handle of an application just knowing the title or part of the title...  It's a 16-bit example, but I've included the 32bit function here.

VB3 How to Get Window Handle Without Specifying Exact Title
http://support.microsoft.com/support/kb/articles/Q113/4/75.asp 
 

Once you have the handle, you can use DestroyWindow to close it...


Cheers!


32-BIT CODE:

------------------------------------------------------------
ADD THIS TO A MODULE TO DEFINE FindWindowLike:
------------------------------------------------------------
Private Const GWL_ID = (-12)
Global ProgHandle As Long

Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5

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 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

Declare Function GetDesktopWindow Lib "user32" () As Long

Declare Function GetExitCodeProcess Lib "kernel32" _
    (ByVal hProcess As Long, lpExitCode As Long) As Long

Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long

Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long

Declare Function SysSetFocus Lib "user32" Alias "SetFocus" _
    (ByVal hwnd As Long) As Long

Function FindWindowLike(hWndArray() As Variant, ByVal hWndStart As Long, WindowText As String, Classname As String, ID) As Long
    Dim hwnd As Long
    Dim r As Long
    ' Hold the level of recursion:
    Static level As Long
    ' Hold the number of matching windows:
    Static iFound As Long
    Dim sWindowText As String
    Dim sClassname As String
    Dim sID
    ' Initialize if necessary:
    If level = 0 Then
        iFound = 0
        ReDim hWndArray(0 To 0)
        If hWndStart = 0 Then hWndStart = GetDesktopWindow()
    End If
    ' Increase recursion counter:
    level = level + 1
    ' Get first child window:
    hwnd = GetWindow(hWndStart, GW_CHILD)
    Do Until hwnd = 0
        DoEvents ' Not necessary
        ' Search children by recursion:
        r = FindWindowLike(hWndArray(), hwnd, WindowText, Classname, ID)
        ' Get the window text and class name:
        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 window is a child get the ID:
        If GetParent(hwnd) <> 0 Then
            r = GetWindowLW(hwnd, GWL_ID)
            sID = CLng("&H" & Hex(r))
        Else
            sID = Null
        End If
        ' Check that window matches the search parameters:
        If sWindowText Like WindowText And sClassname Like Classname Then
            If IsNull(ID) Then
                ' If find a match, increment counter and
                '  add handle to array:
                iFound = iFound + 1
                ReDim Preserve hWndArray(0 To iFound)
                hWndArray(iFound) = hwnd
            ElseIf Not IsNull(sID) Then
                If CLng(sID) = CLng(ID) Then
                    ' If find a match increment counter and
                    '  add handle to array:
                    iFound = iFound + 1
                    ReDim Preserve hWndArray(0 To iFound)
                    hWndArray(iFound) = hwnd
                End If
            End If
        End If
        ' Get next child window:
        hwnd = GetWindow(hwnd, GW_HWNDNEXT)
    Loop
    ' Decrement recursion counter:
    level = level - 1
    ' Return the number of windows found:
    FindWindowLike = iFound
End Function
0
 

Author Comment

by:rbend
ID: 2240222
Thank you all..
"the IGNORANCE"
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2240304
rbend,

Thanks for the points! Glad I could help you.

AnswerTheMan said:

     "what an ignorance !!! no way to read the window title or part of it without the window HWND..."

I think you owe rbend an apology...


Cheers!
0
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 2241323
mcrider : after searching your fine answer for the pharse 'HWND' - i stopped counting after the 5th 'HWND'.

the man wanted to do that without using the 'HWND' term, and i said no one can offer a code that do that without the 'HWND' .

what have i to appologize ?
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2241671
AnswerTheMan,

rbend's english is not that good... but he/she clearly stated that "I just have the title.", meaning that rbend did not know what the hwnd was for the window that is needed to be destroyed.

Instead of carefully reading the question, you just "went off".  That is what you should apologize for...

rbend,

Again, glad I could help!


Cheers!

0
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 2244455
mcrider : Wrong again.....

this Q is a follow up to a privious RBEND's Q in which i have theached him how to use "GetForegroudWindow" and "GetWindowText" to retrive that caption that he have now... in this Q...

there - he asked : if that caption includes some string - how to terminate it ... and i suggested to send ALT+F4 (it's the foreground window).

that's the background to this Q, which you are not aware of.

so,he has the HWND if he wants it or not....

now, you accusing me : <<"Instead of carefully reading the question, you just "went off".  That is what you should apologize for...">>

read this very comment carfully and you come to conclusion that maybe you owe me some apologize....

never mind. BTW - i'm not an ENGLISH speaker myself.....

bye

0
 
LVL 14

Expert Comment

by:mcrider
ID: 2244877
rbend never mentions a previous question in this question and has accepted my response. Please read the question again.

Cheers!
0
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 2245024
sure he does not mention....
you can check Q.10236498 .
it's good for you. it's free, still open, and after my non apologizing you may collect the points i still have not got there inspite of RBEND saying there to me "Your answer is fine".

i'm glad for you RBEND accepted you responce. anyway...i'm quite sure he won't get far in this tread. his human HWND just not meet the requirments.
0
 

Author Comment

by:rbend
ID: 2249826
You all have been helpful.
I thank you.
My HWND thanks you.
Merry Christmas
0
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 2251120
be well
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question