Jump to content

Hide edit scroll bars if not used


toonboon
 Share

Recommended Posts

I know I've done it before, but I forgot how I did it, and any and all searches give me no usable results so I'll just post a thread.

How do I create an edit control with scroll bars that only show when the text length/width exceeds the control size?

[right]~What can I say, I'm a Simplistic person[/right]

Link to comment
Share on other sites

This could simulate what you want. I made it show the scroll bars after 14 lines (the limit of this particular GUI). If you go over 14 lines and then back to 14 or less, it takes away the scrollbars. It does not account for horizontal scrollbars. That might prove to be more difficult.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiScrollBars.au3>
#include <GuiEdit.au3>

$scroll = 0
$Form1 = GUICreate("Form1", 362, 211, 193, 125)
$Edit1 = GUICtrlCreateEdit("", 16, 8, 329, 193, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN))
GUICtrlSetData(-1, "1" & @crlf & "2" & @crlf & "3" & @crlf & "4" & @crlf & "5" & @crlf & "6" & @crlf & "7" & @crlf & "8" & @crlf & "9" & @crlf & "10" & @crlf & "11" & @crlf & "12" & @crlf & "13"); & @crlf & "14" & @crlf & "15" & @crlf & "16" & @crlf & "17" & @crlf & "18")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    $lineCount = _GUICtrlEdit_GetLineCount($Edit1)
    If $lineCount > 14 AND $scroll = 0 Then
        GUICtrlSetStyle($Edit1, BitOR($ES_AUTOVSCROLL,$WS_VSCROLL,$ES_WANTRETURN))
        $scroll = 1
    ElseIf $lineCount <= 14 AND $scroll = 1 Then
        GUICtrlSetStyle($Edit1, BitOR($ES_AUTOVSCROLL,$ES_WANTRETURN))
        $scroll = 0
    EndIf
    sleep (50)
    
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

#include <ByteMe.au3>

Link to comment
Share on other sites

This works for the vertical scroll bar.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScrollBarConstants.au3>
#include <GuiScrollBars.au3>
#include <GuiEdit.au3>
#include <WinAPI.au3>

Opt( "MustDeclareVars", 1 )
Global $tSCROLLINFO = DllStructCreate($tagSCROLLINFO)
Global $hScrollBar = False, $vScrollBar = False
Global $hEdit
MainProgram()
Func MainProgram()
Local $hGui = GUICreate( "Edit", 400, 156, 590, 200 )
Local $idEdit = GUICtrlCreateEdit( "", 10, 10, 380, 136 )
$hEdit = GUICtrlGetHandle( $idEdit )
_GUIScrollBars_ShowScrollBar($hEdit, $SB_HORZ, $hScrollBar)
_GUIScrollBars_ShowScrollBar($hEdit, $SB_VERT, $vScrollBar)
DllStructSetData($tSCROLLINFO, "fMask", BitOR($SIF_RANGE, $SIF_PAGE))
DllStructSetData($tSCROLLINFO, "nMin", 0)
DllStructSetData($tSCROLLINFO, "nPage", 0)
DllStructSetData($tSCROLLINFO, "nMax", 0)
_GUIScrollBars_SetScrollInfo($hEdit, $SB_HORZ, $tSCROLLINFO)
_GUIScrollBars_SetScrollInfo($hEdit, $SB_VERT, $tSCROLLINFO)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState()
While 1
  Switch GUIGetMsg()
   Case $GUI_EVENT_CLOSE
    Exit
  EndSwitch
WEnd
EndFunc
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg
Local $hWndFrom, $iIDFrom, $iCode
$hWndFrom = $ilParam
$iIDFrom = _WinAPI_LoWord($iwParam)
$iCode = _WinAPI_HiWord($iwParam)
Switch $hWndFrom
  Case $hEdit
   Switch $iCode
    Case $EN_CHANGE
     Local $lin = _GUICtrlEdit_GetLineCount( $hEdit )
     $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hEdit, $SB_VERT)
     Local $Max = DllStructGetData($tSCROLLINFO, "nMax")
     Local $Page = DllStructGetData($tSCROLLINFO, "nPage")
     If $lin <= $Page Then
      If $vScrollBar Then
       $vScrollBar = Not $vScrollBar
       DllStructSetData($tSCROLLINFO, "fMask", BitOR($SIF_RANGE, $SIF_PAGE))
       DllStructSetData($tSCROLLINFO, "nMin", 0)
       DllStructSetData($tSCROLLINFO, "nMax", 0)
       DllStructSetData($tSCROLLINFO, "nPage", 0)
       _GUIScrollBars_SetScrollInfo($hEdit, $SB_VERT, $tSCROLLINFO)
       _GUIScrollBars_ShowScrollBar($hEdit, $SB_VERT, $vScrollBar)
      EndIf
     EndIf
    Case $EN_VSCROLL
     $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hEdit, $SB_VERT)
     Local $Max = DllStructGetData($tSCROLLINFO, "nMax")
     Local $Page = DllStructGetData($tSCROLLINFO, "nPage")
     If $Max >= $Page Then
      If Not $vScrollBar Then
       $vScrollBar = Not $vScrollBar
       _GUIScrollBars_ShowScrollBar($hEdit, $SB_VERT, $vScrollBar )
      EndIf
     EndIf
   EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

