NOTE: This issue can now be closed.
Hey bala,
Thank you for taking the time to do this. The code in my project
*looked* fine, but apparently I had something setup incorrectly.
I started a new project, copied over your code and was able to make it work.
For anyone else who's curious, do this:
1. Start a new .NET project.
2. Put a Listbox on the form (Listbox1)
3. Put a timer on the form (Timer1)
4. Insert the following code where appropriate:
Private Const GUI_CARETBLINKING As Integer = &H1S
Private Const GUI_INMOVESIZE As Integer = &H2S
Private Const GUI_INMENUMODE As Integer = &H4S
Private Const GUI_SYSTEMMENUMODE As Integer = &H8S
Private Const GUI_POPUPMENUMODE As Integer = &H10S
Private Const GUI_16BITTASK As Integer = &H20S 'winver >= 5.01
Private Const LB_SETTABSTOPS As Integer = &H192S
Private Const WM_GETTEXT As Integer = &HDS
Private Const WM_GETTEXTLENGTH As Integer = &HES
Private Structure RECT
Dim Left_Renamed As Integer
Dim Top_Renamed As Integer
Dim Right_Renamed As Integer
Dim Bottom_Renamed As Integer
End Structure
Private Structure GUITHREADINFO
Dim cbSize As Integer
Dim flags As Integer
Dim hwndactive As Integer
Dim hwndFocus As Integer
Dim hwndCapture As Integer
Dim hwndMenuOwner As Integer
Dim hwndMoveSize As Integer
Dim hwndcaret As Integer
Dim rcCaret As RECT
End Structure
Private Declare Function GetGuiThreadInfo Lib "user32" Alias
"GetGUIThreadInfo" (ByVal idThread As Integer, ByRef lpgui As
GUITHREADINFO) As Integer
Declare Function GetCurrentThreadId Lib "kernel32" () As Integer
Private Declare Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal
wParam As Integer, ByRef lParam As String) As Integer
Private Declare Function GetWindowTextLength Lib "user32" Alias
"GetWindowTextLengthA" (ByVal hwnd As Integer) As Integer
Private Declare Function GetWindowText Lib "user32" Alias
"GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String,
ByVal cch As Integer) As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
On Error Resume Next
Dim gui As GUITHREADINFO
Static numcalls As Integer
'cbSize must be set before calling
gui.cbSize = Len(gui)
If GetGuiThreadInfo(0, gui) <> 0 Then
'numcalls is just a counter to increment
'a line in the list to show the code is
'working when you rest in one window
numcalls = numcalls + 1
With ListBox1
.Items.Add(CStr(numcalls))
Dim ss
ss = gui.hwndactive
.Items.Add("active window hwnd:" & ss)
ss = GetActiveWindowTitle(gui.hwndactive)
.Items.Add(" active window title:" & ss)
ss = gui.hwndCapture
.Items.Add("mouse capture hwnd:" & ss)
ss = gui.hwndcaret
.Items.Add("showing caret hwnd:" & ss)
ss = GetCaretWindowText(gui.hwndcaret)
.Items.Add("caret window text:" & ss)
ss = -gui.hwndFocus
.Items.Add("keyboard focus hwnd:" & ss)
ss = gui.hwndMenuOwner
.Items.Add("active menu owner hwnd:" & ss)
ss = gui.hwndMoveSize
.Items.Add("move or size loop hwnd:" & ss)
.Items.Add("caret rect (l/r t/b):" & vbTab &
gui.rcCaret.Left_Renamed & "/" & gui.rcCaret.Right_Renamed & " " &
gui.rcCaret.Top_Renamed & "/" & gui.rcCaret.Bottom_Renamed)
End With 'with list1
Else
Me.ListBox1.Items.Add("Error")
End If
End Sub
Private Function GetActiveWindowTitle(ByVal hwndactive As Integer) As String
Dim nLength As Integer
Dim res As Integer
Dim buff As String
'GetWindowText returns the title
'of the window specified as hwndactive
If hwndactive <> 0 Then
nLength = GetWindowTextLength(hwndactive)
If nLength <> 0 Then
buff = Space(nLength + 1)
res = GetWindowText(hwndactive, buff, nLength + 1)
If res <> 0 Then
GetActiveWindowTitle = buff.Substring(0, res)
Exit Function
End If 'if res
End If 'if nlength
End If 'if hwndactive
GetActiveWindowTitle = "(not available)"
End Function
Private Function GetCaretWindowText(ByVal hwndcaret As Integer) As String
Dim nLength As Integer
Dim res As Integer
Dim buff As String
'WM_GETTEXT retrieves the text
'from edit and rich text controls
If hwndcaret <> 0 Then
nLength = SendMessage(hwndcaret, WM_GETTEXTLENGTH, 0, 0)
If nLength <> 0 Then
buff = Space(nLength + 1)
res = SendMessage(hwndcaret, WM_GETTEXT, nLength + 1, buff)
If res <> 0 Then
GetCaretWindowText = buff.Substring(0, res)
Exit Function
End If 'if res
End If 'if nlength
End If 'if hwndcaret
GetCaretWindowText = "(not available)"
End Function
'TinkerBee |