473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

GetActiveWindow and GetWindowText

I am trying to write an application that sits in the system tray and just
monitors for a certain Window to be opened. Once this window is opened all
keystrokes will be captured. I have the key capture working correctly but
cant seem to get the Window thing working.

I think I need to use GetActiveWindow and GetWindowText but not sure. These
are the functions that I am using:

Public Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow"
() As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA"
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
' Create string filled with null characters.
Dim strCaption As String
Dim lngLen As Long
Dim currentForm As Form = Form.ActiveForm

Try
' Return length of string.
lngLen = 256
' Call GetActiveWindow to return handle to active window,
' and pass handle to GetWindowText, along with string and its length.
If (GetWindowText(GetForegroundWindow, strCaption, lngLen) > 0 Then
strCaption = GetWindowText(GetForegroundWindow, strCaption, lngLen

' Return value that Windows has written to string.
Return strCaption
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

I am using a timer control to fire the procedure that will look for the
current window's caption.
If someone could tell me how to do this in VB.NET I would really appreciate
it.
Nov 20 '05 #1
2 47056
On 2004-01-23, Larry Dodd <la********@sbcglobal.net> wrote:
I am trying to write an application that sits in the system tray and just
monitors for a certain Window to be opened. Once this window is opened all
keystrokes will be captured. I have the key capture working correctly but
cant seem to get the Window thing working.

I think I need to use GetActiveWindow and GetWindowText but not sure. These
are the functions that I am using:

Public Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow"
() As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA"
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
' Create string filled with null characters.
Dim strCaption As String
Dim lngLen As Long
Dim currentForm As Form = Form.ActiveForm

Try
' Return length of string.
lngLen = 256
' Call GetActiveWindow to return handle to active window,
' and pass handle to GetWindowText, along with string and its length.
If (GetWindowText(GetForegroundWindow, strCaption, lngLen) > 0 Then
strCaption = GetWindowText(GetForegroundWindow, strCaption, lngLen

' Return value that Windows has written to string.
Return strCaption
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

I am using a timer control to fire the procedure that will look for the
current window's caption.
If someone could tell me how to do this in VB.NET I would really appreciate
it.


1. Change your longs to integers - long is 64-bit in .NET
2. Loose the alias, let the runtime determine the correct function
3. Declare handle types as IntPtr
4. Pass System.Text.StringBuilder to API's that are going to fill the
sting buffer.

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
' 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()
Obviously, you'll want to check the return values of the api calls and
take appropriate action :)

--
Tom Shelton [MVP]
Nov 20 '05 #2
That works great although I decided to use the GetForegroundWindow instead
of GetActiveWindow. Now I would like to know how since I have the current
windows handle how can I find out what the application that belongs to that
window is. For instance I want to know if the active window is Outlook or
Microsoft Word, etc.

Again, thanks for all the help on this.

"Tom Shelton" <to*@mtogden.com> wrote in message
news:e2**************@tk2msftngp13.phx.gbl...
On 2004-01-23, Larry Dodd <la********@sbcglobal.net> wrote:
I am trying to write an application that sits in the system tray and just monitors for a certain Window to be opened. Once this window is opened all keystrokes will be captured. I have the key capture working correctly but cant seem to get the Window thing working.

I think I need to use GetActiveWindow and GetWindowText but not sure. These are the functions that I am using:

Public Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long ' Create string filled with null characters.
Dim strCaption As String
Dim lngLen As Long
Dim currentForm As Form = Form.ActiveForm

Try
' Return length of string.
lngLen = 256
' Call GetActiveWindow to return handle to active window,
' and pass handle to GetWindowText, along with string and its length.
If (GetWindowText(GetForegroundWindow, strCaption, lngLen) > 0 Then
strCaption = GetWindowText(GetForegroundWindow, strCaption, lngLen
' Return value that Windows has written to string.
Return strCaption
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

I am using a timer control to fire the procedure that will look for the
current window's caption.
If someone could tell me how to do this in VB.NET I would really appreciate it.


1. Change your longs to integers - long is 64-bit in .NET
2. Loose the alias, let the runtime determine the correct function
3. Declare handle types as IntPtr
4. Pass System.Text.StringBuilder to API's that are going to fill the
sting buffer.

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
' 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()
Obviously, you'll want to check the return values of the api calls and
take appropriate action :)

--
Tom Shelton [MVP]

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: BadOmen | last post by:
I am using the GetActiveWindow() API but it returns 0 I have a remote that I activate this code with: Code: WinAmpHandler = GetActiveWindow WinAMPhWnd = FindWindow("Winamp v1.x",...
1
by: Mauro | last post by:
Hi, as subject i've a problem with the API GetWindowText. I declare the function GetWindowText: public static extern int GetWindowTextt ( int hwnd, string lpString, int cch );
1
by: R Huff | last post by:
I am trying to capture some text of an external application by using the GetWindowText Win32 function. I am able to successfully retrieve the text of some windows in the application (text boxes,...
10
by: sneffe | last post by:
Hi, im trying to get the caption text from the active window but i cant get it working in vb.net. Can somebody please help me. Im new to using API Public Class Form Inherits...
2
by: Trammel | last post by:
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...
2
by: David A. Osborn | last post by:
I'm having problems getting the caption on the active window handle. The active window handle seems to always come back as zero and the caption blank. Private Declare Function GetActiveWindow...
2
by: abxyz | last post by:
urgent required : how to get input from edit box in vc++ using GetWindowText function
0
by: kloplop321 | last post by:
I found this code(vb only) and it does about the same thing(in vb, not vb .NET 2005) Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute...
4
by: siulm | last post by:
All hi, I would like to move the mouse to X,Y point to any other window application and read its contents providing there is data there. Can GetWindowText do it? is there another function to do...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.