GetActiveWindow and GetWindowText

L

Larry Dodd

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.
 
T

Tom Shelton

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

Larry Dodd

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.
 

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