FindWindowLike in VB.NET?

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

Is there anything in VB.NET that we can use that functions like the old
FindWindowLike in VB6? I am using .NET to close an application that I am
opening; however, I need to be able to check to see if it puts up one of the
'Are you sure you wish to close?' popup messages so I can also close that. I
can control the app fine, but not sure how to 'find' this popup. In VB6 I
just used the FindWindowLike (such as
FindWindowLike(hWnds(),Popup,"OK","Button",Null).

Any way to do this in .NET? Thanks.

Tom
 
Tom said:
Is there anything in VB.NET that we can use that functions like the old
FindWindowLike in VB6?

No, you will have to continue using 'FindWindow*'. Notice that datatypes
have changed in VB.NET, so you will have to update your declares and code
using them.
 
Hi Tom,

I found the following function whilst searching on the net, no idea if
it works but it is designed as a substitute for what you are after,

-------------------------------------------------------------------

Public Declare Function GetDesktopWindow Lib "user32" () As Long

Public Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long

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 GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_OWNER = 4
Public Const GW_CHILD = 5

Form code:

Option Explicit

Private Sub cmdStart_Click()

'Used to return window handles.
Dim TitleToFind As String, ClassToFind As String

List1.Clear

'Set the FindWindowLike text values from
'the strings entered into the textboxes
TitleToFind = (txtTitle) & "*"
ClassToFind = (txtClass)

Call FindWindowLike(0, TitleToFind, ClassToFind)
lbCount = CStr(List1.ListCount) & " matches found"

End Sub


Private Function FindWindowLike(ByVal hWndStart As Long, _
WindowText As String, _
Classname As String) As Long

Dim hwnd As Long
Dim sWindowText As String
Dim sClassname As String
Dim r As Long

'Hold the level of recursion and
'hold the number of matching windows
Static level As Integer

'Initialize if necessary. This is only executed
'when level = 0 and hWndStart = 0, normally
'only on the first call to the routine.
If level = 0 Then
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

'Search children by recursion
Call FindWindowLike(hwnd, WindowText, Classname)

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

'Check if window found matches the search parameters
If (sWindowText Like WindowText) And _
(sClassname Like Classname) Then

List1.AddItem hwnd & vbTab & _
sClassname & vbTab & _
sWindowText
FindWindowLike = hwnd

'uncommenting the next line causes the routine to
'only return the first matching window.
'Exit Do

End If

'Get next child window
hwnd = GetWindow(hwnd, GW_HWNDNEXT)

Loop

'Reduce the recursion counter
level = level - 1

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top