找回密码
 加入
搜索
查看: 397|回复: 7

【已解决】求教au3如何调用函数GdipDrawDriverString在绘制字符串时控制行间距和字符

[复制链接]
发表于 2024-3-1 22:59:51 | 显示全部楼层 |阅读模式
本帖最后由 zghwelcome 于 2024-4-8 20:10 编辑


_GDIPlus_GraphicsDrawString 函数绘制字符串时无法控制行间距及字符间距,搜索后需要调用 GdipDrawDriverString 实现,下面是一段VB代码,求大佬转换成au3,感谢!
<DllImport("Gdiplus.dll", CharSet:=CharSet.Unicode)>  

Friend Shared Function GdipDrawDriverString(ByVal graphics As IntPtr, _
          ByVal text As String, _
          ByVal length As Integer,  _
          ByVal font As IntPtr,  _
          ByVal brush As IntPtr, _
          ByVal positions() As PointF, _
          ByVal flags As Integer, _
          ByVal matrix As IntPtr) As Integer
End Function


Private Shared Sub DrawDriverString(ByVal graphics As Graphics, _
          ByVal text As String, ByVal font As Font,  _
          ByVal brush As Brush, ByVal positions() As PointF)

    DrawDriverString(graphics, text, font, brush, positions, Nothing)

End Sub

Private Shared Sub DrawDriverString(ByVal G As Graphics, _
          ByVal T As String, ByVal F As Font, _
          ByVal B As Brush, ByVal P() As PointF, ByVal M As Matrix)

    If (G Is Nothing) Then Throw New ArgumentNullException("graphics")

    If (T Is Nothing) Then Throw New ArgumentNullException("text")

    If (F Is Nothing) Then Throw New ArgumentNullException("font")

    If (B Is Nothing) Then Throw New ArgumentNullException("brush")

    If (P Is Nothing) Then Throw New ArgumentNullException("positions")

    Dim Field As FieldInfo

    Field = GetType(Graphics).GetField("nativeGraphics", BindingFlags.Instance Or BindingFlags.NonPublic)

    Dim hGraphics As IntPtr = Field.GetValue(G)

    Field = GetType(Font).GetField("nativeFont", BindingFlags.Instance Or BindingFlags.NonPublic)

    Dim hFont As IntPtr = Field.GetValue(F)

    Field = GetType(Brush).GetField("nativeBrush", BindingFlags.Instance Or BindingFlags.NonPublic)

    Dim hBrush As IntPtr = Field.GetValue(B)

    Dim hMatrix As IntPtr = IntPtr.Zero

    If (Not M Is Nothing) Then
      Field = GetType(Matrix).GetField("nativeMatrix", BindingFlags.Instance Or BindingFlags.NonPublic)
      hMatrix = Field.GetValue(M)
    End If

    Dim result As Integer = GdipDrawDriverString(hGraphics, T, T.Length, hFont, hBrush, P, DriverStringOptions.CmapLookup, hMatrix)

End Sub

Private Enum DriverStringOptions
    CmapLookup = 1
    Vertical = 2
    Advance = 4
    LimitSubpixel = 8
End Enum

Public Sub Draw3(ByVal Text As String)
    Clear()
    Dim i As Integer, tP() As PointF
    ReDim tP(Text.Length - 1)
    For i = 0 To Text.Length - 1
      tP(i).X = (i Mod 52) * 16 + 3
      tP(i).Y = 3 + Int(i / 52) * mLineHeight + 12
    Next
    DrawDriverString(mG, Text, mFont, New SolidBrush(mForeColor), tP)

End Sub
 楼主| 发表于 2024-3-1 23:01:09 | 显示全部楼层
上面vb代码 原网址
发表于 2024-3-2 15:11:27 | 显示全部楼层
看不懂也不知道应用场景~ 感觉这单字坐标的搞法只适合等宽字体……  不知道 _GDIPlus_FontFamilyGetLineSpacing 有没有启发

 楼主| 发表于 2024-3-2 16:25:38 | 显示全部楼层
afan 发表于 2024-3-2 15:11
看不懂也不知道应用场景~ 感觉这单字坐标的搞法只适合等宽字体……  不知道 _GDIPlus_FontFamilyGetLineSpa ...

就是想在图片绘制一段文字时,控制文字段落的行间距和字符间距
发表于 2024-3-5 08:44:47 | 显示全部楼层
看明白了,暂时没时间写
 楼主| 发表于 2024-3-5 16:16:24 | 显示全部楼层
haijie1223 发表于 2024-3-5 08:44
看明白了,暂时没时间写

静候大佬
发表于 2024-4-8 17:56:04 | 显示全部楼层

