使用以下代码将字符串写入DesktopWindow的设备上下文,但背景颜色和文本颜色保持不变(白色为蓝色):
Private Sub writeToScreen(txt As String)
Declare Function GetDesktopWindow Lib "user32" () As Integer
Declare Function DrawTextW Lib "user32" (hdc As Integer, lpStr As WString, nCount As Integer, _
ByRef lpRect As RECT, wFormat As Integer) As Integer
Declare Function CreateDCA Lib "gdi32" (lpDriverName As CString, lpDeviceName As Integer, _
lpOutput As Integer, lpInitData As Integer) As Integer
Declare Function DeleteDC Lib "gdi32" (hdc As Integer) As Integer
Declare Function GetTextColor Lib "gdi32" (hdc As Integer) As Color
Declare Function SetTextColor Lib "gdi32" (hdc As Integer, crColor As Color) As Color
Declare Function GetBkColor Lib "gdi32" (hdc As Integer) As Color
Declare Function SetBkColor Lib "gdi32" (hdc As Integer, crColor As Color) As Color
Const DT_MULTILINE = &H00000001
Const DT_NOCLIP = &H100
Const INVALID_COLOR = &hFFFFFFFF
Dim tFormat As Integer = DT_MULTILINE Or DT_NOCLIP
Dim hdc As Integer = CreateDCA("DISPLAY", 0, 0, 0)
Dim tRect As RECT //The RECT structure is defined elsewhere
Dim textCol, backColor As Color
tR.Left = 200
tR.Top = 250
tR.Right = 600
tR.Bottom = 350
textCol = SetTextColor(hdc, &cFF8040)
backColor = SetBkColor(hdc, &c000000)
If DrawTextW(hdc, txt, Len(txt), tR, tFormat) = 0 Then
System.DebugLog("Text Draw Error")
End If
Call SetTextColor(hdc, textCol)
Call SetBkColor(hdc, backColor)
Call DeleteDC(hdc)
End Sub
我做错了什么?文字写得很好,但颜色很难看。
答案 0 :(得分:1)
首先使用SetBkMode()(http://msdn.microsoft.com/en-us/library/dd162965%28v=vs.85%29.aspx)将DC设置为不绘制背景。
SetTextColor()仅用于TextOut(),而不是DrawText(),IIRC - MSDN对它不明确。尝试在DC中选择不同的HBRUSH,这可能会做你想要的。