johnmcloud Posted July 17, 2012 Posted July 17, 2012 (edited) Hi guys, i have a math question: $Widht = 70 $Height = 30 $GUI = GUICreate("Test", $Widht, $Height, 10, 10) $Label = GUICtrlCreateLabel("00:00", -1, -1, $Widht, $Height) GUICtrlSetFont($LabelClock, 20, 600) With font size 20 and the label/gui 70 * 30 it fit perfect, but if i want to make the font size bigger i need to change everytime the GUI size and the label size. How i can make the GUI/Label variable connected to the size of the font? Example i know Font size = 40 $Widht = 70*2 $Height = 30*2 But if i make the size 17 or 54? Hope is clear what is my goal. Thanks Edited July 17, 2012 by johnmcloud
Moderators Melba23 Posted July 17, 2012 Moderators Posted July 17, 2012 johnmcloud,Look at the StringSize UDF in my sig - it tells you the dimensions of a string in any given font/size. 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
johnmcloud Posted July 17, 2012 Author Posted July 17, 2012 I don't know if stringsize is what i need...can you provide an example based on my example? ( Yes, i have see the example in the zip file of the UDF )
johnmcloud Posted July 17, 2012 Author Posted July 17, 2012 Yes Melba, understood ( double post? ) but i need an easier example, just fit the GUI based on the label font dimesion, too array in yours
Blue_Drache Posted July 17, 2012 Posted July 17, 2012 I facepalmed so hard. Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache
johnmcloud Posted July 17, 2012 Author Posted July 17, 2012 I facepalmed so hard.Be careful not to break your head with a facepalm, sorry if i'm not a super mega coder like you.If you don't want to help, please don't lose your time and don't post here.Thanks
PhoenixXL Posted July 17, 2012 Posted July 17, 2012 (edited) Hey johnmcloud, Be calm Using this code u can resize the label expandcollapse popup#include <GDIPlus.au3> #include <WindowsConstants.au3> #include <GuiConstantsEx.au3> #include <Constants.au3> $X = 300 $Y = 300 $ParenT = GUICreate("", 500, 200, -1, -1, $WS_POPUP) GUISetState() $sLabel=GUICtrlCreateLabel('Hey.....asdasdsadasdasdasdasd.....!!',10,10,70,15) GUICtrlSetBkColor(-1,0) GUICtrlSetColor(-1,0xFFFFFF) $sRect=CalcRectangle('Hey.....asdasdsadasdasdasdasd.....!!') ConsoleWrite('Width Should be: '&$sRect[0]&@CRLF _ & 'Height Should be: '&$sRect[1]&@CRLF) Sleep(3000) ConsoleWrite('-> Resizing Successful'&@CRLF) ControlMove($ParenT,'',$sLabel,Default,Default,$sRect[0],$sRect[1]) Sleep(7000) Func CalcRectangle($sString, $sFont='Arial',$fFontsize=8.5) _GDIPlus_Startup() Local $aResult, $hFont, $i=0 ,$iLen = StringLen($sString),$tLayout Local $hGfx = _GDIPlus_GraphicsCreateFromHWND(WinGetHandle(AutoItWinGetTitle())) Local $tRectF = DllStructCreate($tagGDIPRECTF) Local $hFormat = _GDIPlus_StringFormatCreate() Local $hFont = _GDIPlus_FontCreate( _GDIPlus_FontFamilyCreate($sFont), $fFontsize, 0) Do $tLayout = _GDIPlus_RectFCreate(0, 0, $i, 0) $aResult = DllCall($ghGDIPDll, "int", "GdipMeasureString", _ "handle", $hGfx, _ "wstr", $sString, _ "int", $iLen, _ "handle", $hFont, _ "struct*", $tLayout, _ "handle", $hFormat, _ "struct*", $tRectF, _ "int*", 0, _ "int*", 0) If $aResult[8] >= $iLen Then ExitLoop $i += 1 Until False _GDIPlus_FontDispose($hFont) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_GraphicsDispose($hGfx) $tLayout = 0 Local $sReturn[2]=[DllStructGetData($aResult[7],'Width'),DllStructGetData($aResult[7],'Height')] _GDIPlus_Shutdown() Return $sReturn EndFunc Hope so u can carry out the GUI resizing Regards Phoenix XL Edited July 17, 2012 by PhoenixXL My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.
johnmcloud Posted July 17, 2012 Author Posted July 17, 2012 (edited) I'm calm PhonenixXL, don't worry I'm here for learn, not to be fooled I have find another example like yours about the label size ( in GDI+ ), the problem is the GUI size: expandcollapse popup#include <GDIPlus.au3> #include <Misc.au3> $gui = GUICreate("test", 400, 200) $label = GUICtrlCreateLabel("old", 10, 10, 20, 15) GUISetState() updateTXT() While 1 Sleep(10) WEnd Func updateTXT() MsgBox(0, "", "please choose font and a larger font size") $font_attributes = _ChooseFont() If Not @error Then $aDim = GetStringSize("old", $font_attributes[2], $font_attributes[3], $font_attributes[1]) GUICtrlSetPos($label, 10, 10, $aDim[0], $aDim[1]) GUICtrlSetFont($label, $font_attributes[3], "", "", $font_attributes[2], 4) Sleep(2000) Exit EndIf EndFunc ;==>updateTXT Func GetStringSize($string, $font, $fontsize, $fontstyle) Local $GDIp = False Local $iWidth = StringLen($string) * $fontsize Local $iHeight = 2 * $fontsize If Not $ghGDIPDll Then _GDIPlus_Startup() $GDIp = True EndIf Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0) Local $hBitmap = $aResult[6] Local $hGrphContext = _GDIPlus_ImageGetGraphicsContext($hBitmap) Local $hFormat = _GDIPlus_StringFormatCreate() Local $hFamily = _GDIPlus_FontFamilyCreate($font) Local $hFont = _GDIPlus_FontCreate($hFamily, $fontsize, $fontstyle) Local $tLayout = _GDIPlus_RectFCreate(0, 0, 0, 0) Local $aInfo = _GDIPlus_GraphicsMeasureString($hGrphContext, $string, $hFont, $tLayout, $hFormat) _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_GraphicsDispose($hGrphContext) If $GDIp Then _GDIPlus_Shutdown() Local $aDim[2] = [Int(DllStructGetData($aInfo[0], "Width")), Int(DllStructGetData($aInfo[0], "Height"))] Return $aDim EndFunc I'll wait also for a Melba's solution, but if someone has a new idea about GUI resizing based on a label size i can check it out Thanks Edited July 17, 2012 by johnmcloud
PhoenixXL Posted July 17, 2012 Posted July 17, 2012 Well we can create an offset and then resize the GUI Example would be better hold a min.. My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.
Moderators Melba23 Posted July 17, 2012 Moderators Posted July 17, 2012 johnmcloud, too array in yoursArrays are an essential tool - you really need to understand them. Here is the simplest example I can think of: #include <StringSize.au3> While 1 ; Get a font size $iFont_Size = Int(InputBox("Font Size", "Enter a size")) If $iFont_Size Then ; Find the size of the string in the required font $aSize = _StringSize("Fits neatly", $iFont_Size, Default, Default, "Arial") ; And create the GUI to fit $hGUI = GUICreate("Test", $aSize[2], $aSize[3]) GUICtrlCreateLabel($aSize[0], 0, 0, $aSize[2], $aSize[3]) GUICtrlSetFont(-1, $iFont_Size) GUICtrlSetBkColor(-1, 0xFF0000) GUISetState() While 1 If GUIGetMsg() = -3 Then GUIDelete($hGUI) ExitLoop EndIf WEnd Else ExitLoop EndIf WEnd Does that make it easier to understand? 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
PhoenixXL Posted July 17, 2012 Posted July 17, 2012 Here we go expandcollapse popup#include #include #include #include $ParenT = GUICreate("", 500, 200) GUISetState() $sLabel=GUICtrlCreateLabel('Hey.....asdasdsadasdasdasdasd.....!!',10,10,70,15) GUICtrlSetResizing(-1,802) GUICtrlSetBkColor(-1,0) GUICtrlSetColor(-1,0xFFFFFF) $hPos=ControlGetPos($ParenT,'',$sLabel) $sRect=CalcRectangle('Hey.....asdasdsadasdasdasdasd.....!!') ConsoleWrite('Width Should be: '&$sRect[0]&@CRLF _ & 'Height Should be: '&$sRect[1]&@CRLF) Sleep(3000) ConsoleWrite('-> Resizing'&@CRLF) $sGUI=WinGetPos($ParenT) WinMove($ParenT,'',Default,Default,$sGUI[2]+($sRect[0]-$hPos[2]),$sGUI[3]+($sRect[1]-$hPos[3])) ControlMove($ParenT,'',$sLabel,Default,Default,$sRect[0],$sRect[1]) Sleep(7000) Func CalcRectangle($sString, $sFont='Arial',$fFontsize=8.5) _GDIPlus_Startup() Local $aResult, $hFont, $i=0 ,$iLen = StringLen($sString),$tLayout Local $hGfx = _GDIPlus_GraphicsCreateFromHWND(WinGetHandle(AutoItWinGetTitle())) Local $tRectF = DllStructCreate($tagGDIPRECTF) Local $hFormat = _GDIPlus_StringFormatCreate() Local $hFont = _GDIPlus_FontCreate( _GDIPlus_FontFamilyCreate($sFont), $fFontsize, 0) Do $tLayout = _GDIPlus_RectFCreate(0, 0, $i, 0) $aResult = DllCall($ghGDIPDll, "int", "GdipMeasureString", _ "handle", $hGfx, _ "wstr", $sString, _ "int", $iLen, _ "handle", $hFont, _ "struct*", $tLayout, _ "handle", $hFormat, _ "struct*", $tRectF, _ "int*", 0, _ "int*", 0) If $aResult[8] >= $iLen Then ExitLoop $i += 1 Until False _GDIPlus_FontDispose($hFont) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_GraphicsDispose($hGfx) $tLayout = 0 Local $sReturn[2]=[DllStructGetData($aResult[7],'Width'),DllStructGetData($aResult[7],'Height')] _GDIPlus_Shutdown() Return $sReturn EndFunc Note with the resize in label equal resize takes place in the GUI too Hope so this helps Regards Phoenix XL My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.
johnmcloud Posted July 17, 2012 Author Posted July 17, 2012 Ok, this is the UDF of Melba ( i have make more easier lol, i'm just kidding ) #include <StringSize.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> ; Get a font size $iFont_Size = Int(20) ; Size of font ; Find the size of the string in the required font $aSize = _StringSize("Fits neatly", $iFont_Size, Default, Default, "Arial") ; And create the GUI to fit $hGUI = GUICreate("Test", $aSize[2], $aSize[3]) GUICtrlCreateLabel($aSize[0], 0, 0, $aSize[2], $aSize[3]) GUICtrlSetFont(-1, $iFont_Size) GUICtrlSetBkColor(-1, 0xFF0000) GUISetState() While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd And this is PhoneixXl example, i have add WinMove so the GUI automatically fit: expandcollapse popup#include <GDIPlus.au3> #include <WindowsConstants.au3> #include <GuiConstantsEx.au3> #include <Constants.au3> $X = 300 $Y = 300 $ParenT = GUICreate("", 500, 200, 10, 10, $WS_POPUP) GUISetState() $sLabel = GUICtrlCreateLabel('Hey.....asdasdsadasdasdasdasd.....!!', -1, -1) GUICtrlSetBkColor(-1, 0) GUICtrlSetColor(-1, 0xFFFFFF) Sleep(3000) $sRect = CalcRectangle(GUICtrlRead($sLabel)) WinMove($ParenT,"", 10, 10, $sRect[0], $sRect[1]) ControlMove($ParenT, '', $sLabel, Default, Default, $sRect[0], $sRect[1]) Sleep(3000) Func CalcRectangle($sString, $sFont = 'Arial', $fFontsize = 8.5) _GDIPlus_Startup() Local $aResult, $hFont, $i = 0, $iLen = StringLen($sString), $tLayout Local $hGfx = _GDIPlus_GraphicsCreateFromHWND(WinGetHandle(AutoItWinGetTitle())) Local $tRectF = DllStructCreate($tagGDIPRECTF) Local $hFormat = _GDIPlus_StringFormatCreate() Local $hFont = _GDIPlus_FontCreate(_GDIPlus_FontFamilyCreate($sFont), $fFontsize, 0) Do $tLayout = _GDIPlus_RectFCreate(0, 0, $i, 0) $aResult = DllCall($ghGDIPDll, "int", "GdipMeasureString", _ "handle", $hGfx, _ "wstr", $sString, _ "int", $iLen, _ "handle", $hFont, _ "struct*", $tLayout, _ "handle", $hFormat, _ "struct*", $tRectF, _ "int*", 0, _ "int*", 0) If $aResult[8] >= $iLen Then ExitLoop $i += 1 Until False _GDIPlus_FontDispose($hFont) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_GraphicsDispose($hGfx) $tLayout = 0 Local $sReturn[2] = [DllStructGetData($aResult[7], 'Width'), DllStructGetData($aResult[7], 'Height')] _GDIPlus_Shutdown() Return $sReturn EndFunc ;==>CalcRectangle The Phoneix's script seems not cut perfect the label, don't know why. Melba's UDF works fine
PhoenixXL Posted July 17, 2012 Posted July 17, 2012 Yeah, Clearcut rectangle sometimes go 19, 20 Anyways Congo U got it working My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.
johnmcloud Posted July 17, 2012 Author Posted July 17, 2012 (edited) Mmm there is a problem with melba UDF, i have try to change the font but:In the example of the .zip work, why this time not? The font don't change and it doesn't fit Edited July 17, 2012 by johnmcloud
johnmcloud Posted July 17, 2012 Author Posted July 17, 2012 (edited) To change the font of what? Of the label _StringSize($sText, $iSize, $iWeight, $iAttrib, $sFont, $iX) I have make: $aSize = _StringSize("Fits neatly", $iFont_Size, Default, Default, "Courier New") But i see this: Something is wrong, work only with "Arial". I have tested the example in the zip file of the _StringSize and the font change, so i dunno what is wrong Edited July 17, 2012 by johnmcloud
trancexx Posted July 17, 2012 Posted July 17, 2012 (edited) Of the label _StringSize($sText, $iSize, $iWeight, $iAttrib, $sFont, $iX) I have make: $aSize = _StringSize("Fits neatly", $iFont_Size, Default, Default, "Courier New") But i see this: Something is wrong, work only with "Arial" You have problems with logic, not math. And lack basic understanding of programming. You did not change the font of the label. You calculated the size of string for one font and then used different font for label. That's what's wrong. Programming is simple, you just have to learn to learn and learn to think, nothing more. Good luck. Edited July 17, 2012 by trancexx ♡♡♡ . eMyvnE
johnmcloud Posted July 17, 2012 Author Posted July 17, 2012 (edited) I have understood the problem: #include <StringSize.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> ; Get a font name $iFont_Name = "Comic Sans MS" ; Get a font size $iFont_Size = Int(20) ; Size of font ; Find the size of the string in the required font $aSize = _StringSize("Fits neatly", $iFont_Size, Default, Default, $iFont_Name) ; And create the GUI to fit $hGUI = GUICreate("Test", $aSize[2], $aSize[3]) GUICtrlCreateLabel($aSize[0], 0, 0, $aSize[2], $aSize[3]) GUICtrlSetFont(-1, $iFont_Size, 400, 0, $iFont_Name) GUICtrlSetBkColor(-1, 0xFF0000) GUISetState() While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd @trancexx at the start i think was a math problem, an equivalation or a sort of, i know the gui is 70*30 and the size of the font is 20 and fit, how to calcucate the gui size if font size if is example 17 for fit it? Thanks to all, know i think i have solved Edited July 17, 2012 by johnmcloud
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