Jump to content

GDI+ align text


Recommended Posts

Hello,

I would like to know if it's possible to align text with GDI+

It work well with SplashTextOn()

let me show you some exemple:

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout
    Local $sString = "Hello"&Chr(09)&"world"&@CRLF&Chr(09)&"world", $aInfo

    ; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    $hWnd = WinGetHandle("GDI+")
    GUISetState()

    ; Draw a string
    _GDIPlus_Startup ()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)
    $hBrush = _GDIPlus_BrushCreateSolid (0xFF00007F)
    $hFormat = _GDIPlus_StringFormatCreate ()
    $hFamily = _GDIPlus_FontFamilyCreate ("Arial")
    $hFont = _GDIPlus_FontCreate ($hFamily, 12, 0)
    $tLayout = _GDIPlus_RectFCreate (140, 110, 0, 0)
    $aInfo = _GDIPlus_GraphicsMeasureString ($hGraphic, $sString, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx ($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Clean up resources
    _GDIPlus_FontDispose ($hFont)
    _GDIPlus_FontFamilyDispose ($hFamily)
    _GDIPlus_StringFormatDispose ($hFormat)
    _GDIPlus_BrushDispose ($hBrush)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown ()

EndFunc   ;==>_MainoÝ÷ Ù«­¢+ØMÁ±Í¡QáÑ=¸ ÅÕ½ÐíQ¥Ñ±ÅÕ½Ðì°ÅÕ½Ðí!±±¼ÅÕ½ÐìµÀí
¡È À䤵ÀìÅÕ½ÐíݽɱÅÕ½ÐìµÀí
I1µÀí
¡È À䤵ÀìÅÕ½ÐíݽɱÅÕ½Ðì°´Ä°´Ä°´Ä°´Ä°Ð°ÅÕ½ÐìÅÕ½Ðì°ÈФ)M±À ÄÀÀÀÀ¤)MÁ±Í¡=

The Chr(09) doesn't work with the _GDIPlus_GraphicsDrawStringEx but works well with the SplashTextOn()

I tried as well with the _GDIPlus_GraphicsDrawString => same result

I tried as well with with the Send("{tab}") => same result !!!

Thanks for your help !

Link to comment
Share on other sites

What happens when you use @TAB instead?

Good Idea, but it dosn't work for the GDI+ (works fine for the SplashTextOn())

Is there any others solutions ?

(maybe something else than 'tab' ?)

C.

Link to comment
Share on other sites

Space.... :D

This seems surprisingly difficult. I thought that perhaps you could use the return from _GDIPlus_GraphicsMeasureString to find out how much space a string took, then you could offset the position of the next string by that amount. It seemed like a good idea but it doesn't work. :D

Here's what I tried, maybe someone can see what I've done wrong, or why it doesn't work as I hoped.

(It doesn't even work with Courier New.)

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
Func Notused()
    Opt('MustDeclareVars', 1)
    SplashTextOn("Title", "Hello" & Chr(09) & "world" & @CRLF & Chr(09) & "world", -1, -1, -1, -1, 4, "", 24)
    Sleep(10000)
    SplashOff()
EndFunc ;==>Notused

_Main()

Func _Main()
    Local $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $res
    Local $tabby
    Local $sString = "Hello  world", $sString2 = "world", $aInfo
        Local $testStr = "Hello  ";try to use this to measure the offset
; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    $hWnd = WinGetHandle("GDI+")
    GUISetState()
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    $hBrush = _GDIPlus_BrushCreateSolid(0xFF00007F)
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    $hFont = _GDIPlus_FontCreate($hFamily, 12, 0)
    $tLayout = _GDIPlus_RectFCreate(140, 110, 0, 0)
    
;try to see how much space is used by "Hello     "
    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $testStr, $hFont, $tLayout, $hFormat)
    $res = $aInfo[0]
    If IsArray($res) Then ConsoleWrite("$res is an array. It has " & UBound($res) & " elements." & @CRLF)
    ConsoleWrite("at " & $aInfo[0] & ', ' & DllStructGetData($res, 1) & ', ' & DllStructGetData($res, 2) & ', ' & DllStructGetData($res, 3) & ', ' & DllStructGetData($res, 4) & @CRLF)
    
    $tabby = DllStructGetData($res,3);I hoped this would be the offset to print "world"
    
    $tLayout = _GDIPlus_RectFCreate(140, 110, 0, 0)
    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
    
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
    
    $tLayout = _GDIPlus_RectFCreate(140 + $tabby, 130, 0, 0)
    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString2, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString2, $hFont, $aInfo[0], $hFormat, $hBrush)
; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

; Clean up resources
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()

EndFunc ;==>_Main

But here is another way to get the same affect though I might have misunderstood what you're trying to do. It has the advantage that tabs work and there is no need to redraw the text to keep the text correct, after another window has been on top for example. Also, you could flash the colours or change the text much more easily.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("Form2", 413, 298, 303, 219)
$Edit1 = GUICtrlCreateEdit("", 136, 48, 185, 89, BitOR($ES_READONLY, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_WANTRETURN), 0)
GUICtrlSetData(-1, "Team B" & @TAB & "Team A" & @CRLF & "Rovers" & @TAB & "Jaguar" & @CRLF & "Prescott" & @TAB & "Highfield")
GUICtrlSetFont(-1, 16, 400, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
$lab = GUICtrlCreateLabel('', 1, 1, 1, 1)
GUISetState(@SW_SHOW)

$wp = WinGetPos($Form2)
$bkcol = PixelGetColor(10 + $wp[0], 10 + $wp[1])
GUICtrlSetBkColor($Edit1, $bkcol)
GUICtrlSetState($lab, $GUI_FOCUS)
#EndRegion ### START Koda GUI section ### Form=

$lastFocused = ControlGetFocus($Form2)
;ConsoleWrite($lastFocused & @CRLF)
While 1
    $CurrentFocus = ControlGetFocus($Form2)
    If $CurrentFocus = "Edit1" Then
        ControlFocus($Form2, "", $lastfocused)
    Else
        $lastfocused = $CurrentFocus
    EndIf
    
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch

WEnd
Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Space.... :D

Really ?!!!! :D

Obviously, That's the first thing i did !!!

But you cannot align exactly your text !!!

Link to comment
Share on other sites

This seems surprisingly difficult. I thought that perhaps you could use the return from _GDIPlus_GraphicsMeasureString to find out how much space a string took, then you could offset the position of the next string by that amount. It seemed like a good idea but it doesn't work. :D

Here's what I tried, maybe someone can see what I've done wrong, or why it doesn't work as I hoped.

(It doesn't even work with Courier New.)

#include <GuiConstantsEx.au3>
 #include <GDIPlus.au3>
 Func Notused()
     Opt('MustDeclareVars', 1)
     SplashTextOn("Title", "Hello" & Chr(09) & "world" & @CRLF & Chr(09) & "world", -1, -1, -1, -1, 4, "", 24)
     Sleep(10000)
     SplashOff()
 EndFunc;==>Notused
 
 _Main()
 
 Func _Main()
     Local $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $res
     Local $tabby
     Local $sString = "Hello     world", $sString2 = "world", $aInfo
         Local $testStr = "Hello     ";try to use this to measure the offset
; Create GUI
     $hGUI = GUICreate("GDI+", 400, 300)
     $hWnd = WinGetHandle("GDI+")
     GUISetState()
     _GDIPlus_Startup()
     $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
     $hBrush = _GDIPlus_BrushCreateSolid(0xFF00007F)
     $hFormat = _GDIPlus_StringFormatCreate()
     $hFamily = _GDIPlus_FontFamilyCreate("Arial")
     $hFont = _GDIPlus_FontCreate($hFamily, 12, 0)
     $tLayout = _GDIPlus_RectFCreate(140, 110, 0, 0)
     
;try to see how much space is used by "Hello     "
     $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $testStr, $hFont, $tLayout, $hFormat)
     $res = $aInfo[0]
     If IsArray($res) Then ConsoleWrite("$res is an array. It has " & UBound($res) & " elements." & @CRLF)
     ConsoleWrite("at " & $aInfo[0] & ', ' & DllStructGetData($res, 1) & ', ' & DllStructGetData($res, 2) & ', ' & DllStructGetData($res, 3) & ', ' & DllStructGetData($res, 4) & @CRLF)
     
     $tabby = DllStructGetData($res,3);I hoped this would be the offset to print "world"
     
     $tLayout = _GDIPlus_RectFCreate(140, 110, 0, 0)
     $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
     
     _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
     
     $tLayout = _GDIPlus_RectFCreate(140 + $tabby, 130, 0, 0)
     $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString2, $hFont, $tLayout, $hFormat)
     _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString2, $hFont, $aInfo[0], $hFormat, $hBrush)
