473,770 Members | 5,880 Online
Bytes | Software Development & Data Engineering Community
+ 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 DirectTextToSpe ech 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(ByVa l eventSender As System.Object, ByVal
eventArgs As System.EventArg s) Handles MyBase.Load
DirectSS1.Pitch = CInt("100")
DirectSS1.Speed = CInt("150")
LastTitle = ""
DirectSS1.Sayit = "Hello!"
End Sub

Private Sub tmrScan_Tick(By Val sender As System.Object, ByVal e As
System.EventArg s) 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.Str ingBuilder, 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.Str ingBuilder(256)
Dim hWnd As IntPtr = GetActiveWindow ()
GetWindowText(h Wnd, Caption, Caption.Capacit y)
Return Caption.ToStrin g()
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 6462
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.co m> wrote in message
news:%b******** **********@fe2. news.blueyonder .co.uk...
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 DirectTextToSpe ech 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(ByVa l eventSender As System.Object, ByVal
eventArgs As System.EventArg s) Handles MyBase.Load
DirectSS1.Pitch = CInt("100")
DirectSS1.Speed = CInt("150")
LastTitle = ""
DirectSS1.Sayit = "Hello!"
End Sub

Private Sub tmrScan_Tick(By Val sender As System.Object, ByVal e As
System.EventArg s) 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.Str ingBuilder, 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.Str ingBuilder(256)
Dim hWnd As IntPtr = GetActiveWindow ()
GetWindowText(h Wnd, Caption, Caption.Capacit y)
Return Caption.ToStrin g()
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" <atulatsswaredo tcom> wrote in message
news:ea******** *****@TK2MSFTNG P15.phx.gbl...
"Trammel" <Me@Server.co m> wrote in message
news:%b******** **********@fe2. news.blueyonder .co.uk...
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 DirectTextToSpe ech 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(ByVa l eventSender As System.Object, ByVal
eventArgs As System.EventArg s) Handles MyBase.Load
DirectSS1.Pitch = CInt("100")
DirectSS1.Speed = CInt("150")
LastTitle = ""
DirectSS1.Sayit = "Hello!"
End Sub

Private Sub tmrScan_Tick(By Val sender As System.Object, ByVal e As
System.EventArg s) 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.Str ingBuilder, 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.Str ingBuilder(256)
Dim hWnd As IntPtr = GetActiveWindow ()
GetWindowText(h Wnd, Caption, Caption.Capacit y)
Return Caption.ToStrin g()
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 "GetActiveWindo w" to "GetForegroundW indow" 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
3198
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 forms... I tried painting, but the paintaing area provided by the form is only the client area - no visible way to paint on the title bar.
2
1970
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 add script to the function to either not show the path in the title bar or replace the path with other text? function launchEDoc() { var subWindow window.document.title = "InSight" if (document.forms.EDoc.length){
33
3305
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 use them but why? Does this enhance accessibility? Please provide reasons why one would want to use these? Thanks Burnsy
3
4994
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 way to include it on the title bar of the database window? I'm using Access 97. Thanks for any info!
4
13589
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 a single variable. I think this is possible, however I don't know Visual Basic very well. I created a module named DBcommon with the following statements in it: Option Compare Database Global Const DBname As String = "Title String"
2
3191
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> <script language='javascript'> var temp=self.location.href.split("?"); var picUrl = (temp.length>1)?temp:"";
9
1993
by: Abhishek | last post by:
Hi, How do i find the window caption by using the hmodule of that window? Regards Abhishek
1
1821
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! Apparently , the splitcontainerPanel ContainsFocus even after it has lost focus , atleast thats what you'd think after you run the code!
6
5375
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 http://geekswithblogs.net/kobush/articles/CustomBorderForms.aspx
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.