Jump to content

Center Vertically in InputBox


momar33
 Share

Recommended Posts

Hi Momar33,

I wrote the function espicially for you. It should works. Notice, that you estimate manuaky the maximal number of rows and the maximal number of character in one row.

#include <array.au3>
#include <GUIConstants.au3>

GUICreate(" Centered Inputbox", 200,250)
$cInp = GUICtrlCreateInput ("", 10,  35, 180, 80, BitOr($ES_CENTER, $ES_MULTILINE))  ; will not accept drag&drop files
GUICtrlCreateLabel('Center vertically this text above:', 10, 140, 120, 16)
$cInp1 = GUICtrlCreateInput ("Three or four words", 10,  160, 180, 30, $ES_MULTILINE) 

$btn = GUICtrlCreateButton ("Ok", 40,  195, 60, 20)

GUISetState () 

$msg = 0
While $msg <> $GUI_EVENT_CLOSE
       $msg = GUIGetMsg()
       Select
           Case $msg = $btn
               _CenterV($cInp, GUICtrlRead($cInp1), 5, 29)
       EndSelect
Wend

;===============================================================================
;
; Description:    Centers vertically text in a inputbox
; Syntax:          _CenterV($hControl, $sText, $iMaxRows, $iMaxStringLenPerRow)
; Parameter(s):  $hControl - handle of the inputbox, which much be created with $ES_CENTER
;                   $sText - text for the inputbox
;                   $iMaxRows - maximal number of the rows in the inputbox
;                   $iMaxStringPerRow - maximal number of characters in one row of the inputboxbox
; Requirement(s):   inputbox, which much be created with $ES_CENTER
; Return Value(s):  On Success - Returns the converted text and inserts converted text in the inputbox
;                  On Failure - Returns '' and sets @error = 1
; Author(s):        jlorenz1 <jlorenz1 at web dot de>
; Note(s):        $iMaxStringPerRow depends on the width of the inputbox and the format of character
;
;===============================================================================

Func _CenterV($hControl, $sText, $iMaxRows, $iMaxStringLenPerRow)
    Dim $iErr = 0, $sNew ='', $iCounter = 1, $sEmptyRow ='', $sFullRow='', $iEmptyRows= 0
    Dim $avRows[1]
    $avText= StringSplit ($sText, ' ')
    
    If StringLen($sText) > $iMaxStringLenPerRow * $iMaxRows then 
        $iErr = $iErr + 1
    Else        
        For $i = 1 to $avText[0]
            If  StringLen($sNew) + 1 + StringLen($avText[$i]) <= $iMaxStringLenPerRow Then
                $sNew = $sNew & ' ' & $avText[$i]
            Else
                If StringLeft($sNew, 1) == ' ' then StringTrimLeft($sNew, 1)
                $avRows[$iCounter] = $sNew
                $iCounter = $iCounter + 1
                $sNew = $avText[$i]
            EndIf 
        Next
        If $iCounter == 1 then
            ReDim $avRows[$iCounter+1]
            $avRows[$iCounter] = $sNew
        EndIf
        $ii=0
        $avRows[0]= UBound($avRows)-1
        $iEmptyRows= Round(($iMaxRows - $avRows[0])/2, 0)   

        For $i = 1 to $iEmptyRows 
            $sEmptyRow = $sEmptyRow & @CRLF
            $ii =$ii +1
        Next
        
        For $i = 1 to $iCounter
            $sFullRow = $sFullRow &  $avRows[$i]
            If $i == $avRows[0] -1 then ExitLoop
        Next
            
        $sNew = $sEmptyRow & @CRLF & $sFullRow
        GUICtrlSetData ($cInp, '')  
        GUICtrlSetData ($cInp, $sNew)
    EndIf
    SetError ($iErr, 0, $iErr)
    Return  $sNew
EndFunc

Johannes LorenzBensheim, Germanyjlorenz1@web.de[post="12602"]Highlightning AutoIt Syntax in Notepad++ - Just copy in your Profile/application data/notepad++[/post]

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...