Modify

Opened 3 years ago

Closed 3 years ago

#3806 closed Feature Request (Completed)

_GDIPlus_GraphicsDrawString() with color

Reported by: argumentum Owned by: Jpm
Milestone: 3.3.15.4 Component: Standard UDFs
Version: Severity: None
Keywords: _GDIPlus_GraphicsDrawString() Cc:

Description

_GDIPlus_GraphicsDrawString() is a wrapper for _GDIPlus_GraphicsDrawStringEx(). The ability to choose a color is there but not implemented and can be easily added as a last parameter $iARGB:

Func _GDIPlus_GraphicsDrawString_wColor($hGraphics, $sString, $nX, $nY, $sFont = "Arial", $fSize = 10, $iFormat = 0, $iARGB = 0xFF000000)
	Local $hBrush = _GDIPlus_BrushCreateSolid($iARGB)
	Local $hFormat = _GDIPlus_StringFormatCreate($iFormat)
	Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
	Local $hFont = _GDIPlus_FontCreate($hFamily, $fSize)
	Local $tLayout = _GDIPlus_RectFCreate($nX, $nY, 0.0, 0.0)
	Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sString, $hFont, $tLayout, $hFormat)
	If @error Then Return SetError(@error, @extended, 0)
	Local $aResult = _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
	Local $iError = @error, $iExtended = @extended
	_GDIPlus_FontDispose($hFont)
	_GDIPlus_FontFamilyDispose($hFamily)
	_GDIPlus_StringFormatDispose($hFormat)
	_GDIPlus_BrushDispose($hBrush)
	Return SetError($iError, $iExtended, $aResult)
EndFunc   ;==>_GDIPlus_GraphicsDrawString

Attachments (0)

Change History (8)

comment:1 Changed 3 years ago by TicketCleanup

  • Version 3.3.15.3 deleted

Automatic ticket cleanup.

comment:2 Changed 3 years ago by Jpm

Hi,
Do you understand why it is working only if alpha channel is set to FF
0XFF0000 does not work with the AutoIt Example

comment:3 Changed 3 years ago by argumentum

yes. I only added the default for _GDIPlus_BrushCreateSolid(), and this function calls for it.
In my testing below, it shows to work with Alpha other than FF

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    Local $hGUI, $hGraphic

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

    ; Draw a string
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    _GDIPlus_GraphicsDrawString_wColor($hGraphic, "Hello world", 140, 110, "Arial", 10, 0, 0x33006600)
	Sleep(1000)
    _GDIPlus_GraphicsDrawString_wColor($hGraphic, "Hello world", 140, 110, "Arial", 10, 0, 0xFF006600)
;~     _GDIPlus_GraphicsDrawString($hGraphic, "Hello world", 140, 110, "Arial", 10, 0)

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

    ; Clean up resources
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

Func _GDIPlus_GraphicsDrawString_wColor($hGraphics, $sString, $nX, $nY, $sFont = "Arial", $fSize = 10, $iFormat = 0, $iARGB = 0xFF000000)
	Local $hBrush = _GDIPlus_BrushCreateSolid($iARGB)
	Local $hFormat = _GDIPlus_StringFormatCreate($iFormat)
	Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
	Local $hFont = _GDIPlus_FontCreate($hFamily, $fSize)
	Local $tLayout = _GDIPlus_RectFCreate($nX, $nY, 0.0, 0.0)
	Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sString, $hFont, $tLayout, $hFormat)
	If @error Then Return SetError(@error, @extended, 0)
	Local $aResult = _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
	Local $iError = @error, $iExtended = @extended
	_GDIPlus_FontDispose($hFont)
	_GDIPlus_FontFamilyDispose($hFamily)
	_GDIPlus_StringFormatDispose($hFormat)
	_GDIPlus_BrushDispose($hBrush)
	Return SetError($iError, $iExtended, $aResult)
EndFunc   ;==>_GDIPlus_GraphicsDrawString

comment:4 Changed 3 years ago by Jpm

Thanks,
so I think If alpha = 0 then we need to force alpha = 0xff

right?

comment:5 Changed 3 years ago by argumentum

All "Brush" in GDIplus use ARGB. It should not be a "got ya".
The function can exclude the Alpha aspect altogether and pass a $iRGB, but the extra $iARGB is from _GDIPlus_BrushCreateSolid().
On the other hand, an alpha of 0x00 is like .. will show nothing. So adding the 0xFF would aid a scripter that did not read the included help ( that by then will have a use example ).

I vote for "no alpha force" to keep it in line with _GDIPlus_GraphicsDrawStringEx() where the _GDIPlus_BrushCreateSolid() calls for ARGB.

comment:6 Changed 3 years ago by argumentum

...actually my request is because I use https://www.autoitscript.com/forum/files/file/489-my-fine-tuned-high-contrast-theme/ and I don't have black as default text, so, to have a proper default we'd have to lookup the default color by If $iARGB = Default Then $iARGB = _WinAPI_SwitchColor(_WinAPI_GetSysColor($COLOR_WINDOWTEXT)) + 0xFF000000 so the modified example is

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WinAPISysWin.au3>
#include <WinAPIGdi.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>

Example()