After a second look I have changed the example in the previous post a little bit. I think this works nice for an edit control with a vertical scroll bar and with or without word wrap.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScrollBarConstants.au3>
#include <GuiScrollBars.au3>
#include <GuiEdit.au3>

Opt( "MustDeclareVars", 1 )
Global Const $fWordWrap = True
Global Const $iLinesPrPage = 10
Global $tSCROLLINFO = DllStructCreate($tagSCROLLINFO)
Global $hScrollBar = False, $vScrollBar = False
Global $hEdit
MainProgram()
Func MainProgram()
Local $height = $iLinesPrPage * 13 + 6
Local $style = $GUI_SS_DEFAULT_EDIT
If $fWordWrap Then $style = $ES_WANTRETURN+$WS_VSCROLL
Local $hGui = GUICreate( "Edit", 400, $height+20, 590, 200 )
Local $idEdit = GUICtrlCreateEdit( "", 10, 10, 380, $height, $style )
$hEdit = GUICtrlGetHandle( $idEdit )
_GUIScrollBars_ShowScrollBar($hEdit, $SB_VERT, $vScrollBar)
If Not $fWordWrap Then _GUIScrollBars_ShowScrollBar($hEdit, $SB_HORZ, $hScrollBar)
DllStructSetData($tSCROLLINFO, "fMask", BitOR($SIF_RANGE, $SIF_PAGE))
DllStructSetData($tSCROLLINFO, "nMin", 0)
DllStructSetData($tSCROLLINFO, "nMax", 0)
DllStructSetData($tSCROLLINFO, "nPage", 0)
_GUIScrollBars_SetScrollInfo($hEdit, $SB_VERT, $tSCROLLINFO)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
; Initial data
;Local $data = "Data" & @CRLF ;& "Data" & @CRLF
;GUICtrlSetData( $idEdit, $data )
GUISetState()
Send("^{END}") ; To adjust scroll bar to any initial data
Send("^{HOME}")
While 1
  Switch GUIGetMsg()
   Case $GUI_EVENT_CLOSE
    Exit
  EndSwitch
WEnd
EndFunc
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg
Local $hWndFrom, $iIDFrom, $iCode
$hWndFrom = $ilParam
$iIDFrom = BitAND($iwParam, 0xFFFF) ; Low word
$iCode = BitShift($iwParam, 16)   ; High word
Switch $hWndFrom
  Case $hEdit
   Switch $iCode
    Case $EN_CHANGE
     Local $Lin = _GUICtrlEdit_GetLineCount( $hEdit )
     $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hEdit, $SB_VERT)
     Local $Page = DllStructGetData($tSCROLLINFO, "nPage")
     If $Lin <= $Page Then
      If $vScrollBar Then
       $vScrollBar = Not $vScrollBar
       DllStructSetData($tSCROLLINFO, "fMask", BitOR($SIF_RANGE, $SIF_PAGE))
       DllStructSetData($tSCROLLINFO, "nMin", 0)
       DllStructSetData($tSCROLLINFO, "nMax", 0)
       DllStructSetData($tSCROLLINFO, "nPage", 0)
       _GUIScrollBars_SetScrollInfo($hEdit, $SB_VERT, $tSCROLLINFO)
       _GUIScrollBars_ShowScrollBar($hEdit, $SB_VERT, $vScrollBar)
      EndIf
     ElseIf $Lin > $iLinesPrPage Then
      Local $Max = DllStructGetData($tSCROLLINFO, "nMax")
      If $Max >= $Page Then
       If Not $vScrollBar Then
        $vScrollBar = Not $vScrollBar
        _GUIScrollBars_ShowScrollBar($hEdit, $SB_VERT, $vScrollBar )
       EndIf
      EndIf
     EndIf
    Case $EN_VSCROLL
     $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hEdit, $SB_VERT)
     Local $Max = DllStructGetData($tSCROLLINFO, "nMax")
     Local $Page = DllStructGetData($tSCROLLINFO, "nPage")
     If $Max >= $Page Then
      If Not $vScrollBar Then
       $vScrollBar = Not $vScrollBar
       _GUIScrollBars_ShowScrollBar($hEdit, $SB_VERT, $vScrollBar )
      EndIf
     EndIf
   EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

