Control position in GUI with scrollbars

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 849
Joined: 21 Dec 2015, 02:34

Control position in GUI with scrollbars

20 Jan 2018, 20:44

Evening all

I'd be greatful for a little help. For those willing, please (and thank you):



1. run the script
2. scroll all the way to the right hand corner of the gui (but don't resize the GUI)
3. when you see "MYTEXT", left click on it and hold for 1/ second, then drag it a tiny bit to the left and up (20 or 30 pixels maybe)
4. a message box shows the new x,y location of MYTEXT

the problem: "MYTEXT" as per the script is initially located at x875 y300 in the GUI. If we drag it just a few pixels to the left and up, the messagebox should report the new coordinates as something like x850 y275 (approximate).

But it's reporting them in relation to the current size of the GUI (w200 h200). So it reports something like x130 y180 (approximate).

How do I get it to report the new x,y in relation to the original position which was x875 y300?

Code: Select all


CoordMode, mouse,screen
Gui, +AlwaysOnTop +ToolWindow
Gui, +Resize +0x300000
Gui, Font, s8, Arial
Gui, 1:+HWND1ID

Gui, 1: Add, text, x875 y300 gppLaunch, MYTEXT ;even though this text is outside of the visible w200 h200 GUI boundaries, the horizontal scrollbar still won't invoke during script startup, so adding a wide button ato the GUI seems to invoke the scrollbar:

Gui, 1: Add , button, x0 y0 w900 h25, button ;this very wide button is added to invoke the horizonal scrollbar when the script is first executed

Gui, 1: Show, x200 y200 w200 h200, GUI

coordmode, mouse, screen

OnMessage(0x115, "OnScroll") ; WM_VSCROLL
OnMessage(0x114, "OnScroll") ; WM_HSCROLL

Return


;=============================================================================================
AppLaunch:
KeyWait, LButton, T0.15
If ErrorLevel ; drag the control
 
{
soundbeep
GoTo, ControlMove
}

else

{
MouseGetPos,,, OutputVarWin, OutputVarCtl
ControlGetText, TextVar2 , %OutputVarCtl%, ahk_id %OutputVarWin% 
  
if TextVar2=MYTEXT
{
tooltip, you clicked on %TextVar2%
return
}
Return
}
;=============================================================================================








;=============================================================================================
ControlMove:
 MouseGetPos,,,,sHwnd, 2
 Winset,Redraw,,ahk_id %sHwnd%
 SendMessage, 0x112,0xF012,0,,ahk_id %sHwnd% ; [ WM_SYSCOMMAND+SC_MOVE ]
 controlgettext, outputtext, %A_GuiControl%, GUI
 
 CoordMode, mouse,relative
 ControlGetPos, x, y, w, h, %A_GuiControl%, GUI
 
 Winset,Redraw,,ahk_id %sHwnd%
 msgbox, %sHwnd% and text is %outputtext% and x is %x% and y is %y%
 
Return
;=============================================================================================













;SCROLLBAR FUNCTIONALITY BELOW:
GuiSize:
    UpdateScrollBars(A_Gui, A_GuiWidth, A_GuiHeight)
return


UpdateScrollBars(GuiNum, GuiWidth, GuiHeight)
{
    static SIF_RANGE=0x1, SIF_PAGE=0x2, SIF_DISABLENOSCROLL=0x8, SB_HORZ=0, SB_VERT=1
    
    Gui, %GuiNum%:Default
    Gui, +LastFound
    
    ; Calculate scrolling area.
    Left := Top := 9999
    Right := Bottom := 0
    WinGet, ControlList, ControlList
    Loop, Parse, ControlList, `n
    {
        GuiControlGet, c, Pos, %A_LoopField%
        if (cX < Left)
            Left := cX
        if (cY < Top)
            Top := cY
        if (cX + cW > Right)
            Right := cX + cW
        if (cY + cH > Bottom)
            Bottom := cY + cH
    }
    Left -= 8
    Top -= 8
    Right += 8
    Bottom += 8
    ScrollWidth := Right-Left
    ScrollHeight := Bottom-Top
    
    ; Initialize SCROLLINFO.
    VarSetCapacity(si, 28, 0)
    NumPut(28, si) ; cbSize
    NumPut(SIF_RANGE | SIF_PAGE, si, 4) ; fMask
    
    ; Update horizontal scroll bar.
    NumPut(ScrollWidth, si, 12) ; nMax
    NumPut(GuiWidth, si, 16) ; nPage
    DllCall("SetScrollInfo", "uint", WinExist(), "uint", SB_HORZ, "uint", &si, "int", 1)
    
    ; Update vertical scroll bar.
;     NumPut(SIF_RANGE | SIF_PAGE | SIF_DISABLENOSCROLL, si, 4) ; fMask
    NumPut(ScrollHeight, si, 12) ; nMax
    NumPut(GuiHeight, si, 16) ; nPage
    DllCall("SetScrollInfo", "uint", WinExist(), "uint", SB_VERT, "uint", &si, "int", 1)
    
    if (Left < 0 && Right < GuiWidth)
        x := Abs(Left) > GuiWidth-Right ? GuiWidth-Right : Abs(Left)
    if (Top < 0 && Bottom < GuiHeight)
        y := Abs(Top) > GuiHeight-Bottom ? GuiHeight-Bottom : Abs(Top)
    if (x || y)
        DllCall("ScrollWindow", "uint", WinExist(), "int", x, "int", y, "uint", 0, "uint", 0)
}

OnScroll(wParam, lParam, msg, hwnd)
{
    static SIF_ALL=0x17, SCROLL_STEP=10
    
    bar := msg=0x115 ; SB_HORZ=0, SB_VERT=1
    
    VarSetCapacity(si, 28, 0)
    NumPut(28, si) ; cbSize
    NumPut(SIF_ALL, si, 4) ; fMask
    if !DllCall("GetScrollInfo", "uint", hwnd, "int", bar, "uint", &si)
        return
    
    VarSetCapacity(rect, 16)
    DllCall("GetClientRect", "uint", hwnd, "uint", &rect)
    
    new_pos := NumGet(si, 20) ; nPos
    
    action := wParam & 0xFFFF
    if action = 0 ; SB_LINEUP
        new_pos -= SCROLL_STEP
    else if action = 1 ; SB_LINEDOWN
        new_pos += SCROLL_STEP
    else if action = 2 ; SB_PAGEUP
        new_pos -= NumGet(rect, 12, "int") - SCROLL_STEP
    else if action = 3 ; SB_PAGEDOWN
        new_pos += NumGet(rect, 12, "int") - SCROLL_STEP
    else if (action = 5 || action = 4) ; SB_THUMBTRACK || SB_THUMBPOSITION
        new_pos := wParam>>16
    else if action = 6 ; SB_TOP
        new_pos := NumGet(si, 8, "int") ; nMin
    else if action = 7 ; SB_BOTTOM
        new_pos := NumGet(si, 12, "int") ; nMax
    else
        return
    
    min := NumGet(si, 8, "int") ; nMin
    max := NumGet(si, 12, "int") - NumGet(si, 16) ; nMax-nPage
    new_pos := new_pos > max ? max : new_pos
    new_pos := new_pos < min ? min : new_pos
    
    old_pos := NumGet(si, 20, "int") ; nPos
    
    x := y := 0
    if bar = 0 ; SB_HORZ
        x := old_pos-new_pos
    else
        y := old_pos-new_pos
    ; Scroll contents of window and invalidate uncovered area.
    DllCall("ScrollWindow", "uint", hwnd, "int", x, "int", y, "uint", 0, "uint", 0)
    
    ; Update scroll bar.
    NumPut(new_pos, si, 20, "int") ; nPos
    DllCall("SetScrollInfo", "uint", hwnd, "int", bar, "uint", &si, "int", 1)
}

;=============================================================================================
Last edited by scriptor2016 on 27 Jan 2018, 03:00, edited 1 time in total.
scriptor2016
Posts: 849
Joined: 21 Dec 2015, 02:34

Re: Control position in GUI with scrollbars

27 Jan 2018, 02:57

I've spent the last week on this and am determined to solve it. I've tried every mathematical sequence to figure it out but I'm doing something wrong. I need another approach because I've exhausted myself with this.

To try another aproach, I'm wondering about the following code. When holding down shift over a control, it returns the position of the control relative to the window it's in.

Is there a way to make it return the control's position relative to the entire screen?

I think I can do it by getting the x,y position of the window which contains the control and then adding those x,y values to the x,y values of the control's position, but I was wondering if there's another more direct way to do it?

Code: Select all

#SingleInstance, Force

Loop {

MouseGetPos,,,, hCtrl, 2

ControlGetPos, X, Y,,,, ahk_id %hCtrl%

If GetKeyState( "Shift" )

Tooltip, %  "`nPos:`t" X "," Y "`n`n" Text

Else Tooltip

Sleep 1

}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 261 guests