Jump to content
Sign in to follow this  
trancexx

StringSize

Recommended Posts

trancexx

Subtitle doesn't make sense, don't try to find one. :idea:

Seems everyone has a version of this function.

Here's another one. This one is trick free and it's mine, mine, mine...

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include <WinAPI.au3>

; Make GUI
GUICreate("StringSize", 600, 400)
GUISetBkColor(0) ; black magic GUI

; First example:
Global $sLabelText = "Pa Da Pam Pam..."
Global $aLabelTextSize = _StringSize($sLabelText, 14)
GUICtrlCreateLabel($sLabelText, 10, 20, $aLabelTextSize[0], $aLabelTextSize[1]) ; create label in calculated size
GUICtrlSetFont(-1, 14) ; set wanted font
GUICtrlSetBkColor(-1, 0xFFFFFF) ; make the background white

; Print size
ConsoleWrite($aLabelTextSize[0] & " x " & $aLabelTextSize[1] & @CRLF)

; Secand example:
$sLabelText = "abcdehmma"
$aLabelTextSize = _StringSize($sLabelText, 60, 800)
GUICtrlCreateLabel($sLabelText, 10, 80, $aLabelTextSize[0], $aLabelTextSize[1]) ; create label in calculated size
GUICtrlSetFont(-1, 60, 800) ; set wanted font
GUICtrlSetBkColor(-1, 0xFFFFFF) ; make the background white

; Print size
ConsoleWrite($aLabelTextSize[0] & " x " & $aLabelTextSize[1] & @CRLF)

; Third example:
$sLabelText = "First line of the text" & @LF & "Second line of the text" & @CRLF & "Third line of the text" & @CRLF & "Fourth line of the text. See ya!"
$aLabelTextSize = _StringSize($sLabelText, 17, 700, 2, "Tahoma", 4)
GUICtrlCreateLabel($sLabelText, 10, 220, $aLabelTextSize[0], $aLabelTextSize[1]) ; create label in calculated size
GUICtrlSetFont(-1, 17, 700, 2, "Tahoma", 4) ; set wanted font
GUICtrlSetBkColor(-1, 0xFFFFFF) ; make the background white

; Print size
ConsoleWrite($aLabelTextSize[0] & " x " & $aLabelTextSize[1] & @CRLF)

; Show GUI
GUISetState()
; Wait for the exit
While GUIGetMsg() <> -3
WEnd


; FUNCTION
Func _StringSize($sText, $iSize = 8.5, $iWeight = 400, $iAttrib = 0, $sName = "", $iQuality = 2)
    Local Const $LOGPIXELSY = 90
    Local $fItalic = BitAND($iAttrib, 2)
    Local $hDC = _WinAPI_GetDC(0)
    Local $hFont = _WinAPI_CreateFont(-_WinAPI_GetDeviceCaps($hDC, $LOGPIXELSY) * $iSize / 72, 0, 0, 0, $iWeight, $fItalic, BitAND($iAttrib, 4), BitAND($iAttrib, 8), 0, 0, 0, $iQuality, 0, $sName)
    Local $hOldFont = _WinAPI_SelectObject($hDC, $hFont)
    Local $tSIZE
    Local $iWidth = 0, $iHeight = 0
    Local $aArrayOfStrings = StringSplit(StringStripCR($sText), @LF, 2)
    For $sString In $aArrayOfStrings
        If $fItalic Then $sString &= " "
        $tSIZE = _WinAPI_GetTextExtentPoint32($hDC, $sString)
        If DllStructGetData($tSIZE, "X") > $iWidth Then $iWidth = DllStructGetData($tSIZE, "X")
        $iHeight += DllStructGetData($tSIZE, "Y")
    Next
    _WinAPI_SelectObject($hDC, $hOldFont)
    _WinAPI_DeleteObject($hFont)
    _WinAPI_ReleaseDC(0, $hDC)
    Local $aOut[2] = [$iWidth, $iHeight]
    Return $aOut
EndFunc   ;==>_StringSize

