How to change the position of Scrollabe Gui Scrollbar?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fabricio234
Posts: 122
Joined: 06 Mar 2020, 21:48

How to change the position of Scrollabe Gui Scrollbar?

Post by fabricio234 » 21 Apr 2022, 18:15

This script adds horizontal/vertical scrollbar into the GUI, would like to ask if someone knows how to change the position of these ScrollBars

I mean these:
AutoHotkey_2022-04-21_19-50-23.png
AutoHotkey_2022-04-21_19-50-23.png (3.41 KiB) Viewed 269 times
By change position I mean, repositioning the xy position where the scrollbar is drawn, something like this:

Photoshop_2022-04-21_20-13-04.png
Photoshop_2022-04-21_20-13-04.png (2.95 KiB) Viewed 269 times

Code: Select all

SetBatchLines, -1
OnMessage(0x115, "OnScroll")
OnMessage(0x114, "OnScroll")
OnMessage(0x020A, "WM_MOUSEWHEEL")


Gui, +hWndScrollableGuiHwnd +0x300000


Loop, 100 {
	y +=40
	Gui, Add, Button, x30 y%y%, %A_Index%
}
Gui, Show, w300 h300, ScrollGui
Return



Esc::ExitApp
GuiSize:
FileAppend, A_GuiHeight: %A_GuiHeight%`n,*
UpdateScrollBars(A_Gui, A_GuiWidth, A_GuiHeight)
Return


; For scroll functionality
;-------------------------
WM_MOUSEWHEEL(wParam, lParam, msg, hWnd) {

	Global ScrollableGuiHwnd
   Debug := 1

   If (Debug) {
      ; Scroll up
      If ((wParam >> 16) < 32768)
         OnScroll(0, 1, 0x115, ScrollableGuiHwnd)
      Else ; Scroll down
         OnScroll(1, 1, 0x115, ScrollableGuiHwnd)
   }

}



; OnScroll and UpdateScrollBars by Lexikos (made x64/x32 compatible by jeeswg)
; https://www.autohotkey.com/boards/viewtopic.php?p=237412#p237412
OnScroll(wParam, lParam, msg, hwnd) {

   Static SCROLL_STEP := 20

   Critical

   bar := msg=0x115
   VarSetCapacity(si, 28, 0)
   NumPut(28, si, 0, "uint")   ; cbSize
   NumPut(0x17, si, 4, "uint") ; fMask

   If !DllCall("GetScrollInfo", "ptr", hwnd, "int", bar, "ptr", &si)
      Return

   VarSetCapacity(Rect, 16)
   DllCall("GetClientRect", "ptr", hWnd, "ptr", &Rect)

   rX := NumGet(rect, 0, "int")
   rY := NumGet(rect, 4, "int")
   rW := NumGet(rect, 8, "int")
   rH := NumGet(rect, 12, "int")

   new_pos := NumGet(si, 20, "int") ; 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, "uint") ; 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", "ptr", hWnd, "int", x, "int", y, "ptr", 0, "ptr", 0)
   
   ; Update scroll bar.
   NumPut(new_pos, si, 20, "int") ; nPos
   DllCall("SetScrollInfo", "ptr", hWnd, "int", bar, "ptr", &si, "int", 1)

   ;DllCall("UpdateWindow", "ptr", hWnd)
   ; Redraw.
   DllCall("User32.dll\InvalidateRect", "Ptr", hWnd, "Ptr", 0, "Int", 1)
   DllCall("UpdateWindow", "ptr", hWnd)

}



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 -= 10
   Top -= 10
   Right += 10
   Bottom += 10
   ScrollWidth := 3200 ;Right-Left
   ScrollHeight := Bottom-Top

   ; Initialize SCROLLINFO.
   VarSetCapacity(si, 28, 0)
   NumPut(28, si, 0, "uint") ; cbSize
   NumPut(SIF_RANGE | SIF_PAGE, si, 4, "uint") ; fMask

   ; Update horizontal scroll bar.
   NumPut(ScrollWidth, si, 12, "int") ; nMax
   NumPut(GuiWidth, si, 16, "uint") ; nPage
   DllCall("SetScrollInfo", "ptr", WinExist(), "int", SB_HORZ, "ptr", &si, "int", 1)

   ; Update vertical scroll bar.
   ; NumPut(SIF_RANGE | SIF_PAGE | SIF_DISABLENOSCROLL, si, 4, "uint") ; fMask
   NumPut(ScrollHeight, si, 12, "int") ; nMax
   NumPut(GuiHeight, si, 16, "uint") ; nPage

   DllCall("SetScrollInfo", "ptr", WinExist(), "int", SB_VERT, "ptr", &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)
   
   FileAppend, x: %x% y: %y%`n,*
   
   If (x || y)
      DllCall("ScrollWindow", "ptr", WinExist(), "int", x, "int", y, "ptr", 0, "ptr", 0)

}

Return to “Ask for Help (v1)”