Jump to content

is possible centralize text in a row that I created?


Recommended Posts

hi people, fine?

i need a litte help!

let's assume I have two strings that form a rectangle!

Example:

_______________________
_______________________

and i need to put how many spaces before one word, to centralize this!

example:

_______________________
   this is a example
_______________________

i tried using the number of characters, but it doesn't work, because some letters take up less space than other

example, the letter "i" takes up less space than the letter "a"

is it possible?

thanks a lot!

Edited by golfinhu
Link to comment
Share on other sites

  • Moderators

golfinhu,

Look at the StringSize UDF in my sig - it tells you exactly how many pixels you need to write the text. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

ok, I'll look at your udf and try again ;D

thanks a lot Melba!

Edit:

you can help me?

i tried this:

i've tested the space " "

and it's return 11 in width

and i tried it, only for test!

#include <StringSize.au3>
#include <Array.au3>
#include <String.au3>

local $sLabelMsg  = "Test"
Local $size = _StringSize($sLabelMsg, Default, Default, Default, "Arial") ; get the size of the string
Local $line =  _StringSize("______________________", Default, Default, Default, "Arial") ; get the size of the string
Local $faltam = $line[2] - $size[2] ; getting the diference
Local $spaces = Round(($faltam / 11) / 2) ; getting how many spaces i need put
$text = _StringRepeat(" ", $spaces) & $sLabelMsg ; add the spaces
ClipPut($text) ; putting it on clipboard

but he is far left!

My logic is terrible today :s

you can look my code please? thanks a lot and sorry for inconvenience

Edited by golfinhu
Link to comment
Share on other sites

Try changing this line to what I have below:

Local $spaces = Round(($faltam /7) ) ; getting how many spaces i need put

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Here my version:

#include <GDIPlus.au3>
#include <Array.au3>
#include <String.au3>

Local $sLabelMsg  = "This is a test"
Local $sLabel = "_______________________"
$aSize1 = GetStringSize($sLabelMsg, "Arial", 8.5, 0)
$w1 = $aSize1[0]
$aSize2 = GetStringSize($sLabel, "Arial", 8.5, 0)
$w2 = $aSize2[0]
$aSize3 = GetStringSize(" ", "Arial", 8.5, 0)
$w3 = $aSize3[0]
$spaces = Floor((($w2 - $w1) / 2) / $w3)

$text = _StringRepeat(" ", $spaces) & $sLabelMsg ; add the spaces
ConsoleWrite($sLabel & @CRLF)
ConsoleWrite($text & @CRLF)
ConsoleWrite($sLabel & @CRLF)

Func GetStringSize($string, $font, $fontsize, $fontstyle) ;coded by UEZ 2011 Beta
    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 $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
    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_GraphicsDrawStringEx($hGrphContext, $string, $hFont, $aInfo[0], $hFormat, $hBrush)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
;~  _GDIPlus_BrushDispose($hBrush)
    _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

Not fully tested!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Here my version:

#include <GDIPlus.au3>
#include <Array.au3>
#include <String.au3>

Local $sLabelMsg  = "This is a test"
Local $sLabel = "_______________________"
$aSize1 = GetStringSize($sLabelMsg, "Arial", 8.5, 0)
$w1 = $aSize1[0]
$aSize2 = GetStringSize($sLabel, "Arial", 8.5, 0)
$w2 = $aSize2[0]
$aSize3 = GetStringSize(" ", "Arial", 8.5, 0)
$w3 = $aSize3[0]
$spaces = Floor((($w2 - $w1) / 2) / $w3)

$text = _StringRepeat(" ", $spaces) & $sLabelMsg ; add the spaces
ConsoleWrite($sLabel & @CRLF)
ConsoleWrite($text & @CRLF)
ConsoleWrite($sLabel & @CRLF)

Func GetStringSize($string, $font, $fontsize, $fontstyle) ;coded by UEZ 2011 Beta
    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 $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
    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_GraphicsDrawStringEx($hGrphContext, $string, $hFont, $aInfo[0], $hFormat, $hBrush)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
;~  _GDIPlus_BrushDispose($hBrush)
    _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

Not fully tested!

Br,

UEZ

i will test it!

thanks a lot for your code!

Link to comment
Share on other sites

  • Moderators

golfinhu,

Just use the UDF - then you need not worry about the width of the space character. That is why I wrote it after all! :)

#include <GUIConstantsEx.au3>
#include <StringSize.au3>
#include <Array.au3>
#include <String.au3>

local $sLabelMsg  = "Test"
Local $size = _StringSize($sLabelMsg, Default, Default, Default, "Arial") ; get the size of the string
Local $line =  _StringSize("______________________", Default, Default, Default, "Arial") ; get the size of the string

Local $faltam = ($line[2] - $size[2]) / 2 ; get the half the difference

For $i = 1 To 99
    ; Add a space
    $sLabelMsg = " " & $sLabelMsg
    $size = _StringSize($sLabelMsg, Default, Default, Default, "Arial") ; get the size of the string
    ; Loop until the difference is less than half the previous difference
    If $line[2] - $size[2] < $faltam Then ExitLoop

Next

; Display the result
$hGUI = GUICreate("Test", 500, 500)

GUICtrlCreateLabel("______________________" & @CRLF & $sLabelMsg & @CRLF & "______________________", 0, 0, 500, 50)
GUICtrlSetFont(-1, Default, Default, Default, "Arial")

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...