If the Edit control is read only this works for both scrollbars.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScrollBarConstants.au3>
#include <GuiScrollBars.au3>
#include <GuiEdit.au3>

Opt( "MustDeclareVars", 1 )
Global Const $iLinesPrPage = 10
Global Const $iHorzScrollBarHeight = 17
Global $tSCROLLINFO = DllStructCreate($tagSCROLLINFO)
Global $hScrollBar = False, $vScrollBar = False
Global $hEdit
MainProgram()
Func MainProgram()
Local $style = $GUI_SS_DEFAULT_EDIT+$ES_READONLY
Local $height = $iLinesPrPage * 13 + 6 + $iHorzScrollBarHeight
Local $hGui = GUICreate( "Edit", 400, $height+20, 590, 200 )
Local $idEdit = GUICtrlCreateEdit( "", 10, 10, 380, $height, $style )
GUICtrlSetBkColor( $idEdit, 0xFFFFFF )
$hEdit = GUICtrlGetHandle( $idEdit )
_GUIScrollBars_ShowScrollBar($hEdit, $SB_HORZ, $hScrollBar)
_GUIScrollBars_ShowScrollBar($hEdit, $SB_VERT, $vScrollBar)
DllStructSetData($tSCROLLINFO, "fMask", BitOR($SIF_RANGE, $SIF_PAGE))
DllStructSetData($tSCROLLINFO, "nMin", 0)
DllStructSetData($tSCROLLINFO, "nPage", 0)
DllStructSetData($tSCROLLINFO, "nMax", 0)
_GUIScrollBars_SetScrollInfo($hEdit, $SB_HORZ, $tSCROLLINFO)
_GUIScrollBars_SetScrollInfo($hEdit, $SB_VERT, $tSCROLLINFO)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
; Initial data
Local $data = "00 Test" & @CRLF & _
        "01 Test" & @CRLF & _
        "02 Test" & @CRLF & _
        "03 Test" & @CRLF & _
        "04 Test 0 Test 1 Test 2 Test 3 Test 4 Test 5 Test 6 Test 7 Test 8 Test 9" & @CRLF & _
        "05 Test" & @CRLF & _
        "07 Test" & @CRLF & _
        "08 Test" & @CRLF & _
        "09 Test 0 Test 1 Test 2 Test 3 Test 4 Test 5 Test 6 Test 7 Test 8 Test 9 Test 0" & @CRLF & _
        "10 Test" & @CRLF & _
        "11 Test" & @CRLF & _
        "12 Test" & @CRLF & _
        "13 Test" & @CRLF & _
        "14 Test 0 Test 1 Test 2 Test 3 Test 4 Test 5 Test 6 Test 7 Test 8 Test 9 Test 0 Test 1" & @CRLF & _
        "15 Test"
GUICtrlSetData( $idEdit, $data )
GUISetState()
_GUICtrlEdit_LineScroll( $hEdit, 1, 1 ) ; Adjust scrollbars
Send("^{HOME}")
While 1
  Switch GUIGetMsg()
   Case $GUI_EVENT_CLOSE
    Exit
  EndSwitch
WEnd
EndFunc
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg
Local $hWndFrom, $iIDFrom, $iCode
$hWndFrom = $ilParam
$iIDFrom = BitAND($iwParam, 0xFFFF) ; Low word
$iCode = BitShift($iwParam, 16)   ; High word
Switch $hWndFrom
  Case $hEdit
   Switch $iCode
    Case $EN_HSCROLL
     $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hEdit, $SB_HORZ)
     Local $Max = DllStructGetData($tSCROLLINFO, "nMax")
     Local $Page = DllStructGetData($tSCROLLINFO, "nPage")
     If $Max >= $Page Then
      If Not $hScrollBar Then
       $hScrollBar = Not $hScrollBar
       _GUIScrollBars_ShowScrollBar($hEdit, $SB_HORZ, $hScrollBar)
      EndIf
     EndIf
    Case $EN_VSCROLL
     $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hEdit, $SB_VERT)
     Local $Max = DllStructGetData($tSCROLLINFO, "nMax")
     Local $Page = DllStructGetData($tSCROLLINFO, "nPage")
     If $Max >= $Page Then
      If Not $vScrollBar Then
       $vScrollBar = Not $vScrollBar
       _GUIScrollBars_ShowScrollBar($hEdit, $SB_VERT, $vScrollBar)
      EndIf
     EndIf
   EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
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...