Der_Andi Posted August 16, 2010 Share Posted August 16, 2010 (edited) Ok, now I found really something weird. A very long line gets calculated too short, despite the correct use of the style parameter. Here's a short example: expandcollapse popup#NoTrayIcon Opt("GuiOnEventMode", 1) #include <GuiConstants.au3> #include <StaticConstants.au3> #include <GDIPlus.au3> $fontName = "Tahoma" $fontSize = 9 $guiMain = GUICreate("", 500, 200) GUISetOnEvent($gui_event_close, "quit") GUISetFont($fontSize, 400, 0, $fontName, $guiMain) $text = "THIS IS LOOOOOOOOOOOOOOOOOOOONG TEXT" $len = getTextLengthInPixel($text, "Tahoma", 10, 2) ; bold GUICtrlCreateLabel($text, 20, 20, $len, 20, $ss_leftnowordwrap) GUICtrlSetFont(-1, 10, 600) GUICtrlCreateLabel("Following text", 20 + $len, 20+1, 100, 20) GUISetState() While True Sleep(1000) WEnd Func getTextLengthInPixel($text, $font, $size = 9, $style = 0) ;Calculate actual width of the given text _GDIPlus_Startup() $hGraphic = _GDIPlus_GraphicsCreateFromHWND($guiMain) $hFamily = _GDIPlus_FontFamilyCreate($font) $hGDIFont = _GDIPlus_FontCreate($hFamily, $size, $style) $hFormat = _GDIPlus_StringFormatCreate() $tLayout = _GDIPlus_RectFCreate(0, 0, @DesktopWidth, @DesktopHeight) $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hGDIFont, $tLayout, $hFormat) $width = Int(DllStructGetData($aInfo[0], 3)) + 1 print($width) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_FontDispose($hGDIFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_Shutdown() Return $width EndFunc Func print($s) ConsoleWrite($s & @CRLF) EndFunc Func quit() Exit EndFunc For the moment, this workaround while getting the width from the structure will suffice for my thing: $width = Int(DllStructGetData($aInfo[0], 3)) * 1.05 + 10 ;instead of +1 Maybe it's an issue after all and the dev's wanna have a look into it... Edited August 16, 2010 by Der_Andi Link to comment Share on other sites More sharing options...
VAN0 Posted May 1, 2022 Share Posted May 1, 2022 On 8/16/2010 at 9:35 AM, Der_Andi said: Ok, now I found really something weird. A very long line gets calculated too short, despite the correct use of the style parameter. Here's a short example: expandcollapse popup#NoTrayIcon Opt("GuiOnEventMode", 1) #include <GuiConstants.au3> #include <StaticConstants.au3> #include <GDIPlus.au3> $fontName = "Tahoma" $fontSize = 9 $guiMain = GUICreate("", 500, 200) GUISetOnEvent($gui_event_close, "quit") GUISetFont($fontSize, 400, 0, $fontName, $guiMain) $text = "THIS IS LOOOOOOOOOOOOOOOOOOOONG TEXT" $len = getTextLengthInPixel($text, "Tahoma", 10, 2) ; bold GUICtrlCreateLabel($text, 20, 20, $len, 20, $ss_leftnowordwrap) GUICtrlSetFont(-1, 10, 600) GUICtrlCreateLabel("Following text", 20 + $len, 20+1, 100, 20) GUISetState() While True Sleep(1000) WEnd Func getTextLengthInPixel($text, $font, $size = 9, $style = 0) ;Calculate actual width of the given text _GDIPlus_Startup() $hGraphic = _GDIPlus_GraphicsCreateFromHWND($guiMain) $hFamily = _GDIPlus_FontFamilyCreate($font) $hGDIFont = _GDIPlus_FontCreate($hFamily, $size, $style) $hFormat = _GDIPlus_StringFormatCreate() $tLayout = _GDIPlus_RectFCreate(0, 0, @DesktopWidth, @DesktopHeight) $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hGDIFont, $tLayout, $hFormat) $width = Int(DllStructGetData($aInfo[0], 3)) + 1 print($width) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_FontDispose($hGDIFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_Shutdown() Return $width EndFunc Func print($s) ConsoleWrite($s & @CRLF) EndFunc Func quit() Exit EndFunc For the moment, this workaround while getting the width from the structure will suffice for my thing: $width = Int(DllStructGetData($aInfo[0], 3)) * 1.05 + 10 ;instead of +1 Maybe it's an issue after all and the dev's wanna have a look into it... Any solution to this problem? I have a floating window that being resized based on text, increasing width with static number does not work in my case... Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 13, 2022 Moderators Share Posted May 13, 2022 VAN0, Have you tried my StringSize UDF - the link is in my sig. 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...
VAN0 Posted May 14, 2022 Share Posted May 14, 2022 (edited) 6 hours ago, Melba23 said: VAN0, Have you tried my StringSize UDF - the link is in my sig. M23 Thanks, just tried it, it has the same issue P.S. the decision to have height before width in return array is odd Edited May 14, 2022 by VAN0 Link to comment Share on other sites More sharing options...
pixelsearch Posted May 14, 2022 Share Posted May 14, 2022 Something seems incorrect in this script, but even by modifying one line, the result won't be accurate : ; $len = getTextLengthInPixel($text, "Tahoma", 10, 2) ; bold (?) (no, 2 means italic) $len = getTextLengthInPixel($text, "Tahoma", 10, 1) ; bold <========================= The display will change from : to For what it's worth... Link to comment Share on other sites More sharing options...
pixelsearch Posted May 15, 2022 Share Posted May 15, 2022 On 5/1/2022 at 8:19 AM, VAN0 said: I have a floating window that being resized based on text, increasing width with static number does not work in my case... Why not showing your code ? I'm sure you'll get help if you do so, because Melba23's StringSize function calculates accurately the label width (I tried it just now) : #include <GUIConstantsEx.au3> #include "StringSize.au3" ; Melba23's $hGUI = GUICreate("StringSize Test", 450, 80) GUISetFont(9, 400, 0, "Arial", $hGUI) ; A label sized by StringSize $sText = "THIS IS LOOOOOOOOOOOOOOOOOOOONG TEXT" $aSize = _StringSize($sText, 10, 600, 0, "Tahoma") GUICtrlCreateLabel($aSize[0], 20, 20, $aSize[2], $aSize[3]) GUICtrlSetFont(-1, 10, 600, 0, "Tahoma") GUICtrlSetBkColor(-1, 0x80FF80) GUICtrlCreateLabel("Another line not formatted in any way", 20, 50) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Writing exactly 4 same arguments in _StringSize(...) and GUICtrlSetFont(-1, ...) gave a very accurate result as shown in the preceding pic. Now if your (bold) text had to change within the label, it should be possible to recalculate the label's width (using the StringSize function) and re-display the label accordingly inside the (resized) floating window. Your move 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