Advertisement
Guest User

AutoIt formatage auto

a guest
Apr 14th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 4.16 KB | None | 0 0
  1.  
  2. ; ------------------------ ;
  3. ;    TEST FORMATAGE AUTO   ;
  4. ; ------------------------ ;
  5.  
  6. #include-once
  7. #include <GUIConstantsEx.au3>
  8. #include <WinAPI.au3>
  9. #include <WindowsConstants.au3>
  10. #include <MsgBoxConstants.au3>
  11. #include <ButtonConstants.au3>
  12. #include <EditConstants.au3>
  13. #include <GUIConstantsEx.au3>
  14. #include <ColorConstants.au3>
  15. #include <Math.au3>
  16.  
  17. Global $MyText =    "Sed @ut@ perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, " & _
  18.                     "totam rem aperiam, eaque ipsa quae ab illo @inventore@ veritatis et quasi architecto beatae vitae dicta sunt explicabo. " & _
  19.                     "Nemo @enim@ ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos " & _
  20.                     "qui ratione @voluptatem@ sequi nesciunt. Neque porro quisquam est, qui @dolorem ipsum@ quia dolor sit amet, consectetur, " & _
  21.                     "adipisci velit, sed quia non numquam eius modi tempora incidunt @ut@ labore et dolore magnam aliquam quaerat voluptatem. " & _
  22.                     "Ut enim ad minima veniam, @quis@ nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi " & _
  23.                     "@consequatur@? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, " & _
  24.                     "vel illum qui dolorem @eum@ fugiat quo voluptas nulla pariatur?"
  25.  
  26. Global $GUI = GUICreate("TEST",500,500)
  27. GUISetState(@SW_SHOW,$GUI)
  28.  
  29. Global $LabelArray[0]
  30. TextToKWLabel($LabelArray,$MyText,50,50,400)
  31.  
  32. While 1
  33.     Sleep(100)
  34.     If GUIGetMsg() == $GUI_EVENT_CLOSE Then
  35.         GUIDelete($GUI)
  36.         ExitLoop
  37.     EndIf
  38. WEnd
  39.  
  40. #cs ------------------------------------
  41. Hauteur = 16
  42. return heigth of the multi-label
  43. @Mot-clef@
  44. #ce ------------------------------------
  45. Func TextToKWLabel(ByRef $Array,$Text,$X,$Y,$Width)
  46.  
  47. Local $temp,$dim,$rank=0
  48. Local $TextArray,$Length,$PosX=$X,$PosY=$Y
  49.  
  50. If Not IsArray($Array) Then Return SetError(2, 0, 0)
  51.  
  52. $TextArray = StringSplit($Text,"@")
  53. $Length = UBound($Array,1)
  54.  
  55. ReDim $Array[$Length+1][_Max(UBound($Array,2),$TextArray[0]+1)]
  56. $Array[$Length][0] = $TextArray[0]
  57.  
  58. For $i = 1 To $TextArray[0]
  59.     If $TextArray[$i] == "" Then ContinueLoop
  60.     $dim = GetStringDimension($TextArray[$i])
  61.     If $PosX+$dim > $X+$Width And Not IsPair($i) Then
  62.         $temp = SimpleStringSplit($TextArray[$i],$Width-$PosX)
  63.         $rank += 1
  64.         ReDim $Array[$Length+1][_Max(UBound($Array,2),$TextArray[0]+1+$rank)]
  65.         $dim = GetStringDimension($temp[0])
  66.         $Array[$Length][$i+$rank-1] = GUICtrlCreateLabel($temp[0],$PosX,$PosY,$dim,16)
  67.         $PosX = $X
  68.         $PosY += 16
  69.         $dim = GetStringDimension($temp[1])
  70.         $Array[$Length][$i+$rank] = GUICtrlCreateLabel($temp[1],$PosX,$PosY,$dim,16)
  71.         $PosX += $dim
  72.     ElseIf $PosX+$dim > $X+$Width Then
  73.         $PosX = $X
  74.         $PosY += 16
  75.         $Array[$Length][$i+$rank] = GUICtrlCreateLabel($TextArray[$i],$PosX,$PosY,$dim,16)
  76.         $PosX += $dim
  77.     Else
  78.         $Array[$Length][$i+$rank] = GUICtrlCreateLabel($TextArray[$i],$PosX,$PosY,$dim,16)
  79.         $PosX += $dim
  80.     EndIf
  81.     If IsPair($i) Then
  82.         GUICtrlSetFont(-1,8.5,0,4)
  83.         GUICtrlSetColor(-1,$COLOR_BLUE)
  84.     EndIf
  85. Next
  86.  
  87. Return $PosY + 16
  88.  
  89. EndFunc
  90.  
  91. Func IsPair($X)
  92.     Return Mod($X,2) == 0
  93. EndFunc
  94.  
  95. ; Split a string in 2 splits while the string width is supp than a max width
  96. Func SimpleStringSplit($String,$Max)
  97.     Local $Return[2]=["",""], $Range = 0
  98.     Local $Words = StringSplit($String," ")
  99.     For $i = 1 To $Words[0]
  100.         If $Words[$i] == "" Then ContinueLoop
  101.         $Range += GetStringDimension("" & $Words[$i] & " ")
  102.         If $Range > $Max Then
  103.             $Return[1] &= $Words[$i]
  104.             If $i <> $Words[0] Then $Return[1] &= " "
  105.         Else
  106.             $Return[0] &= $Words[$i]
  107.             If $i <> $Words[0] Then $Return[0] &= " "
  108.         EndIf
  109.     Next
  110.     Return $Return
  111. EndFunc
  112.  
  113. ; return Width (function in an autoit's example)
  114. Func GetStringDimension($sText)
  115.     Local $hWnd = GUICreate("")
  116.     Local $hDC = _WinAPI_GetDC($hWnd)
  117.     Local $hFont = _SendMessage($hWnd, $WM_GETFONT)
  118.     Local $hSelectObject = _WinAPI_SelectObject($hDC, $hFont)
  119.     Local $tSIZE = _WinAPI_GetTextExtentPoint32($hDC, $sText)
  120.     _WinAPI_SelectObject($hDC, $hSelectObject)
  121.     _WinAPI_ReleaseDC($hWnd, $hDC)
  122.     Local $aReturn = DllStructGetData($tSIZE, 1)
  123.     GUIDelete($hWnd)
  124.     Return $aReturn
  125. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement