Solved

Finding a windows hWnd 1000 pts

Posted on 2000-05-03
11
286 Views
Last Modified: 2010-05-02
I would like to be able to find the hWnd of a window but this window is special because it is like this:

Title

           
          123 - Prog Name - 123


and the 123 changes every time the prog is ran

is there any way to get the windows hWnd?

thanks
0
Comment
Question by:marka64
11 Comments
 
LVL 9

Expert Comment

by:Ruchi
ID: 2776180
0
 
LVL 9

Accepted Solution

by:
Ruchi earned 1000 total points
ID: 2776187
From http://www.freevbcode.com/ShowCode.Asp?ID=336

"Sometimes you need to find a window using the API Call findwindow. If this window's 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 included is a function that will grab the window's caption. This is something that will be useful to a lot of programmers."

'Declarations:


Option Explicit

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


'call it like so
'call msgbox(dlhfindwin&(me,"text of window",false))
'or to Get the caption Do this
'call msgbox(getcaption$(dlhfindwin&(me,"text of window",false)))

Public Function GetCaption(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 DLHFindWin(frm As Form, WinTitle As String, _
CaseSensitive As Boolean) As Long

Dim lhWnd As Long, sA As String

   lhWnd& = frm.hwnd


Do

   DoEvents
      If lhWnd& = 0 Then Exit Do
         If CaseSensitive = False Then
             sA$ = LCase$(GetCaption(lhWnd&))
             WinTitle$ = LCase$(WinTitle$)
         Else
             sA$ = GetCaption(lhWnd&)
         End If

       If InStr(sA$, WinTitle$) Then
          DLHFindWin& = lhWnd&
          Exit Do
       Else
         DLHFindWin& = 0
       End If

       lhWnd& = GetNextWindow(lhWnd&, 2)

Loop

End Function
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2776200
0
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.

 
LVL 28

Expert Comment

by:AzraSound
ID: 2776209
AAAHH!!  my comp crashed in mid post...oh well...heres my piece anyways, though I'm sure Ruchi's code works just fine (as usual)   =)


****MODULE CODE****


Option Explicit

#If Win16 Then
   DefInt A-Z
   ' Required Win16 API declarations
   Private Declare Function FindWindow Lib "User" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Integer
   Private Declare Function GetWindow Lib "User" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
   Private Declare Function GetWindowText Lib "User" (ByVal hWnd As Integer, ByVal lpString As String, ByVal aint As Integer) As Integer
   Private Declare Function GetParent Lib "User" (ByVal hWnd As Integer) As Integer
#ElseIf Win32 Then
   DefLng A-Z
   ' Required Win32 API declarations
   Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
   Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd 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
   Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
#End If

' Constant used by GetWindowWord to find next window
Private Const GW_HWNDNEXT = 2


Public Function FindWindowPartial(TitleStart$) As Long
   Dim hWndTmp 'As SysInt
   Dim nRet 'As SysInt
   Dim TitleTmp As String
   '
   ' Find first window and loop through all subsequent
   ' windows in master window list.
   '
   hWndTmp = FindWindow(vbNullString, vbNullString)
   Do Until hWndTmp = 0
      '
      ' Make sure this window has no parent.
      '
      If GetParent(hWndTmp) = 0 Then
         '
         ' Retrieve caption text from current window.
         '
         TitleTmp = Space(256)
         nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))
         If nRet Then
            '
            ' Clean up return string, preparing for
            ' case-insensitive comparison.
            '
            TitleTmp = UCase(Left(TitleTmp, nRet))
            '
            ' Use appropriate method to determine if
            ' current window's caption either starts
            ' with or contains passed string.
            '
            If InStr(TitleTmp, UCase(TitleStart)) Then
                FindWindowPartial = hWndTmp
                Exit Do
            End If
         End If
      End If
      '
      ' Get next window in master window list and continue.
      '
      hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
   Loop
End Function



Call it from a form like this:


Private Sub Command1_Click()
    MsgBox FindWindowPartial("any part of the window caption")
End Sub

0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2776210
Changes the handle of every caption. Here's the code:

'Declarations

Private Declare Function SetWindowText Lib "user32" _
Alias "SetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String) As Long

'Usage: Call WindowCaptionChangeAll("- Microsoft Internet Explorer")
'Code:


Public Sub WindowCaptionChangeAll(NewText As String)

For nI = 1 To 10000
Call SetWindowText(nI, NewText)
Next

End Sub

Hope that helps.
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2776219
Use the FindWindowEx API

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Public Function GetHwnds(ByVal Class As String, ByVal Caption As String) As Variant
    Dim lHwnd() As Long
    Dim lCount As Long
    Dim lHandle As Long
    ReDim lHwnd(0)
    Do
        lHandle = FindWindowEx(0, lHandle, Class, Caption)
        If lHandle Then
            ReDim Preserve lHwnd(lCount)
            lHwnd(lCount) = lHandle
            lCount = lCount + 1
        End If
    Loop While lHandle
    GetHwnds = lHwnd
End Function

Private Sub Command1_Click()
    Dim lHwnd As Variant
    Dim iIndex As Long
    lHwnd = GetHwnds("NotePad", vbNullString)
    For iIndex = 0 To UBound(lHwnd)
        If lHwnd(iIndex) Then
            'Do Whatever with the Window Handle Here..
            Debug.Print lHwnd(iIndex)
        End If
    Next
End Sub



The 1000 points is a lot!!! :)
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2776512
Actually, there is a Microsoft KB article that explains *exactly* how to do this:

HOWTO: Get a Window Handle Without Specifying an Exact Title
http://support.microsoft.com/support/kb/articles/Q147/6/59.asp 


Cheers!®©
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2777248
"Provides a custom FindWindowPartial function that returns the handle of the first window that matches your search criteria. You can search for window captions that start with, or contain, any substring you wish"

http://www.mvps.org/vb/code/FindPart.zip
0
 

Author Comment

by:marka64
ID: 2777340
Thanks everyone!!!
0
 
LVL 4

Expert Comment

by:amit_panjwani
ID: 2783867
Code could have been more refined and  optmized.
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2784358
Thank you for the points! I 'm glad that I could help you, Mark.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Updates not working for MS Windows 7 12 181
Add a task in Outlook from access 11 42
SQL VB connection works in one PC and doesn't in another 15 67
How to Add / Edit Windows Menu 4 67
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 …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 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