Mat Posted July 12, 2009 Share Posted July 12, 2009 (edited) For a project euler thing concerning Pascals triangle, I decided it would be easy to make a sort of GUI for it. And its done!! the only reason this is not in example scripts is becouse there are a few more functions I intend to make for it... But I want to get the basics done first. The problem is simple. I would like it to be a triangle. currently its an edit like this: 1 1.1 1.2.1 1.3.3.1 Nobody post to say this is a triangle. You know what I mean. I could do this: .....1 ....1.1 ...1.2.1 ..1.3.3.1 (with spaces and courier new) but what about this? .........1 ........1.1 .......1.2.1 ......1.3.3.1 .....1.4.6.4.1 ....1.5.10.10.5.1 problem!! and since this is going well into hundreds of rows... there are some big numbers in there!!! I can't think of any solution to this, so any help is appreciated. My code at the mo... or the simple bits anyhow... expandcollapse popup; Pascals triangle $hGUI = GUICreate ("Test Pascals Trinagle", 500, 500, -1, -1, 0x00040000 + 0x00010000 + 0x00020000 + 0x00080000 + 0x00C00000) $hGen = GUICtrlCreateButton ("Generate", 2, 2, 80, 20) GUICtrlSetResizing (-1, 544) $hGet = GUICtrlCreateButton ("Get Row sum", 84, 2, 80, 20) GUICtrlSetResizing (-1, 544) $hEdit = GUICtrlCreateEdit ("Matts Pascals triangle generator", 2, 24, 496, 476) GUICtrlSetResizing (-1, 102) GUISetState () While 1 Switch GUIGetMsg () Case -3 Exit Case $hGen $inp = Inputbox ("Amount to calculate", "Enter the number of rows to calculate.", 50) If @Error Then Return 0 GUICtrlSetData ($hEdit, _Generate ($inp)) Case $hGet $inp = Inputbox ("Amount to calculate", "Enter the row to get the sum of.", 50) If @Error Then Return 0 MsgBox (0, "Result", "Sum of row " & $inp & " is: " & Execute (StringReplace (StringRegExpReplace (_Generate ($inp), ".*\n", ""), ".", "+"))) EndSwitch WEnd Func _Generate ($nNum) Local $aLine[4] = [0, 1, 1, 0], $ret = "1" & @CRLF & "1.1" For $i = 3 to $nNum Local $aNew[$i + 2] $aNew[0] = 0 $aNew[1] = 1 For $n = 2 to $i - 1 $aNew[$n] = $aLine[$n - 1] + $aLine[$n] Next $aNew[$i] = 1 $aNew[$i + 1] = 0 $ret &= @CRLF & "1" For $n = 2 to $i $ret &= "." & $aNew[$n] Next $aLine = $aNew Next Return $ret EndFunc ;==>_Generate MDiesel Edit: One line solution to Get row sum!! StringReplace ($sLine, ".", "+")!! why din't I think of that before... Edited July 13, 2009 by mdiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 12, 2009 Moderators Share Posted July 12, 2009 MDiesel, How about this? #include <GUIConstantsEx.au3> #include <EditConstants.au3> #Include <GuiEdit.au3> $hGUI = GUICreate("Test", 500, 500) $hEdit = GUICtrlCreateEdit("", 10, 10, 480, 480, $ES_CENTER) _GUICtrlEdit_AppendText($hEdit, @CRLF & "1") _GUICtrlEdit_AppendText($hEdit, @CRLF & "1.1") _GUICtrlEdit_AppendText($hEdit, @CRLF & "1.2.1") _GUICtrlEdit_AppendText($hEdit, @CRLF & "1.3.3.1") GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd M23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
Mat Posted July 12, 2009 Author Share Posted July 12, 2009 Forgot I could center text!! And their I was waiting for a complex solution, and you just center it... Full script wil be in example scripts soon. Thanks melba MDiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Mat Posted July 13, 2009 Author Share Posted July 13, 2009 I knew there was a reason I hadn't used $ES_CENTER.... You can't use it with a horizontal scrollbar. Any solution to that? MDiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 13, 2009 Moderators Share Posted July 13, 2009 (edited) MDiesel,This will work for monospace fonts:expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #Include <GuiEdit.au3> Global $aLines[5] Generate() Align() $hGUI = GUICreate("Test", 500, 500) $hEdit = GUICtrlCreateEdit("", 10, 10, 80, 480, $WS_HSCROLL) GUICtrlSetFont(-1, Default, Default, Default, "Courier New") For $i = 1 To 4 _GUICtrlEdit_AppendText($hEdit, @CRLF & $aLines[$i]) Next GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func Generate() ; Simulate generating lines $aLines[1] = "1" $aLines[2] = "1.1" $aLines[3] = "1.2.1" $aLines[4] = "1.3.3.1.1.3.3.1.1.3.3.1" EndFunc Func Align() Local $iHalf_Max_Line = Ceiling(StringLen($aLines[4]) / 2) For $i = 1 To 3 Local $sText = $aLines[$i] Local $iHalf_Line = Ceiling(StringLen($sText) / 2) Local $iAdd_Spaces = $iHalf_Max_Line - $iHalf_Line For $j = 1 To $iAdd_Spaces $sText = " " & $sText Next $aLines[$i] = $sText Next EndFuncI am working on something similar for proportional fonts... ...but do not hold your breath!M23Edit: And this will work for proportional fonts: expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #Include <GuiEdit.au3> #include <StringSize.au3> Global $aLines[5] Generate() Align() $hGUI = GUICreate("Test", 500, 500) $hEdit = GUICtrlCreateEdit("", 10, 10, 80, 480, $WS_HSCROLL) For $i = 1 To 4 _GUICtrlEdit_AppendText($hEdit, @CRLF & $aLines[$i]) Next GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func Generate() ; Simulate generating lines $aLines[1] = "1" $aLines[2] = "1.1" $aLines[3] = "1.2.1" $aLines[4] = "1.3.3.1.1.3.3.1.1.3.3.1" EndFunc Func Align() Local $aInfo = _StringSize($aLines[4]) Local $iHalf_Max_Line = $aInfo[2] / 2 For $i = 1 To 3 Local $sText = $aLines[$i] Local $aInfo = _StringSize($sText) Local $iHalf_Line = $aInfo[2] / 2 Local $sAdd = "" Do $sAdd &= " " $aInfo = _StringSize($sAdd) Until $iHalf_Line + $aInfo[2] >= $iHalf_Max_Line $aLines[$i] = $sAdd & $sText Next EndFuncBut you need my string sizing UDF as an include:expandcollapse popup#include-once #include <WindowsConstants.au3> #include <SendMessage.au3> #include <WinAPI.au3> ; #FUNCTION# ======================================================================================= ; ; Name...........: _StringSize ; Description ...: Returns size of rectangle required to display string - width can be chosen ; Syntax ........: _StringSize($sText[, $iSize[, $iWeight[, $iAttrib[, $sName[, $iWidth]]]]]) ; Parameters ....: $sText - String to display ; $iSize - Font size in points - default AutoIt GUI default ; $iWeight - Font weight (400 = normal) - default AutoIt GUI default ; $iAttrib - Font attribute (0-Normal, 2-Italic, 4-Underline, 8 Strike - default AutoIt ; $sName - Font name - default AutoIt GUI default ; $iWidth - [optional] Width of rectangle - default is unwrapped width of string ; Requirement(s) : v3.2.12.1 or higher ; Return values .: Success - Returns array with details of rectangle required for text: ; |$array[0] = String formatted with @CRLF at required wrap points ; |$array[1] = Height of single line in selected font ; |$array[2] = Width of rectangle required to hold formatted string ; |$array[3] = Height of rectangle required to hold formatted string ; Failure - Returns 0 and sets @error: ; |1 - Incorrect parameter type (@extended = parameter index) ; |2 - Failure to create GUI to test label size ; |3 - Failure of _WinAPI_SelectObject ; |4 - Font too large for chosen width - longest word will not fit ; Author ........: Melba23 ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ;=================================================================================================== Func _StringSize($sText, $iSize = Default, $iWeight = Default, $iAttrib = Default, $sName = Default, $iWidth = 0) Local $hWnd, $hFont, $hDC, $oFont, $tSize, $hGUI, $hText_Label, $sTest_Line Local $iLine_Count, $iLine_Width, $iWrap_Count, $iLast_Word Local $asLines[1], $avSize_Info[4], $aiPos[4] ; Check parameters are correct type If Not IsString($sText) Then Return SetError(1, 1, 0) If Not IsNumber($iSize) And $iSize <> Default Then Return SetError(1, 2, 0) If Not IsInt($iWeight) And $iWeight <> Default Then Return SetError(1, 3, 0) If Not IsInt($iAttrib) And $iAttrib <> Default Then Return SetError(1, 4, 0) If Not IsString($sName) And $sName <> Default Then Return SetError(1, 5, 0) If Not IsNumber($iWidth) Then Return SetError(1, 6, 0) ; Create GUI to contain test labels, set to passed font parameters $hGUI = GUICreate("", 1200, 500, 10, 10) If $hGUI = 0 Then Return SetError(2, 0, 0) GUISetFont($iSize, $iWeight, $iAttrib, $sName) ; Store unwrapped text $avSize_Info[0] = $sText ; Ensure EoL is @CRLF and break text into lines If StringInStr($sText, @CRLF) = 0 Then StringRegExpReplace($sText, "[x0a|x0d]", @CRLF) $asLines = StringSplit($sText, @CRLF, 1) ; Draw label with unwrapped lines to check on max width $hText_Label = GUICtrlCreateLabel($sText, 10, 10) $aiPos = ControlGetPos($hGUI, "", $hText_Label) GUICtrlDelete($hText_Label) ; Store line height for this font size after removing label padding (always 8) $avSize_Info[1] = ($aiPos[3] - 8)/ $asLines[0] ; Store width and height of this label $avSize_Info[2] = $aiPos[2] $avSize_Info[3] = $aiPos[3] - 4 ; Reduce margin ; Check if wrapping is required If $aiPos[2] > $iWidth And $iWidth > 0 Then ; Set returned text element to null $avSize_Info[0] = "" ; Set width element to max allowed $avSize_Info[2] = $iWidth ; Set line count to zero $iLine_Count = 0 ; Take each line in turn For $j = 1 To $asLines[0] ; Size this line unwrapped $hText_Label = GUICtrlCreateLabel($asLines[$j], 10, 10) $aiPos = ControlGetPos($hGUI, "", $hText_Label) GUICtrlDelete($hText_Label) ; Check wrap status If $aiPos[2] < $iWidth Then ; No wrap needed so count line and store $iLine_Count += 1 $avSize_Info[0] &= $asLines[$j] & @CRLF Else ; Wrap needed so need to count wrapped lines ; Create label to hold line as it grows $hText_Label = GUICtrlCreateLabel("", 0, 0) ; Initialise Point32 method $hWnd = ControlGetHandle($hGui, "", $hText_Label) $hFont = _SendMessage($hWnd, $WM_GETFONT) $hDC = _WinAPI_GetDC($hWnd) $oFont = _WinAPI_SelectObject($hDC, $hFont) If $oFont = 0 Then Return SetError(3, 0, 0) ; Zero counter $iWrap_Count = 0 While 1 ; Set line width to 0 $iLine_Width = 0 ; Initialise pointer for end of word $iLast_Word = 0 For $i = 1 To StringLen($asLines[$j]) ; Is this just past a word ending? If StringMid($asLines[$j], $i, 1) = " " Then $iLast_Word = $i - 1 ; Increase line by one character $sTest_Line = StringMid($asLines[$j], 1, $i) ; Place line in label GUICtrlSetData($hText_Label, $sTest_Line) ; Get line length $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sTest_Line) $iLine_Width = DllStructGetData($tSize, "X") ; If too long exit the loop If $iLine_Width >= $iWidth - Int($iSize / 2) Then ExitLoop Next ; End of the line of text? If $i > StringLen($asLines[$j]) Then ; Yes, so add final line to count $iWrap_Count += 1 ; Store line $avSize_Info[0] &= $sTest_Line & @CRLF ExitLoop Else ; No, but add line just completed to count $iWrap_Count += 1 ; Check at least 1 word completed or return error If $iLast_Word = 0 Then GUIDelete($hGUI) Return SetError(4, 0, 0) EndIf ; Store line up to end of last word $avSize_Info[0] &= StringLeft($sTest_Line, $iLast_Word) & @CRLF ; Strip string to point reached $asLines[$j] = StringTrimLeft($asLines[$j], $iLast_Word) ; Trim leading whitespace $asLines[$j] = StringStripWS($asLines[$j], 1) ; Repeat with remaining characters in line EndIf WEnd ; Add the number of wrapped lines to the count $iLine_Count += $iWrap_Count ; Clean up _WinAPI_ReleaseDC($hWnd, $hDC) GUICtrlDelete($hText_Label) EndIf Next ; Convert lines to pixels and add reduced margin $avSize_Info[3] = ($iLine_Count * $avSize_Info[1]) + 4 EndIf ; Clean up GUIDelete($hGUI) ; Return array Return $avSize_Info EndFunc ; => _StringSize Edited July 13, 2009 by Melba23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
Yoriz Posted July 13, 2009 Share Posted July 13, 2009 expandcollapse popup; Pascals triangle #include <Array.au3> #Include <String.au3> $hGUI = GUICreate ("Test Pascals Trinagle", 500, 500, -1, -1, 0x00040000 + 0x00010000 + 0x00020000 + 0x00080000 + 0x00C00000) $hGen = GUICtrlCreateButton ("Generate", 2, 2, 80, 20) GUICtrlSetResizing (-1, 544) $hGet = GUICtrlCreateButton ("Get Row sum", 84, 2, 80, 20) GUICtrlSetResizing (-1, 544) $hEdit = GUICtrlCreateEdit ("Matts Pascals triangle generator", 2, 24, 496, 476) GUICtrlSetResizing (-1, 102) GUISetState () While 1 Switch GUIGetMsg () Case -3 Exit Case $hGen $inp = Inputbox ("Amount to calculate", "Enter the number of rows to calculate.", 50) ;~ If @Error Then Return 0 GUICtrlSetData ($hEdit, _Generate ($inp)) GUICtrlSetFont(-1, -1, -1, -1, "Courier New") Case $hGet $inp = Inputbox ("Amount to calculate", "Enter the row to get the sum of.", 50) ;~ If @Error Then Return 0 MsgBox (0, "Result", "Sum of row " & $inp & " is: " & Execute (StringReplace (StringRegExpReplace (_Generate ($inp), ".*\n", ""), ".", "+"))) EndSwitch WEnd Func _Generate ($nNum) Local $aLine[4] = [0, 1, 1, 0], $ret = "1" & @CRLF & "1.1" For $i = 3 to $nNum Local $aNew[$i + 2] $aNew[0] = 0 $aNew[1] = 1 For $n = 2 to $i - 1 $aNew[$n] = $aLine[$n - 1] + $aLine[$n] Next $aNew[$i] = 1 $aNew[$i + 1] = 0 $ret &= @CRLF & "1" For $n = 2 to $i $ret &= "." & $aNew[$n] Next $aLine = $aNew Next $sRetTemp = StringReplace($ret,@CRLF,"^") $aRet = stringsplit($sRetTemp,"^") $iLastItemLen = StringLen($aRet[$aRet[0]]) For $i = $aRet[0] To 1 Step -1 $iHalfLineLen = Round(($iLastItemLen - StringLen($aRet[$i]))/2) $aRet[$i] = _StringRepeat(" ",$iHalfLineLen) & $aRet[$i] & _StringRepeat(" ",$iHalfLineLen) Next $ret = _ArrayToString($aRet,@CRLF) Return $ret EndFunc ;==>_Generate GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now