SetBkColor 和 SetTextColor 不设置 DrawText 的背景和文本颜色

发布于 2024-11-19 08:33:14 字数 1539 浏览 5 评论 0原文

使用以下代码将字符串写入 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

我做错了什么?文字写得很好,但颜色很难看。

Using the following code to write a string to the DesktopWindow's device context works, but the background color and text color remain the same (white on blue):

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

What am I doing wrong? The text gets written just fine, but the colors are ugly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2024-11-26 08:33:14

首先使用 SetBkMode() (http://msdn.microsoft.com/en-us/library/dd162965%28v=vs.85%29.aspx) 将 DC 设置为不绘制背景。

SetTextColor() 仅用于 TextOut(),而不用于 DrawText(),IIRC - MSDN 对此不明确。尝试在 DC 中选择不同的 HBRUSH,这可能会满足您的要求。

Use SetBkMode() (http://msdn.microsoft.com/en-us/library/dd162965%28v=vs.85%29.aspx) first to set the DC to not draw a background.

SetTextColor() is only used for TextOut(), not DrawText(), IIRC - MSDN is ambiguous on it. Try seleting a different HBRUSH into the DC, that may do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文