LOLZguy712
LOLZguy712

Reputation: 57

Determining if the active window title contains a certain string

What would be the code to check if an active window title contains a certain string? I've tried things involving Process.MainWindowTitle However I couldn't get it to function properly. Any other ideas?

Upvotes: 1

Views: 648

Answers (1)

Arman
Arman

Reputation: 1442

Here is a code snippet from vbBlogspot

The WinAPI call getting the text length of a window:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
      Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function

And here is the main function being described there.

Private Function getActiveWindowTitle() As String
'Get the handle currenly active Window
Dim Hwnd As IntPtr = GetForegroundWindow()

Dim WndTitleLength As Integer
Dim WndTitle As String

'Get the Window title length
WndTitleLength = GetWindowTextLength(Hwnd)
WndTitle = Space(WndTitleLength + 1)

'Get the Window title
GetWindowText(Hwnd, WndTitle, WndTitleLength + 1)

Return Microsoft.VisualBasic.Left(WndTitle, WndTitleLength)

End Function

Though this is intended to get the title of the current window, you can make some adjustments to, say, determine only the windowtextLength. If it equates to zero, it doesn't contain a title.

Upvotes: 1

Related Questions