; Loop until user exits
     Do
     Until GUIGetMsg() = $GUI_EVENT_CLOSE
 
; Clean up resources
     _GDIPlus_FontDispose($hFont)
     _GDIPlus_FontFamilyDispose($hFamily)
     _GDIPlus_StringFormatDispose($hFormat)
     _GDIPlus_BrushDispose($hBrush)
     _GDIPlus_GraphicsDispose($hGraphic)
     _GDIPlus_Shutdown()
 
 EndFunc;==>_Main

But here is another way to get the same affect though I might have misunderstood what you're trying to do. It has the advantage that tabs work and there is no need to redraw the text to keep the text correct, after another window has been on top for example. Also, you could flash the colours or change the text much more easily.

#include <EditConstants.au3>
 #include <GUIConstantsEx.au3>
 #include <WindowsConstants.au3>
 
 #Region ### START Koda GUI section ### Form=
 $Form2 = GUICreate("Form2", 413, 298, 303, 219)
 $Edit1 = GUICtrlCreateEdit("", 136, 48, 185, 89, BitOR($ES_READONLY, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_WANTRETURN), 0)
 GUICtrlSetData(-1, "Team B" & @TAB & "Team A" & @CRLF & "Rovers" & @TAB & "Jaguar" & @CRLF & "Prescott" & @TAB & "Highfield")
 GUICtrlSetFont(-1, 16, 400, 0, "Arial")
 GUICtrlSetColor(-1, 0x0000FF)
 $lab = GUICtrlCreateLabel('', 1, 1, 1, 1)
 GUISetState(@SW_SHOW)
 
 $wp = WinGetPos($Form2)
 $bkcol = PixelGetColor(10 + $wp[0], 10 + $wp[1])
 GUICtrlSetBkColor($Edit1, $bkcol)
 GUICtrlSetState($lab, $GUI_FOCUS)
 #EndRegion ### START Koda GUI section ### Form=
 
 $lastFocused = ControlGetFocus($Form2)
;ConsoleWrite($lastFocused & @CRLF)
 While 1
     $CurrentFocus = ControlGetFocus($Form2)
     If $CurrentFocus = "Edit1" Then
         ControlFocus($Form2, "", $lastfocused)
     Else
         $lastfocused = $CurrentFocus
     EndIf
     
     $nMsg = GUIGetMsg()
     Switch $nMsg
         Case $GUI_EVENT_CLOSE
             Exit
 
     EndSwitch
 
 WEnd

Well, I know that in normal GUI or Splash..() it works well,... But I'd'like to display some text on top of a picture and aligned...

Link to comment
Share on other sites

.. But I'd'like to display some text on top of a picture and aligned...

I was afraid you were going to say that.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I was afraid you were going to say that.

:D:D:)

well,... What can I say,...

I am a man hard to please... :D

Link to comment
Share on other sites

I think you can use space but only when you use nonproporcional font like Courier New instead of proporcional Arial.

Well, That's work !!!

I found more info here and here about proporcional and non-proporcional fonts.

Thanks this will HELP !!!

C.

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...