473,480 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
2 6440
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
3153
by: Picho | last post by:
Hi all, Is there any .NET way (I am not rulling out API usage) to add button(s) to a form's title bar? I found some non-.NET solutions that did actually work in VB6 but not in the ..NET...
2
1943
by: Paul Moffitt | last post by:
We have a Javascript function that opens an electronic document in a subwindow. The Title bar of the subwindow shows the filepath of the document. Looking at the script below is there any way to...
33
3251
by: bissatch | last post by:
Hi, I fully understand the purpose of an alt attribute within a <img> tag but why would you use a title or summary attribute within, for example, a <p> tag. I have read books recommending that I...
3
4983
by: Ken | last post by:
Hi. I've defined a custom database property called Version. It is a text value. I wanted to know if there is a way to include that value on the title bar of a form window. Also, is there a...
4
13559
by: DerekM | last post by:
I have a database project that I created with several forms and reports. I would like to be able to declare a single title and be able to change the title on all the forms and reports by changing...
2
3165
by: skim1114 | last post by:
I have this code in my popup window, which works perfectly except that I need it to display a new title for every image. Here the popup window code: <HTML> <HEAD> <TITLE>DETAIL</TITLE>...
9
1973
by: Abhishek | last post by:
Hi, How do i find the window caption by using the hmodule of that window? Regards Abhishek
1
1806
by: giddy | last post by:
hi , I made a simple little caption control , i want to put it in a split container so it looks a litte like the VS windows. Problem is , i cant get it to the correct Inactive/Active colour! ...
6
5350
by: slg | last post by:
Is there a simpler way to increase the mdi form caption height? I am following this article but not sure where should i change? I belive its most probably in the WmNCCalcSize TIA ...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6732
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.