Link to home
Start Free TrialLog in
Avatar of Hepen
Hepen

asked on

OnFocus event for Address Bar in Internet Explorer

Anyone know how to get an OnFocus event going for the address bar in an IE Browser?  So when you click in the address bar you pop a msgbox or something?

Hepen
Avatar of Shahid Thaika
Shahid Thaika
Flag of India image

Do you mean the address bar in the real IE, because the IE Web control doesn't include an address bar. If it is in the real IE, there could be a method. There is only one ComboBox control (which is the address bar as well) in the IE window. You can try to use FindWindow and GetNextWindow API (90% sure of them) on IE's window, to search and find the combobox controls handle (.hWnd). With this value, you can use the GetWindowText API through a timer to see if the text has changed. If yes, it'll mean that your address bar has a focus. It is more like a Control_TextChanged sort of event rather than Control_OnFocus event. But considering that people click the address bar only when they want to change the location, I guess this approach is ok.

I would have loved to give you the code for searching the combo box control, but I don't have API guide installed as of now. It has a proper example for the same. If you don't have it, you can download it from www.allapi.net. It and Api Viewer have references and examples of all the APIs that can be used with VB.

Hope this info helps in your solving the problem
Just downloaded API-Guide and got this tutorial. It is for searching and hiding the 'Start Button'. You can modify the code to search the IE window for the combo box.



Const SWP_HIDEWINDOW = &H80
Const SWP_SHOWWINDOW = &H40
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Dim tWnd As Long, bWnd As Long, sSave As String * 250
Private Sub Form_Load()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net

    'This code will hide the Start-button
    'Find the taskbar's handle
    tWnd = FindWindow("Shell_traywnd", vbNullString)
    'Search for a child window
    bWnd = GetWindow(tWnd, GW_CHILD)
    Do
        'get the child window's classname
        GetClassName bWnd, sSave, 250
        'We have the handle of the Start button If the classname is 'button'
        If LCase(Left$(sSave, 6)) = "button" Then Exit Do
        'Search the next child
        bWnd = GetWindow(bWnd, GW_HWNDNEXT)
    Loop
    'Hide the start button
    SetWindowPos bWnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'Show the start button
    SetWindowPos bWnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
End Sub
Avatar of Hepen
Hepen

ASKER

Yes it is for "Real IE" Window.  I'm trying to use your code still have not got it to work yet but i'm working on it. I was hoping to get some kind of hook on the address bar so when you click inside of it an event triggers of some kind?

Hepen
I'll just try to write a source code for you. But till then try the following. Use FindWindow API with 'IEFrame' as the Class name. To find a specific window, type its caption in the caption parameter.

Loop till you find the combo box's handle. Use GetWindowText API to check for the change of text.
ASKER CERTIFIED SOLUTION
Avatar of zzzzzooc
zzzzzooc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is what I have so far. But cannot get the text from the handle. Working on it.

Option Explicit
Private Const WM_GETTEXT = &HD
Private Const SWP_HIDEWINDOW = &H80
Private Const SWP_SHOWWINDOW = &H40
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Dim lhWnd As Long
Dim sSave As String * 255

Private Sub Form_Load()
    'Find the IE Window.Only the first is found.
    lhWnd = FindWindow("IEFrame", vbNullString)
   
    'get handle of child until the combo box is reached
    lhWnd = getHandle(lhWnd, "WorkerW", 7)
    If lhWnd <> 0 Then
        lhWnd = getHandle(lhWnd, "ReBarWindow32", 13)
    Else
        GoTo Out:
    End If
   
    If lhWnd <> 0 Then
        lhWnd = getHandle(lhWnd, "ComboBoxEx32", 12)
    Else
        GoTo Out:
    End If
   
    GetWindowText lhWnd, sSave, 255
   
    If lhWnd <> 0 Then
        lhWnd = getHandle(lhWnd, "ComboBox", 8)
    Else
        GoTo Out:
    End If
   
    GetWindowText lhWnd, sSave, 255
   
    'The combo box has and Edit control (text box!)
    If lhWnd <> 0 Then
        lhWnd = getHandle(lhWnd, "Edit", 4)
    Else
        GoTo Out:
    End If
   
    'Need to write code here to get text some how
    'Tried many things :(
    sSave = SendMessage(lhWnd, WM_GETTEXT, -1, sSave)
    GetWindowText lhWnd, sSave, 255
    Exit Sub
   
