Kip Posted August 28, 2009 Posted August 28, 2009 (edited) There is not much else to say when you've read the title and description. The function + example: expandcollapse popup$text = "Hello World! Whazzup"&@CRLF&"enter"&@CRLF&"enter"&@CRLF&"enter"&@CRLF&"enter"&@CRLF&"enter"&@CRLF&"enter"&@CRLF&"enter" $iSize = 8.5 $sFont = "Courier New" $hGui = GUICreate("dsd") $Label_1 = GUICtrlCreateLabel($text,10,10,500,500) GUICtrlSetFont (-1, $iSize, 400, 0, $sFont) $aSize = _StringSize($text, $sFont, $iSize) GUICtrlCreateLabel("",10,0,$aSize[0],9) ; Horizontal bar GUICtrlSetBkColor(-1,0x000000) GUICtrlCreateLabel("",0,10,9,$aSize[1]) ; Vertical bar GUICtrlSetBkColor(-1,0x000000) GUISetState() While 1 WEnd ; Created by Zedna? +Kip ; Returns an array: ; [0] = width (px) ; [1] = height (px) Func _StringSize($sText, $sFont="Ms Sans Serif", $iSize=8.5, $bItalic=False, $bUnderlined=False, $bStrike=False, $iWeight=400, $sLineBreak=@CRLF) Local $aReturn[2] Local $hGui = GUICreate("dsd") Local $iStyle = 0 $iStyle += $bItalic*2 $iStyle += $bUnderlined*4 $iStyle += $bStrike*8 Local $iLabel = GUICtrlCreateLabel("",10,10,500,500) GUICtrlSetFont (-1, $iSize, $iWeight, $iStyle, $sFont) Local $hControl = GUICtrlGetHandle($iLabel) Local $hDC = DLLCall("user32.dll","int","GetDC","hwnd",$hControl) $hDC = $hDC[0] Local $hFont = DllCall("user32.dll", "ptr", "SendMessage", "hwnd", $hControl, "int", 0x0031, "int", 0, "int", 0) $hFont = $hFont[0] Local $hOldFont = DllCall("GDI32.dll", "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $hFont) $hOldFont = $hOldFont[0] Local $struct_size = DllStructCreate("int;int") Local $sLineSplit = StringSplit($sText,$sLineBreak,1) Local $i For $i = 1 to $sLineSplit[0] DllCall("gdi32.dll", "int", "GetTextExtentPoint32", "int", $hDC, "str", $sLineSplit[$i], "long", StringLen($sLineSplit[$i]), "ptr", DllStructGetPtr($struct_size)) ;MsgBox(0,"sdsd",DllStructGetData($struct_size,1)) If DllStructGetData($struct_size,1) > $aReturn[0] Then $aReturn[0] = DllStructGetData($struct_size,1) $aReturn[1] += DllStructGetData($struct_size,2) Next DllCall("GDI32.dll", "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $hOldFont) DllCall("GDI32.dll", "int", "DeleteObject", "int", $hOldFont) DllCall("User32.dll", "int", "ReleaseDC", "hwnd", 0, "hwnd", $hDC) GUIDelete($hGui) Return $aReturn EndFunc Edited August 28, 2009 by Kip Professor_Bernd 1 MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API.
Alek Posted August 28, 2009 Posted August 28, 2009 any reason not to use _GDIPlus_GraphicsMeasureString? [font="Impact"]Never fear, I is here.[/font]
Kip Posted August 28, 2009 Author Posted August 28, 2009 (edited) Well, I didn't notice that function to be honest, but some reasons. - It doesn't require _GDIPlus_StartUp() - Easier to use - Takes less memory? If I look at the example I see that there are 5 things being created: $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd) $hFormat = _GDIPlus_StringFormatCreate () $hFamily = _GDIPlus_FontFamilyCreate ("Arial") $hFont = _GDIPlus_FontCreate ($hFamily, 12, 2) $tLayout = _GDIPlus_RectFCreate (140, 110, 0, 0) Compared to: Local $hGui = GUICreate("dsd") Local $iLabel = GUICtrlCreateLabel("",10,10,500,500) Edited August 28, 2009 by Kip MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API.
JScript Posted August 28, 2009 Posted August 28, 2009 On 8/28/2009 at 9:17 PM, 'Kip said: Well, I didn't notice that function to be honest, but some reasons. - It doesn't require _GDIPlus_StartUp() - Easier to use - Takes less memory? If I look at the example I see that there are 5 things being created: $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd) $hFormat = _GDIPlus_StringFormatCreate () $hFamily = _GDIPlus_FontFamilyCreate ("Arial") $hFont = _GDIPlus_FontCreate ($hFamily, 12, 2) $tLayout = _GDIPlus_RectFCreate (140, 110, 0, 0) Compared to: Local $hGui = GUICreate("dsd") Local $iLabel = GUICtrlCreateLabel("",10,10,500,500) I made this UDF a long time ago and it works perfectly!!! expandcollapse popup#include-once ; #INDEX# ======================================================================================================================= ; Title .........: _StringEx ; AutoIt Version.: 3.2.12++ ; Language.......: English ; Description ...: Add some String functions. ; Version .......: 1.08.2209.2600 ; Remarks .......: ; Credits .......: jscript ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_StringSize ; =============================================================================================================================== ; #INTERNAL_USE_ONLY#============================================================================================================ ;============================================================================================================================== ; #VARIABLES# =================================================================================================================== ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _StringSize ; Description ...: Returns the size (in pixels) of an string. ; Syntax.........: _StringSize( "string" [, size [, weight [, fontname ]]] ) ; Parameters ....: string - The string to evaluate the size. ; Size - [Optional] Fontsize (default is 9). ; Weight - [Optional] Font weight (default 400 = normal). ; FontName - [Optional] Font to use (OS default GUI font is used if the font is "" or is not found). ; Requirement(s).: ; Return values .: Success - Returns a 2-element array containing the following information: ; $array[0] = Width ; $array[1] = Height ; Failure - Returns the same array with 0 and sets @error to 1. ; Author ........: jscript ; Example .......: _StringSize( "Text" ) ; =============================================================================================================================== Func _StringSize($sString, $Isize = 9, $iWeight = 400, $sFontName = "") Local $hSTR_INTERNALWIN, $hGuiSwitch, $iCtrlID, $aCtrlGetPos, $aItemSize[2] = [0, 0] $hSTR_INTERNALWIN = GUICreate("***StringExInternalWin*** - " & Random(8, 1024), 0, 0, 0, 0, _ BitOR(0x80000000, 0x20000000), BitOR(0x00000080, 0x00000020)) $hGuiSwitch = GUISwitch($hSTR_INTERNALWIN) GUISetFont($Isize, $iWeight, -1, $sFontName, $hSTR_INTERNALWIN) $iCtrlID = GUICtrlCreateLabel($sString, 0, 0) $aCtrlGetPos = ControlGetPos($hSTR_INTERNALWIN, "", $iCtrlID) GUIDelete($hSTR_INTERNALWIN) GUISwitch($hGuiSwitch) If IsArray($aCtrlGetPos) Then $aItemSize[0] = $aCtrlGetPos[2]; Width $aItemSize[1] = $aCtrlGetPos[3]; Height Return SetError(0, 0, $aItemSize) EndIf Return SetError(1, 0, $aItemSize) EndFunc ;==>_GetStringSize http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!) Reveal hidden contents Somewhere Out ThereJames Ingram Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere!
Kip Posted August 29, 2009 Author Posted August 29, 2009 Quote any reason not to use _GDIPlus_GraphicsMeasureString?And my version can handle strings with enters. MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API.
Skrip Posted August 29, 2009 Posted August 29, 2009 On 8/29/2009 at 8:35 AM, 'Kip said: And my version can handle strings with enters.Thats actually a very useful feature. [left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]
badapple89 Posted September 26, 2013 Posted September 26, 2013 Been looking at a few of these kinds of UDFs but yours seems pretty simple and measures more accurately.
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