2011-07-09 69 views
0

使用以下代碼將字符串寫入DesktopWindow的設備上下文,但背景顏色和文本顏色保持不變(藍色爲白色):SetBkColor和SetTextColor不設置DrawText的背景和文本顏色

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 

我在做什麼錯?文字寫得很好,但顏色很難看。

+5

**你做錯的主要事情是直接畫到桌面。**你從來沒有**應該這樣做。您不擁有桌面窗口。 Windows擁有桌面窗口。就像你作爲一個蹣跚學步的孩子一樣學習,你不應該偷竊和毀壞你不擁有的東西。現在,請重新設計您的應用程序。 –

+0

改爲使用Graphics.FromHdc()和TextRenderer.DrawText()。 –

+0

@Cody Gray那裏沒有爭論,但那不是我問的。 –

回答

1

首先使用SetBkMode()(http://msdn.microsoft.com/en-us/library/dd162965%28v=vs.85%29.aspx)將DC設置爲不繪製背景。

SetTextColor()僅用於TextOut(),而不是DrawText(),IIRC - MSDN在其上不明確。嘗試在DC中選擇一個不同的HBRUSH,它可以做你想做的事。

+0

謝謝,它做到了:) –