Jump to content

How get width/height of embedded IE control?


Recommended Posts

I want to get the position of an embedded IE control (either relative to the screen or relative to the window is ok).

ControlGetPos on the return value from _IECreateEmbedded seems to work at the start, but if I click on any other control (such as the label in the example) it returns the position and size of the label instead of the IE control. What's happening? muttley

ControlGetPos returns 0 for the controlID from the return value of GUICtrlCreateObj($oIE, 10, 40, 620, 400). Why doesn't ControlGetPos on $GUIActiveX below return an array that has the position and size of the control? Is $GUIActiveX not the correct controlID I'm supposed to be using?

Here is an example script:

#include <GUIConstants.au3>
#include <IE.au3>

Opt("GUIOnEventMode", 1)

Local $winTitle = @ScriptName

Local $gui = GUICreate($winTitle, 640, 480, default, default, BitOR($WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_MINIMIZEBOX)) ;$WS_POPUP,$WS_EX_TOPMOST+$WS_EX_TOOLWINDOW = 136
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitScript")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "WindowResized")
GUISetOnEvent($GUI_EVENT_RESTORE, "WindowResized")
GUISetOnEvent($GUI_EVENT_PRIMARYUP, "WindowResized")
GUISetOnEvent($GUI_EVENT_RESIZED, "WindowResized")

Local $oIE = _IECreateEmbedded()

Local $GUIActiveX = GUICtrlCreateObj($oIE, 10, 40, 620, 400)
GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)

_IENavigate($oIE, "http://www.autoitscript.com")
_IELoadWait($oIE, 100, 5000)

Local $lbl = GUICtrlCreateLabel(LabelText(), 10, 4, 600, 30)
GUICtrlSetResizing(-1, $GUI_DOCKALL)

GUISetState()

While 1
    Sleep(50)
WEnd

Func LabelText()
    $pos = ControlGetPos($winTitle, "", $oIE)
    
    If IsArray($pos) Then
        Return "Click this text and then resize the window.  Notice the values are for the last selected control, not the IE control." & @CRLF & _
        "ControlGetPos($winTitle, "", $oIE) return values: " & $pos[0] & ", " & $pos[1] & ", " & $pos[2] & ", " & $pos[3]
    Else
        Return "ControlGetPos did not return an array.  @error = " & @error
    EndIf
EndFunc

Func WindowResized()
    GUICtrlSetData($lbl, LabelText())
EndFunc

Func ExitScript()
    Exit
EndFunc
Link to comment
Share on other sites

This cleaned up version works fine for me:

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>

Opt("GUIOnEventMode", 1)

Global $winTitle = @ScriptName
Global $gui = GUICreate($winTitle, 640, 480, Default, Default, BitOR($WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_MINIMIZEBOX));$WS_POPUP,$WS_EX_TOPMOST+$WS_EX_TOOLWINDOW = 136
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitScript")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "WindowResized")
GUISetOnEvent($GUI_EVENT_RESTORE, "WindowResized")
GUISetOnEvent($GUI_EVENT_PRIMARYUP, "WindowResized")
GUISetOnEvent($GUI_EVENT_RESIZED, "WindowResized")

Global $oIE = _IECreateEmbedded()

Global $GUIActiveX = GUICtrlCreateObj($oIE, 10, 40, 620, 400)
GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)

_IENavigate($oIE, "http://www.autoitscript.com")
_IELoadWait($oIE, 100, 5000)

Global $lbl = GUICtrlCreateLabel("Click this text and then resize the window." & @CRLF & "Notice the values are for the last selected control, not the IE control.", 10, 4, 600, 30)
GUICtrlSetResizing(-1, $GUI_DOCKALL)

GUISetState()

While 1
    Sleep(50)
WEnd

Func LabelText()
    Local $pos = ControlGetPos($winTitle, "", $oIE)

    If IsArray($pos) Then
        TrayTip("LabelText()", "$pos is an array: " & $pos[0] & ", " & $pos[1] & ", " & $pos[2] & ", " & $pos[3], 5)
        Return "ControlGetPos($winTitle, "", $oIE) return values: " & $pos[0] & ", " & $pos[1] & ", " & $pos[2] & ", " & $pos[3]
    Else
        TrayTip("LabelText()", "$pos is not an array", 5)
        Return "ControlGetPos did not return an array.  @error = " & @error
    EndIf
EndFunc  ;==>LabelText

Func WindowResized()
    GUICtrlSetData($lbl, LabelText())
EndFunc  ;==>WindowResized

Func ExitScript()
    Exit
EndFunc  ;==>ExitScript

The fact that you were able to run your version without #include <WindowsConstants.au3> proves you are using an out-of-date version of AuotIt. Download AutoIt 3.2.12.1 at least.

It is a little strange that you have to use $oIE instead of $GUIActiveX in ControlGetPos(), because I think the Control ID should have worked too.

muttley

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...