By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,698 Members | 1,328 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,698 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
--
Trammel is a member of DWC (http://dwc.no-ip.org)
(Please reply to group only)
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
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.
"Trammel" <Me@Server.com> wrote in message
news:%b******************@fe2.news.blueyonder.co.u k...
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
--
Trammel is a member of DWC (http://dwc.no-ip.org)
(Please reply to group only)

Nov 21 '05 #2

P: n/a
"Atul" <atulatsswaredotcom> wrote in message
news:ea*************@TK2MSFTNGP15.phx.gbl...
"Trammel" <Me@Server.com> wrote in message
news:%b******************@fe2.news.blueyonder.co.u k...
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


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

--
Trammel is a member of DWC (http://dwc.no-ip.org)
(Please reply to group only)
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.