Get title/caption of window with focus? (Faulty code inside)

T

Trammel

Hi. I have been trying to make a simple application that will sit in the
background and alert me of any changes in window focus (IE: Pop-up windows)
on my system.

I have used DirectTextToSpeech from Microsoft and tried the following
code...
The problem I am having is that it detects its own caption fine... but no
captions of other windows :¬/

Could someone please have a look at the following code and lemme know what
Im doing wrong?

===============================================================

Option Strict Off
Option Explicit On

Friend Class Form1Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code "
*** snip ***
#End Region
#Region "Upgrade Support "
*** snip ***
#End Region

Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles MyBase.Load
DirectSS1.Pitch = CInt("100")
DirectSS1.Speed = CInt("150")
LastTitle = ""
DirectSS1.Sayit = "Hello!"
End Sub

Private Sub tmrScan_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrScan.Tick
Dim ThisTitle As String
ThisTitle = RetCurTitle()
If ThisTitle <> LastTitle Then
LastTitle = ThisTitle
DirectSS1.Sayit = "Window focus has changed."
DirectSS1.Sayit = "New window title is " + ThisTitle
Label1.Text = "New window title is " + ThisTitle
End If
End Sub
End Class

Module WindowScanner
Public Declare Function GetActiveWindow Lib "user32" () As System.IntPtr
Public Declare Auto Function GetWindowText Lib "user32" (ByVal hWnd As
System.IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As
Integer) As Integer
Public LastTitle As String
Public Function RetCurTitle() As String
' Create a buffer of 256 characters
Dim Caption As New System.Text.StringBuilder(256)
Dim hWnd As IntPtr = GetActiveWindow()
GetWindowText(hWnd, Caption, Caption.Capacity)
Return Caption.ToString()
End Function

End Module
 
A

Atul

I think the GetActiveWindow function only returns a valid active window if
the window was created by your own thread/program.

- Atul
Sky Software http://www.ssware.com/
Drop-In Windows Explorer-Like Shell Browsing UI for your apps.
 
T

Trammel

Atul said:
I think the GetActiveWindow function only returns a valid active window if
the window was created by your own thread/program.

Thanks for the pointer there Atul.

I changed the 2 references of "GetActiveWindow" to "GetForegroundWindow" and
it all works fine now... it speaks every new window name when it pops-up :¬)
 

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

Top