UEZ Posted March 14, 2009 Share Posted March 14, 2009 (edited) I try to get the x pixel length of a text which is drawn in a graphics window (buffer) without displaying it. expandcollapse popup#include <WindowsConstants.au3> #include <GuiConstantsEx.au3> #include <GDIPlus.au3> ;~ Opt('MustDeclareVars', 1) _Main() Func _Main() Local $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $coord Local $gui_x, $gui_y, $color, $coord_x, $test $gui_x = 180 $gui_y = 40 ; Create GUI $hGUI = GUICreate("GDI+", $gui_x, $gui_y, 0, 0, BitOR($WS_POPUP,$DS_MODALFRAME), 0) $hWnd = WinGetHandle("GDI+") GUISetState() ; Draw a string _GDIPlus_Startup () $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd) $Bitmap = _GDIPlus_BitmapCreateFromGraphics($gui_x, $gui_y, $hGraphic) $Buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap) $hBrush = _GDIPlus_BrushCreateSolid (0xFFFFFFFF) $hFormat = _GDIPlus_StringFormatCreate () $hFamily = _GDIPlus_FontFamilyCreate ("Arial") $hFont = _GDIPlus_FontCreate ($hFamily, 24, 0) _GDIPlus_GraphicsClear($Buffer, 0xFFFF7FAF) $tLayout = _GDIPlus_RectFCreate (0, 0, $gui_x, $gui_y) _GDIPlus_GraphicsDrawStringEx ($Buffer, "WAbBgGiL", $hFont, $tLayout, $hFormat, $hBrush) _GDIPlus_GraphicsDrawImageRect($hGraphic, $Bitmap, 0, 0, $gui_x, $gui_y) ;~ _GDIPlus_ImageSaveToFile($Bitmap,@ScriptDir & "\Test02.jpg") For $x = $gui_x - 1 To 1 Step - 1 $coord = PixelSearch($x, 0, $gui_x, $gui_y, 0xFFFFFF, -1, -1, $hWnd) If Not @error Then ExitLoop EndIf Next $xr = $x + 1 For $x = 1 To $gui_x $coord = PixelSearch(0, 0, $x + 1, $gui_y, 0xFFFFFF, -1, -1, $hWnd) If Not @error Then ExitLoop EndIf Next $xl = $x _GDIPlus_GraphicsDrawLine($hGraphic,$xr, 0, $xr, $gui_y) _GDIPlus_GraphicsDrawLine($hGraphic,$xl, 0, $xl, $gui_y) ConsoleWrite("Text length: " & $xr - $xl & " pixel" & @CRLF) ; 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 Any idea how to implement that (better and faster)? UEZ Edited November 8, 2010 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 More sharing options...
monoceres Posted March 14, 2009 Share Posted March 14, 2009 Hi! Check out _GDIPlus_GraphicsMeasureString(), it does exactly what you want Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
UEZ Posted March 14, 2009 Author Share Posted March 14, 2009 Thanks for your fast response but I want to get the pixel length of the drawn text. _GDIPlus_GraphicsMeasureString() gives me only the amount of characters back and little more. E.g. if I want to center the text I need the pixel length of drawn text! 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 More sharing options...
trancexx Posted March 14, 2009 Share Posted March 14, 2009 Thanks for your fast response but I want to get the pixel length of the drawn text. _GDIPlus_GraphicsMeasureString() gives me only the amount of characters back and little more.E.g. if I want to center the text I need the pixel length of drawn text!UEZ_GDIPlus_GraphicsMeasureString() returns array which first element -0- is pointer to stucture that holds data that you need. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
UEZ Posted March 14, 2009 Author Share Posted March 14, 2009 (edited) Here the example from the help (a little bit modified): expandcollapse popup#include <GuiConstantsEx.au3> #include <GDIPlus.au3> #include <Array.au3> Opt('MustDeclareVars', 1) _Main() Func _Main() Local $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $aInfo Local $sString = "Hello world" Local $width = 400 Local $heigh = 40 ; Create GUI $hGUI = GUICreate("GDI+", $width, $heigh) $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, 2) $tLayout = _GDIPlus_RectFCreate (0, 0, 0, 0) $aInfo = _GDIPlus_GraphicsMeasureString ($hGraphic, $sString, $hFont, $tLayout, $hFormat) _GDIPlus_GraphicsDrawStringEx ($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush) ;~ _ArrayDisplay($aInfo) ; 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 Currently, I cannot see what $aInfo[0] is doing! How can I get the values of $tagGDIPRECTF structure ? UEZ Edited March 14, 2009 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 More sharing options...
UEZ Posted March 14, 2009 Author Share Posted March 14, 2009 Ok, I got it. expandcollapse popup#include <GuiConstantsEx.au3> #include <GDIPlus.au3> #include <Array.au3> Opt('MustDeclareVars', 1) _Main() Func _Main() Local $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $aInfo Local $x, $y, $w, $h Local $sString = "Hello world" Local $width = 400 Local $heigh = 40 ; Create GUI $hGUI = GUICreate("GDI+", $width, $heigh) $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, 2) $tLayout = _GDIPlus_RectFCreate (10, 10, 0, 0) $aInfo = _GDIPlus_GraphicsMeasureString ($hGraphic, $sString, $hFont, $tLayout, $hFormat) ;~ _ArrayDisplay($aInfo) $x = DllStructGetData($aInfo[0],1) $y = DllStructGetData($aInfo[0],2) $w = DllStructGetData($aInfo[0],3) $h = DllStructGetData($aInfo[0],4) _GDIPlus_GraphicsDrawStringEx ($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush) ConsoleWrite("x: " & $x & @CRLF & "y: " & $y & @CRLF & "w: " & $w & @CRLF & "h: " & $h & @CRLF) ; 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 Now I will check whether it is possible to measure before drawing the text... 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 More sharing options...
UEZ Posted March 14, 2009 Author Share Posted March 14, 2009 (edited) Here the complete version: expandcollapse popup#include <GuiConstantsEx.au3> #include <GDIPlus.au3> #include <Array.au3> Opt('MustDeclareVars', 1) Global $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout Global $x, $y, $w, $h Global $sString = "Hello world" Global $width = 400 Global $heigh = 40 ; Create GUI $hGUI = GUICreate("GDI+", $width, $heigh) $hWnd = WinGetHandle("GDI+") GUISetState() _Main() Func _Main() ; 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, 2) Measure($sString) $tLayout = _GDIPlus_RectFCreate (($width - $w) / 2, ($heigh - $h) / 2, 0, 0) _GDIPlus_GraphicsDrawStringEx ($hGraphic, $sString, $hFont, $tLayout, $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 Func Measure($text) _GDIPlus_Startup () Local $hBuffer = _GDIPlus_GraphicsCreateFromHWND ($hWnd) Local $hBrush = _GDIPlus_BrushCreateSolid (0xFF00007F) Local $hFormat = _GDIPlus_StringFormatCreate () Local $hFamily = _GDIPlus_FontFamilyCreate ("Arial") Local $hFont = _GDIPlus_FontCreate ($hFamily, 12, 2) Local $tLayout = _GDIPlus_RectFCreate (0, 0, 0, 0) Local $aInfo = _GDIPlus_GraphicsMeasureString ($hBuffer, $sString, $hFont, $tLayout, $hFormat) $x = DllStructGetData($aInfo[0],1) $y = DllStructGetData($aInfo[0],2) $w = DllStructGetData($aInfo[0],3) $h = DllStructGetData($aInfo[0],4) ConsoleWrite("x: " & $x & @CRLF & "y: " & $y & @CRLF & "w: " & $w & @CRLF & "h: " & $h & @CRLF) EndFunc This is a fast and dirty code Thanks for monoceres and trancexx for the hints UEZ Edited March 14, 2009 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 More sharing options...
monoceres Posted March 15, 2009 Share Posted March 15, 2009 (edited) I'd just like to add that the easiest way to center text is to use the function _GDIPlus_StringFormatSetAlign(). Edited March 15, 2009 by monoceres Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
trancexx Posted March 15, 2009 Share Posted March 15, 2009 And I would like to add that information that I provided was/is wrong. It's not pointer obviously, but the whole structure. ♡♡♡ . eMyvnE 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