VB icon

Find Any Window

Email
Submitted on: 1/2/2015 11:57:00 AM
By: DoWnLoHo (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: VB 5.0, VB 6.0
Views: 761
 
     Sometimes you need to find a window using the API Call findwindow , but what if this windows caption changes you can't find that same window all the time. With this function you can find any window just by knowing a few letters in the caption. This will return the windows' hWnd , also includes a function that will grab the windows caption. This is something that will be useful to alot of programmers. Updated! 2.23.01
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Find Any Window
'**************************************
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Find Any Window
' Description:Sometimes you need to find a window using the API Call findwindow , but what if this windows caption changes 
you can't find that same window all the time. With this function you can find any window just by knowing a few letters
in the caption. This will return the windows' hWnd , also includes a function that will grab the windows caption. 
This is something that will be useful to alot of programmers. Updated! 2.23.01
' By: DoWnLoHo (from psc cd)
'
' Assumes:call it like so
call msgbox(FindAnyWindow&(me,"text of window"))
or to get the caption do this
call msgbox(getcaption$(FindAnyWindow&(me,"text of window")))
'
' Side Effects:none(that I know of)
'**************************************

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


Other 13 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.