You are right. I added width and height information to the call
Now it works. It would be nice for it to work without the width and height information. In V1, I used this function from the GDIP-Library:
Code: Select all
Gdip_TextToGraphics(pGraphics, Text, Options, Font:="Arial", Width:="", Height:="", Measure:=0, userBrush:=0, Unit:=0, acceptTabStops:=0) {
; Font parameter can be a name of an already installed font or it can point to a font file
; to be loaded and used to draw the string.
;
; Set Unit to 3 [Pts] to have the texts rendered at the same size
; with the texts rendered in GUIs with -DPIscale
;
; userBrush - if a pBrush object is passed, this will be used to draw the text
;
; Remarks: by changing the alignment, the text will be rendered at a different X
; coordinate position; the position of the text is set relative to
; the given X position coordinate and the text width..
; See also Gdip_SetStringFormatAlign().
;
; On success, the function returns a string in the following format:
; "x|y|width|height|chars|lines"
; The first four elements represent the boundaries of the text.
; The string is returned by Gdip_MeasureString()
Static Styles := "Regular|Bold|Italic|BoldItalic|Underline|Strikeout"
, Alignments := "Near|Left|Centre|Center|Far|Right"
OWidth := Width
IWidth := Width, IHeight:= Height
pattern_opts := (A_AhkVersion < "2") ? "iO)" : "i)"
RegExMatch(Options, pattern_opts "X([\-\d\.]+)(p*)", xpos)
RegExMatch(Options, pattern_opts "Y([\-\d\.]+)(p*)", ypos)
RegExMatch(Options, pattern_opts "W([\-\d\.]+)(p*)", PWidth)
RegExMatch(Options, pattern_opts "H([\-\d\.]+)(p*)", Height)
RegExMatch(Options, pattern_opts "C(?!(entre|enter))([a-f\d]+)", Colour)
RegExMatch(Options, pattern_opts "Top|Up|Bottom|Down|vCentre|vCenter", vPos)
RegExMatch(Options, pattern_opts "NoWrap", NoWrap)
RegExMatch(Options, pattern_opts "R(\d)", Rendering)
RegExMatch(Options, pattern_opts "S(\d+)(p*)", Size)
Width := PWidth
if !(IWidth && IHeight) && ((xpos && xpos[2]) || (ypos && ypos[2]) || (Width && Width[2]) || (Height && Height[2]) || (Size && Size[2]))
return -1
fColor := (Colour && Colour[2]) ? Colour[2] : "ff000000"
If (StrLen(fColor)=6)
fColor := "ff" fColor
if (fColor && !userBrush)
pBrush := Gdip_BrushCreateSolid("0x" fColor)
Style := 0
For eachStyle, valStyle in StrSplit(Styles, "|")
{
if RegExMatch(Options, "\b" valStyle)
Style |= (valStyle != "StrikeOut") ? (A_Index-1) : 8
}
Align := 0
For eachAlignment, valAlignment in StrSplit(Alignments, "|")
{
if RegExMatch(Options, "\b" valAlignment)
Align |= A_Index//2.1 ; 0|0|1|1|2|2
}
xpos := (xpos && (xpos[1] != "")) ? xpos[2] ? IWidth*(xpos[1]/100) : xpos[1] : 0
ypos := (ypos && (ypos[1] != "")) ? ypos[2] ? IHeight*(ypos[1]/100) : ypos[1] : 0
Width := (Width && Width[1]) ? Width[2] ? IWidth*(Width[1]/100) : Width[1] : IWidth
Height := (Height && Height[1]) ? Height[2] ? IHeight*(Height[1]/100) : Height[1] : IHeight
Rendering := (Rendering && (Rendering[1] >= 0) && (Rendering[1] <= 5)) ? Rendering[1] : 4
Size := (Size && (Size[1] > 0)) ? Size[2] ? IHeight*(Size[1]/100) : Size[1] : 12
If RegExMatch(Font, "^(.\:\\.)")
{
hFontCollection := Gdip_NewPrivateFontCollection()
hFontFamily := Gdip_CreateFontFamilyFromFile(Font, hFontCollection)
} Else hFontFamily := Gdip_FontFamilyCreate(Font)
If !hFontFamily
hFontFamily := Gdip_FontFamilyCreateGeneric(1)
hFont := Gdip_FontCreate(hFontFamily, Size, Style, Unit)
FormatStyle := NoWrap ? 0x4000 | 0x1000 : 0x4000
hStringFormat := Gdip_StringFormatCreate(FormatStyle)
If !hStringFormat
hStringFormat := Gdip_StringFormatGetGeneric(1)
thisBrush := userBrush ? userBrush : pBrush
if !(hFontFamily && hFont && hStringFormat && thisBrush && pGraphics)
{
E := !pGraphics ? -2 : !hFontFamily ? -3 : !hFont ? -4 : !hStringFormat ? -5 : !pBrush ? -6 : 0
If pBrush
Gdip_DeleteBrush(pBrush)
If hStringFormat
Gdip_DeleteStringFormat(hStringFormat)
If hFont
Gdip_DeleteFont(hFont)
If hFontFamily
Gdip_DeleteFontFamily(hFontFamily)
If hFontCollection
Gdip_DeletePrivateFontCollection(hFontCollection)
return E
}
CreateRectF(RC, xpos, ypos, Width, Height)
If (acceptTabStops=1)
Gdip_SetStringFormatTabStops(hStringFormat, [50,100,200])
Gdip_SetStringFormatAlign(hStringFormat, Align)
If InStr(Options, "autotrim")
Gdip_SetStringFormatTrimming(hStringFormat, 3)
Gdip_SetTextRenderingHint(pGraphics, Rendering)
ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hStringFormat, RC)
ReturnRCtest := StrSplit(ReturnRC, "|")
testX := Floor(ReturnRCtest[1]) - 2
If (testX>xpos && NoWrap && (PWidth>2 || OWidth>2))
{
; error correction of posX for different text alignments
; when width is given, but no text wrap
nxpos := Floor(xpos - (testX - xpos))
CreateRectF(RC, nxpos, ypos, Width, Height)
ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hStringFormat, RC)
; MsgBox, % nxpos "--" xpos "--" ypos "`n" width "--" height "`n" ReturnRC
}
If vPos
{
ReturnRC := StrSplit(ReturnRC, "|")
if (vPos[0] = "vCentre") || (vPos[0] = "vCenter")
ypos += (Height-ReturnRC[4])//2
else if (vPos[0] = "Top") || (vPos[0] = "Up")
ypos += 0
else if (vPos[0] = "Bottom") || (vPos[0] = "Down")
ypos += Height-ReturnRC[4]
CreateRectF(RC, xpos, ypos, Width, ReturnRC[4])
ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hStringFormat, RC)
}
if !Measure
_E := Gdip_DrawString(pGraphics, Text, hFont, hStringFormat, thisBrush, RC)
If pBrush
Gdip_DeleteBrush(pBrush)
Gdip_DeleteStringFormat(hStringFormat)
Gdip_DeleteFont(hFont)
Gdip_DeleteFontFamily(hFontFamily)
If hFontCollection
Gdip_DeletePrivateFontCollection(hFontCollection)
return _E ? _E : ReturnRC
}
It does not depend on witdth/height options.
Anyway. Good enough for me right now. Thanks for your help!!! Spitzi