You calculate the size of the string considering the font and make the label in that size. That's it.

edit: edit

Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
lsakizada

Works perfect! great work.

Very usefull not only for labels.


Be Green Now or Never (BGNN)!

Share this post


Link to post
Share on other sites
Yashied
trancexx

When you manage to find the cause of that don't forget to post the solution.

I'm clueless.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
KaFu

Just from the looks it seems like the font on the screenshot is painted with quality = 5, a little blurry... Yashied, maybe adjust the quality setting in _StringSize() and retry. trancexx, maybe this is a system specific setting? Saw a function floating around to aquire the system's font default setting, should this be taken into account?

Edit:

I would recommand following steps to make it OS/Font independant:

- Create Control

- Set Font

- Set Control Size with _StringSize(), where _StringSize() should read the actual font settings used by the control.

Edited by KaFu

Share this post


Link to post
Share on other sites
trancexx

Just from the looks it seems like the font on the screenshot is painted with quality = 5, a little blurry... Yashied, maybe adjust the quality setting in _StringSize() and retry. trancexx, maybe this is a system specific setting? Saw a function floating around to aquire the system's font default setting, should this be taken into account?

Edit:

I would recommand following steps to make it OS/Font independant:

- Create Control

- Set Font

- Set Control Size with _StringSize(), where _StringSize() should read the actual font settings used by the control.

Yes, but that's defeating the main purpose of why I posted this.

Anyway, _WinAPI_CreateFont() function seems to be the cause of problems. It has $szFace = 'Arial' as the last parameter. AutoIt on the other hand is using empty string as default.

Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Yashied

Share this post


Link to post
Share on other sites
slayerz

Great example...thanks trancexx :idea:


AUTOIT[sup] I'm lovin' it![/sup]

Share this post


Link to post
Share on other sites
Mat

If Melba is reading this, this is exactly what I was hinting at. No GUI's, just api.

Well done :idea:

Share this post


Link to post
Share on other sites
Melba23

Of course Melba23 is reading this! :(

This shows the gulf between a hobbyist coder like me and a real programmer - vast! :)

Nice example trancexx. :idea:

M23

Edit: There is one difference though, my version will wrap the text for you to fit an designated width - you do not have to preformat with @CRLFs, the UDF does it for you.

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Mat

Of course Melba23 is reading this! :)

This shows the gulf between a hobbyist coder like me and a real programmer - vast! :)

Nice example trancexx. :idea:

M23

Edit: There is one difference though, my version will wrap the text for you to fit an designated width - you do not have to preformat with @CRLFs, the UDF does it for you.

I'm fairly sure that with a little editing trancexx will be able to figure it out :)

There is one more difference though... I can understand what goes on in your code :(

Share this post


Link to post
Share on other sites
Melba23

Mat,

There is one more difference though... I can understand what goes on in your code

Too true! :idea:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
trancexx

I'm not a real programmer Melba23.

@Yashied, could you try replacing _WinAPI_GetDC(0) with

_WinAPI_GetDC(_WinAPI_GetDesktopWindow())

And if that doesn't work then with the gui handle.

Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Melba23

trancexx,

I'm not a real programmer Melba23

You could have fooled me! :)

Would you accept "very talented coder"? :idea:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
trancexx

trancexx,

You could have fooled me! :(

Would you accept "very talented coder"? :)

M23

I would.

You have to see my other talents...

Sometimes I replace barcode stamps on the fruits in the grocery, steal a snickers even. I can say I'm very talented in different areas.

Tell no one about that. It's kinda of a secret. :idea:


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Melba23

trancexx,

Sometimes I replace barcode stamps on the fruits in the grocery

You devil, you! :idea:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
trancexx

Have you ever done that?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
TheSaint
@trancexx - If you are not indulging in a bit of leg pull, then you are obviously a gal after my own heart. However, I would suggest you give up the habit, and indulge in other things that are still exciting ... but only embarrassing when you eventually get caught or found out. Take it from one that was lucky enough to learn in time! :idea:

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×