[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

FindWindow function equivalent

Posted on 2006-05-25
5
Medium Priority
?
873 Views
Last Modified: 2012-08-14
From my VB.NET application I need to determine if a certain windows explorer window is open. I know I can do this using FindWindow API function:

FindWindow(vbNullString, "Window Title")

by passing to it Window title. But I want to be able to find window by using it's address (what's in the address bar). For example "C:\Test" instead of Window title. How can I accomplish that? is there an API function for that purpose?
0
Comment
Question by:YZlat
  • 4
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16761135
Here is a class that I use to enumerate Windows:

Imports System.Runtime.InteropServices
Imports System.Text

Public Class ApiWindow
  Public MainWindowTitle As String
  Public ClassName As String
  Public hWnd As Int32
End Class

Public Class WindowsEnumerator

  Private Delegate Function EnumCallBackDelegate(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer

  ' Top-level windows.
  Private Declare Function EnumWindows Lib "user32" _
   (ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer

  ' Child windows.
  Private Declare Function EnumChildWindows Lib "user32" _
   (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer

  ' Get the window class.
  Private Declare Function GetClassName _
   Lib "user32" Alias "GetClassNameA" _
   (ByVal hwnd As Integer, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer

  ' Test if the window is visible--only get visible ones.
  Private Declare Function IsWindowVisible Lib "user32" _
   (ByVal hwnd As Integer) As Integer

  ' Test if the window's parent--only get the one's without parents.
  Private Declare Function GetParent Lib "user32" _
   (ByVal hwnd As Integer) As Integer

  ' Get window text length signature.
  Private Declare Function SendMessage _
   Lib "user32" Alias "SendMessageA" _
   (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

  ' Get window text signature.
  Private Declare Function SendMessage _
   Lib "user32" Alias "SendMessageA" _
   (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As StringBuilder) As Int32

  Private _listChildren As New ArrayList
  Private _listTopLevel As New ArrayList

  Private _topLevelClass As String = String.Empty
  Private _childClass As String = String.Empty

  Public Overloads Function GetTopLevelWindows() As ApiWindow()

    EnumWindows(AddressOf EnumWindowProc, &H0)

    Return _listTopLevel.ToArray(GetType(ApiWindow))

  End Function

  Public Overloads Function GetTopLevelWindows(ByVal className As String) As ApiWindow()

    _topLevelClass = className

    Return Me.GetTopLevelWindows()

  End Function

  Public Overloads Function GetChildWindows(ByVal hwnd As Int32) As ApiWindow()

    ' Clear the window list.
    _listChildren = New ArrayList

    ' Start the enumeration process.
    EnumChildWindows(hwnd, AddressOf EnumChildWindowProc, &H0)

    ' Return the children list when the process is completed.
    Return _listChildren.ToArray(GetType(ApiWindow))

  End Function

  Public Overloads Function GetChildWindows(ByVal hwnd As Int32, ByVal childClass As String) As ApiWindow()

    ' Set the search
    _childClass = childClass

    Return Me.GetChildWindows(hwnd)

  End Function

  Private Function EnumWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32

    ' Eliminate windows that are not top-level.
    If GetParent(hwnd) = 0 AndAlso IsWindowVisible(hwnd) Then

      ' Get the window title / class name.
      Dim window As ApiWindow = GetWindowIdentification(hwnd)

      ' Match the class name if searching for a specific window class.
      If _topLevelClass.Length = 0 OrElse window.ClassName.ToLower() = _topLevelClass.ToLower() Then
        _listTopLevel.Add(window)
      End If

    End If

    ' To continue enumeration, return True (1), and to stop enumeration
    ' return False (0).
    ' When 1 is returned, enumeration continues until there are no
    ' more windows left.

    Return 1

  End Function

  Private Function EnumChildWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32

    Dim window As ApiWindow = GetWindowIdentification(hwnd)

    ' Attempt to match the child class, if one was specified, otherwise
    ' enumerate all the child windows.
    If _childClass.Length = 0 OrElse window.ClassName.ToLower() = _childClass.ToLower() Then
      _listChildren.Add(window)
    End If

    Return 1

  End Function

  Private Function GetWindowIdentification(ByVal hwnd As Integer) As ApiWindow

    Const W_GETTEXT As Int32 = &HD
    Const W_GETTEXTLENGTH As Int32= &HE

    Dim window As New ApiWindow

    Dim title As New StringBuilder

    ' Get the size of the string required to hold the window title.
    Dim size As Int32 = SendMessage(hwnd, W_GETTEXTLENGTH, 0, 0)

    ' If the return is 0, there is no title.
    If size > 0 Then
      title = New StringBuilder(size + 1)

      SendMessage(hwnd, W_GETTEXT, title.Capacity, title)
    End If

    ' Get the class name for the window.
    Dim classBuilder As New StringBuilder(64)
    GetClassName(hwnd, classBuilder, 64)

    ' Set the properties for the ApiWindow object.
    window.ClassName = classBuilder.ToString()
    window.MainWindowTitle = title.ToString()
    window.hWnd = hwnd

    Return window

  End Function
End Class
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16761150
Here is an example for finding Windows Explorer windows (ExploreWClass) and the combo box address bar (ComboBoxEx32):

    Dim enumerator As New WindowsEnumerator

    For Each parent As ApiWindow In enumerator.GetTopLevelWindows("ExploreWClass")

      Debug.WriteLine(parent.MainWindowTitle & ", " & parent.ClassName)

      For Each child As ApiWindow In enumerator.GetChildWindows(parent.hWnd, "ComboBoxEx32")

        Debug.WriteLine("    " & child.MainWindowTitle & ", " & child.ClassName)

      Next child

    Next parent

Bob
0
 
LVL 35

Author Comment

by:YZlat
ID: 16761519
Bob, but doesn't it use Window Title instead of Address?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 16761686
The ComboBoxEx32 window class is the Address bar.  If you look at GetWindowIdentification, you will notice that it sends WM_GETTEXT (I know, I know, it is a typo that I just noticed) to get the text for the Address bar combo box, and stores the value in ApiWindow.MainWindowTitle property.

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16761705
I did this, because my Windows Explorer instances do not display full addresses, just the short name (Tools | Folder Options | View | Display the full path in the title bar).

The 'Display the full path in the address bar is checked'.

Bob
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

873 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