差点都忘了你这个问题了。。。
#NoTrayIcon
#include <Winapi.au3> 
#include <gdiplus.au3> 
Global $hGUI = GUICreate("GdipDrawDriverString", 850, 550)
GUISetState()
_GDIPlus_Startup()
Global $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI) 
Global $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
Global $hFamily = _GDIPlus_FontFamilyCreate("宋体")
Global $hFont = _GDIPlus_FontCreate($hFamily, 12, 2)
Global $Text = _
                '豫章故郡,洪都新府。星分翼轸,地接衡庐。襟三江而带五湖,控蛮荆而引瓯越。物华天宝,龙光射牛斗之墟;人杰地灵,徐孺下陈蕃之榻。雄州雾列,俊采星驰。台隍枕夷夏之交,宾主尽东南之美。都督阎公之雅望,棨戟遥临;宇文新州之懿范,襜帷暂驻。十旬休假,胜友如云;千里逢迎,高朋满座。腾蛟起凤,孟学士之词宗;紫电青霜,王将军之武库。家君作宰,路出名区;童子何知,躬逢胜饯。' & @CRLF & _
                '时维九月,序属三秋。潦水尽而寒潭清,烟光凝而暮山紫。俨骖騑于上路,访风景于崇阿;临帝子之长洲,得天人之旧馆。层峦耸翠,上出重霄;飞阁流丹,下临无地。鹤汀凫渚,穷岛屿之萦回;桂殿兰宫,即冈峦之体势。' & @CRLF & _
                '披绣闼,俯雕甍,山原旷其盈视,川泽纡其骇瞩。闾阎扑地,钟鸣鼎食之家;舸舰弥津,青雀黄龙之舳。云销雨霁,彩彻区明。落霞与孤鹜齐飞,秋水共长天一色。渔舟唱晚,响穷彭蠡之滨;雁阵惊寒,声断衡阳之浦。' & @CRLF & _
                '遥襟甫畅,逸兴遄飞。爽籁发而清风生,纤歌凝而白云遏。睢园绿竹,气凌彭泽之樽;邺水朱华,光照临川之笔。四美具,二难并。穷睇眄于中天,极娱游于暇日。天高地迥,觉宇宙之无穷;兴尽悲来,识盈虚之有数。望长安于日下,目吴会于云间。地势极而南溟深,天柱高而北辰远。关山难越,谁悲失路之人?萍水相逢,尽是他乡之客。怀帝阍而不见,奉宣室以何年?' & @CRLF & _
                '嗟乎!时运不齐,命途多舛。冯唐易老,李广难封。屈贾谊于长沙,非无圣主;窜梁鸿于海曲,岂乏明时?所赖君子见机,达人知命。老当益壮,宁移白首之心?穷且益坚,不坠青云之志。酌贪泉而觉爽,处涸辙以犹欢。北海虽赊,扶摇可接;东隅已逝,桑榆非晚。孟尝高洁,空余报国之情;阮籍猖狂,岂效穷途之哭!' & @CRLF & _
                '勃,三尺微命,一介书生。无路请缨,等终军之弱冠;有怀投笔,慕宗悫之长风。舍簪笏于百龄,奉晨昏于万里。非谢家之宝树,接孟氏之芳邻。他日趋庭,叨陪鲤对;今兹捧袂,喜托龙门。杨意不逢,抚凌云而自惜;钟期既遇,奏流水以何惭?' & @CRLF & _
                '呜乎!胜地不常,盛筵难再;兰亭已矣,梓泽丘墟。临别赠言,幸承恩于伟饯;登高作赋,是所望于群公。敢竭鄙怀,恭疏短引;一言均赋,四韵俱成。请洒潘江,各倾陆海云尔:' & @CRLF & _
                '滕王高阁临江渚,佩玉鸣鸾罢歌舞。' & @CRLF & _
                '画栋朝飞南浦云,珠帘暮卷西山雨。' & @CRLF & _
                '闲云潭影日悠悠,物换星移几度秋。' & @CRLF & _
                '阁中帝子今何在?槛外长江空自流。'
Enum $CmapLookup = 1, $Vertical = 2, $Advance = 4, $LimitSubpixel = 8
Global $mLineHeight = 25;//调整行间距
Global $pBuffer = _WinAPI_CreateBuffer(8 * StringLen($Text))
For $i = 0 To StringLen($Text) - 1
        Local $tP = DllStructCreate('float x;float y;', $pBuffer + $i * 8)
        DllStructSetData($tP, 'x', Mod($i, 52) * 16 + 3)
        DllStructSetData($tP, 'y', 3 + Int($i / 52) * $mLineHeight + 12)
Next
GdipDrawDriverString($hGraphic, $Text, StringLen($Text), $hFont, $hBrush, $pBuffer, $CmapLookup, Null)
Do
        Sleep(50)
Until GUIGetMsg() = -3
_WinAPI_FreeMemory($pBuffer)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()
Func GdipDrawDriverString($hGraphics, $sString, $length, $hFont, $hBrush, $tPointF, $flags, $matrix)
        Local $aCall = DllCall($__g_hGDIPDll, "int", "GdipDrawDriverString", "handle", $hGraphics, "wstr", $sString, "int", $length, "handle", $hFont, "handle", $hBrush, "ptr", $tPointF, "int", $flags, "handle", $matrix);
        If @error Then Return SetError(@error, @extended, False)
        If $aCall[0] Then Return SetError(10, $aCall[0], False)
        Return True
EndFunc   ;==>GdipDrawDriverString

评分

参与人数 1金钱 +60 收起 理由
zghwelcome + 60 很给力!

查看全部评分

 楼主| 发表于 2024-4-8 20:07:27 | 显示全部楼层
haijie1223 发表于 2024-4-8 17:56
差点都忘了你这个问题了。。。

感谢大佬解答
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-12-22 11:47 , Processed in 0.083722 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表