如何在vba中修改TextOut函数所用字体的颜色?

如何在vba中用GDI在屏幕上写字? 一文中我们介绍了如何用TextOut函数在屏幕上写字,但是字体的颜色是黑色的。

如果要修改所用字体的颜色,可以使用SetTextColor直接选择要用的字体的颜色,然后再用TextOut函数写字。

代码如下:

Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hDC As Long, ByVal nXStart As Long, ByVal nYStart As Long, ByVal lpString As String, ByVal cchString As Long) As Long
Sub QQ1722187970()
    Dim hDC As Long
    hDC = GetDC(0)
    Dim str As String
    str = "我爱你中国!!!"
    '用红色书写文字
    SetTextColor hDC, vbRed
    TextOut hDC, 100, 100, str, LenB(str)
    ReleaseDC 0, hDC
End Sub
       

发表评论