Func Example() ; for https://www.autoitscript.com/trac/autoit/ticket/3806#comment:6
    Local $hGUI, $hGraphic

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

    ; Draw a string
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    _GDIPlus_GraphicsDrawString_wColor($hGraphic, "Hello world", 140, 110, "Arial", 10, 0, Default)
	Sleep(1000)
    _GDIPlus_GraphicsDrawString_wColor($hGraphic, "Hello world", 140, 110, "Arial", 10, 0, 0xFF00FF00)
	Sleep(1000)
    _GDIPlus_GraphicsDrawString($hGraphic, "Hello world", 140, 110, "Arial", 10, 0)

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

    ; Clean up resources
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

Func _GDIPlus_GraphicsDrawString_wColor($hGraphics, $sString, $nX, $nY, $sFont = "Arial", $fSize = 10, $iFormat = 0, $iARGB = Default)
	If $iARGB = Default Then $iARGB = _WinAPI_SwitchColor(_WinAPI_GetSysColor($COLOR_WINDOWTEXT)) + 0xFF000000
	Local $hBrush = _GDIPlus_BrushCreateSolid($iARGB)
	Local $hFormat = _GDIPlus_StringFormatCreate($iFormat)
	Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
	Local $hFont = _GDIPlus_FontCreate($hFamily, $fSize)
	Local $tLayout = _GDIPlus_RectFCreate($nX, $nY, 0.0, 0.0)
	Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sString, $hFont, $tLayout, $hFormat)
	If @error Then Return SetError(@error, @extended, 0)
	Local $aResult = _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
	Local $iError = @error, $iExtended = @extended
	_GDIPlus_FontDispose($hFont)
	_GDIPlus_FontFamilyDispose($hFamily)
	_GDIPlus_StringFormatDispose($hFormat)
	_GDIPlus_BrushDispose($hBrush)
	Return SetError($iError, $iExtended, $aResult)
EndFunc   ;==>_GDIPlus_GraphicsDrawString

comment:7 Changed 3 years ago by argumentum

..also, to avoid all those #include, this next example may be a better fit

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
;~ #include <WinAPISysWin.au3>
;~ #include <WinAPIGdi.au3>
;~ #include <WinAPISysWin.au3>
;~ #include <WindowsConstants.au3>

Example()

Func Example() ; for https://www.autoitscript.com/trac/autoit/ticket/3806#comment:7
    Local $hGUI, $hGraphic

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

    ; Draw a string
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    _GDIPlus_GraphicsDrawString_wColor($hGraphic, "Hello world", 140, 110, "Arial", 10, 0, Default)
	Sleep(1000)
    _GDIPlus_GraphicsDrawString_wColor($hGraphic, "Hello world", 140, 110, "Arial", 10, 0, 0xFF00FF00)
	Sleep(1000)
    _GDIPlus_GraphicsDrawString($hGraphic, "Hello world", 140, 110, "Arial", 10, 0)

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

    ; Clean up resources
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

Func __GDIPlus_GetSysColor($iIndex = 8) ; $COLOR_WINDOWTEXT = 8 ; to avoid all those #include
	Local $aResult = DllCall("user32.dll", "INT", "GetSysColor", "int", $iIndex)
	If @error Then Return SetError(@error, @extended, 0)
	Return BitOR(BitAND($aResult[0], 0x00FF00), BitShift(BitAND($aResult[0], 0x0000FF), -16), BitShift(BitAND($aResult[0], 0xFF0000), 16))
EndFunc   ;==>_WinAPI_GetSysColor

Func _GDIPlus_GraphicsDrawString_wColor($hGraphics, $sString, $nX, $nY, $sFont = "Arial", $fSize = 10, $iFormat = 0, $iARGB = Default)
;~ 	If $iARGB = Default Then $iARGB = _WinAPI_SwitchColor(_WinAPI_GetSysColor($COLOR_WINDOWTEXT)) + 0xFF000000
	If $iARGB = Default Then $iARGB = __GDIPlus_GetSysColor() + 0xFF000000 ; to avoid all those #include
	Local $hBrush = _GDIPlus_BrushCreateSolid($iARGB)
	Local $hFormat = _GDIPlus_StringFormatCreate($iFormat)
	Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
	Local $hFont = _GDIPlus_FontCreate($hFamily, $fSize)
	Local $tLayout = _GDIPlus_RectFCreate($nX, $nY, 0.0, 0.0)
	Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sString, $hFont, $tLayout, $hFormat)
	If @error Then Return SetError(@error, @extended, 0)
	Local $aResult = _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
	Local $iError = @error, $iExtended = @extended
	_GDIPlus_FontDispose($hFont)
	_GDIPlus_FontFamilyDispose($hFamily)
	_GDIPlus_StringFormatDispose($hFormat)
	_GDIPlus_BrushDispose($hBrush)
	Return SetError($iError, $iExtended, $aResult)
EndFunc   ;==>_GDIPlus_GraphicsDrawString

by adding GDIPlus_GetSysColor() as an internal function.

comment:8 Changed 3 years ago by Jpm

  • Milestone set to 3.3.15.4
  • Owner set to Jpm
  • Resolution set to Completed
  • Status changed from new to closed

Added by revision [12494] in version: 3.3.15.4

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The owner will remain Jpm.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.