Out:
    MsgBox "There was some error!", vbOKOnly + vbCritical, "Error!"
    Unload Me
End Sub

Private Function getHandle(lParent As Long, ClassName As String, cLength As Integer) As Long
Dim strTemp As String * 255
Dim lTemp As Long

'Search for a child window
lTemp = GetWindow(lParent, GW_CHILD)

If lTemp = 0 Then
    getHandle = 0
    Exit Function
End If
   
Do
    'get the child window's classname
    GetClassName lTemp, strTemp, 255
       
    If Not StrComp((Left$(strTemp, cLength)), ClassName, vbTextCompare) Then
        getHandle = lTemp
        Exit Function
    End If
Out:
    'Search the next child
    lTemp = GetWindow(lTemp, GW_HWNDNEXT)
Loop
End Function









OK FINALLY GOT THE PERFECT SOLUTION
JUST ADD A TIMER TO A FORM AND COPY THE BELOW CODE. :)











Option Explicit

Private Const WM_GETTEXTLENGTH = &HE
Private Const WM_GETTEXT = &HD

Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private lhWnd As Long
Private dhWnd As Long
Private currAddress As String

Private Sub Form_Load()
    'Find the IE Window.Only the first is found.
    lhWnd = FindWindow("IEFrame", vbNullString)
   
    'get handle of child until the combo box is reached
    lhWnd = getHandle(lhWnd, "WorkerW", 7)
    If lhWnd <> 0 Then
        lhWnd = getHandle(lhWnd, "ReBarWindow32", 13)
    Else
        GoTo Out:
    End If
   
    If lhWnd <> 0 Then
        lhWnd = getHandle(lhWnd, "ComboBoxEx32", 12)
    Else
        GoTo Out:
    End If
   
    If lhWnd <> 0 Then
        lhWnd = getHandle(lhWnd, "ComboBox", 8)
    Else
        GoTo Out:
    End If
   
    'The combo box has and Edit control (text box!)
    If lhWnd <> 0 Then
        lhWnd = getHandle(lhWnd, "Edit", 4)
    Else
        GoTo Out:
    End If
   
    currAddress = getAddress
    dhWnd = GetDesktopWindow
    Exit Sub
   
Out:
    MsgBox "There was some error!", vbOKOnly + vbCritical, "Error!"
    Unload Me
End Sub

Private Function getHandle(lParent As Long, ClassName As String, cLength As Integer) As Long
Dim strTemp As String * 255
Dim lTemp As Long

'Search for a child window
lTemp = GetWindow(lParent, GW_CHILD)

If lTemp = 0 Then
    getHandle = 0
    Exit Function
End If
   
Do
    'get the child window's classname
    GetClassName lTemp, strTemp, 255
       
    If Not StrComp((Left$(strTemp, cLength)), ClassName, vbTextCompare) Then
        getHandle = lTemp
        Exit Function
    End If
   
    currAddress = getAddress
   
    dhWnd = GetDesktopWindow
Out:
    'Search the next child
    lTemp = GetWindow(lTemp, GW_HWNDNEXT)
Loop
End Function

Private Sub Timer1_Timer()
If currAddress <> getAddress Then
    MessageBox dhWnd, "The text in the address bar has been changed", "Changed", 64
    currAddress = getAddress
End If
End Sub

Private Function getAddress() As String
Dim txtLen As Long
Dim sSave As String
txtLen = SendMessage(lhWnd, WM_GETTEXTLENGTH, 0, 0)
txtLen = txtLen + 1
sSave = Space(txtLen)
txtLen = SendMessage(lhWnd, WM_GETTEXT, txtLen, ByVal sSave)
getAddress = Trim$(sSave)
End Function


This has definitely got to answer your question :)
Hey Hepen,
Does the code solve your problem. Let me know :)