2 回答
TA贡献1842条经验 获得超21个赞
是的,您必须SetTextColor在调用之前使用更改颜色DrawText
你是正确的调用DrawText与DT_CALCRECT选择。这不会绘制任何东西,它只是计算矩形的高度(基于宽度...)PythonDrawText将返回一个计算矩形的元组。
然后DrawText再次调用,使用相同的文本格式,没有DT_CALCRECT标志。然后偏移矩形,改变颜色,并绘制下一个文本。
请注意,这在 pywin32 中会变得非常混乱,首先在 C/C++ 中尝试它可能会更容易。
if message == win32con.WM_PAINT:
hDC, paintStruct = win32gui.BeginPaint(hWnd)
fontSize = 16
lf = win32gui.LOGFONT()
lf.lfFaceName = "Stencil"
lf.lfHeight = fontSize
lf.lfWeight = 600
lf.lfQuality = win32con.NONANTIALIASED_QUALITY
hf = win32gui.CreateFontIndirect(lf)
win32gui.SelectObject(hDC, hf)
text1 = 'line1'
text2 = 'line2'
text3 = 'line3'
rect = win32gui.GetClientRect(hWnd)
textformat = win32con.DT_LEFT | win32con.DT_TOP
win32gui.SetTextColor(hDC,win32api.RGB(255,0,0))
drawrect = win32gui.DrawText(hDC, text1, -1, rect, textformat | win32con.DT_CALCRECT);
win32gui.DrawText(hDC, text1, -1, rect, textformat)
l = drawrect[1][0]
t = drawrect[1][1]
r = drawrect[1][2]
b = drawrect[1][3]
height = b - t
rect = (l, t + height, r, b + height)
win32gui.SetTextColor(hDC,win32api.RGB(0,255,0))
drawrect = win32gui.DrawText(hDC, text2, -1, rect, textformat | win32con.DT_CALCRECT);
win32gui.DrawText(hDC, text2, -1, rect, textformat)
l = drawrect[1][0]
t = drawrect[1][1]
r = drawrect[1][2]
b = drawrect[1][3]
height = b - t
rect = (l, t + height, r, b + height)
win32gui.SetTextColor(hDC,win32api.RGB(0,0,255))
drawrect = win32gui.DrawText(hDC, text3, -1, rect, textformat | win32con.DT_CALCRECT);
win32gui.DrawText(hDC, text3, -1, rect, textformat)
win32gui.EndPaint(hWnd, paintStruct)
return 0
添加回答
举报