我设法使一个程序合并在一起,该程序在多行上接收一个字符串并将它们打印到透明背景上。我想知道如果有颜色的字符串不同颜色的各个部分的一种方式。我知道有,但是我对win32的缺乏了解确实在这里困扰我。我需要将文本分成两部分并调用drawText()还是可以在字符串的一半位置更改文本颜色?关于信息或解决方案的任何观点都是很好的。
示例:string =“用户名:用户已发送的某些消息。”
我已经在Stack和其他多个网站上进行搜索,但到目前为止还没有高兴。 我通常不会,但是我已经转储了代码,因为它可以运行,您可以明白我的意思了。
由于缺少注释和代码状态,我向您致歉。
import win32api
import win32con
import win32gui
import time
import threading
from collections import deque
userAndMessage = deque()
def queue(message):
userAndMessage.append(message)
def getQueue():
return userAndMessage;
def dequeue():
return userAndMessage.popleft()
def cleanMessage(message):
return message.split("\r\n")[0]
def showMessages():
return userAndMessage[0] + "\n" + userAndMessage[1] + "\n" +
userAndMessage[2] + "\n" + userAndMessage[3] + "\n" + userAndMessage[4]
#Code example modified from:
#Christophe Keller
#Hello World in Python using Win32
windowText = ''
def main():
#get instance handle
hInstance = win32api.GetModuleHandle()
# the class name
className = 'SimpleWin32'
# create and initialize window class
wndClass = win32gui.WNDCLASS()
wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW
wndClass.lpfnWndProc = wndProc
wndClass.hInstance = hInstance
wndClass.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW)
wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH)
wndClass.lpszClassName = className
# register window class
wndClassAtom = None
try:
wndClassAtom = win32gui.RegisterClass(wndClass)
except Exception as e:
print (e)
raise e
exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED |
win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST |
win32con.WS_EX_TRANSPARENT
style = win32con.WS_DISABLED | win32con.WS_POPUP | win32con.WS_VISIBLE
hWindow = win32gui.CreateWindowEx(
exStyle,
wndClassAtom,
None, # WindowName
style,
20, # x
900, # y
1920, # width
600, # height
None, # hWndParent
None, # hMenu
hInstance,
None # lpParam
)
# Show & update the window
win32gui.SetLayeredWindowAttributes(hWindow, 0x00ffffff, 255,
win32con.LWA_COLORKEY | win32con.LWA_ALPHA)
win32gui.SetWindowPos(hWindow, win32con.HWND_TOPMOST, 0, 0, 0, 0,
win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE
| win32con.SWP_SHOWWINDOW)
win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL)
win32gui.UpdateWindow(hWindow)
# New code: Create and start the thread
thr = threading.Thread(target=customDraw, args=(hWindow,))
thr.setDaemon(False)
thr.start()
# Dispatch messages
win32gui.PumpMessages()
# New code: Attempt to change the text 1 second later
def customDraw(hWindow):
strOne = "SomeUser: This is test line one"
strTwo = "SomeOtherUser: This is test line two"
strThree = "AndAnother: This is test line three"
strFour = "UserOne: This is test line four"
strFive = "AndAgain: This is test line five"
queue(strOne)
queue(strTwo)
queue(strThree)
queue(strFour)
queue(strFive)
global windowText
windowText = showMessages()
win32gui.RedrawWindow(hWindow, None, None, win32con.RDW_INVALIDATE |
win32con.RDW_ERASE)
def wndProc(hWnd, message, wParam, lParam):
if message == win32con.WM_PAINT:
hDC, paintStruct = win32gui.BeginPaint(hWnd)
fontSize = 26
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)
win32gui.SetTextColor(hDC,win32api.RGB(240,0,50))
rect = win32gui.GetClientRect(hWnd)
win32gui.DrawText(hDC,windowText,-1, rect, win32con.DT_CALCRECT);
win32gui.DrawText(
hDC,
windowText,
-1,
rect,
win32con.DT_NOCLIP | win32con.DT_VCENTER |
win32con.DT_EXPANDTABS
)
win32gui.EndPaint(hWnd, paintStruct)
return 0
elif message == win32con.WM_DESTROY:
print('Being destroyed')
win32gui.PostQuitMessage(0)
return 0
else:
return win32gui.DefWindowProc(hWnd, message, wParam, lParam)
if __name__ == '__main__':
main()
可能会有一些缩进,这不是程序中的情况,只是我必须在每行文本上按空格键4次。
谢谢
答案 0 :(得分:1)
是的,在调用DrawText
之前必须使用DT_CALCRECT
更改颜色
您使用DrawText
选项正确调用了DrawText
。这不会画任何东西,它只是计算矩形的高度(基于宽度...)。Python的DT_CALCRECT
将为计算出的矩形返回一个元组。
然后再次使用相同的文本格式(没有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
标志)调用state = {
social_media: [],
page_home: [],
loading: true,
};
getCoffee() {
return new Promise(resolve => {
setTimeout(() => resolve('☕'), 2000); // it takes half of a second to make coffee
});
}
async showData() {
try {
const wpSocial = axios(`${ACF_DATA_URL}/options/social_media_data`);
const wpHome = axios(`${WP_DATA_URL}/pages?slug=home`);
await this.getCoffee();
await Promise.all([wpSocial, wpHome]).then(response => {
this.setState({
social_media: response[0].data.social_media_data,
page_home: response[1].data[0],
loading: false,
});
});
} catch (e) {
console.error(e); //
}
}
componentDidMount() {
this.showData();
}
。然后偏移矩形,更改颜色,然后绘制下一个文本。
请注意,这在pywin32中会变得非常混乱,首先在C / C ++中进行尝试可能会更容易。
<exclusions>
<exclusion>
<artifactId>xercesImpl</artifactId>
<groupId>xerces</groupId>
</exclusion>
<exclusion>
<artifactId>xmlParserAPIs</artifactId>
<groupId>xerces</groupId>
</exclusion>
</exclusions>
答案 1 :(得分:1)
@Barmak,这是我从您的帮助中获得的代码...我将您标记为正确,如果您未发布,我仍然会为此感到困惑。如果您运行它,则可以看到它正在运行,您的传奇!
import win32api
import win32con
import win32gui
import time
import threading
from collections import deque
messagePrompt = ' :'
userAndMessage = deque()
def queue(message):
userAndMessage.append(message)
def getQueue():
return userAndMessage;
def dequeue():
return userAndMessage.popleft()
def cleanMessage(message):
return message.split("\r\n")[0]
def showMessages():
return userAndMessage[0] + "\n" + userAndMessage[1] + "\n" + userAndMessage[2] +
"\n" + userAndMessage[3] + "\n" + userAndMessage[4]
#Code example modified from:
#Christophe Keller
#Hello World in Python using Win32
# New code: Define globaL
def main():
#get instance handle
hInstance = win32api.GetModuleHandle()
# the class name
className = 'SimpleWin32'
# create and initialize window class
wndClass = win32gui.WNDCLASS()
wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW
wndClass.lpfnWndProc = wndProc
wndClass.hInstance = hInstance
wndClass.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW)
wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH)
wndClass.lpszClassName = className
# register window class
wndClassAtom = None
try:
wndClassAtom = win32gui.RegisterClass(wndClass)
except Exception as e:
print (e)
raise e
exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED |
win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT
style = win32con.WS_DISABLED | win32con.WS_POPUP | win32con.WS_VISIBLE
hWindow = win32gui.CreateWindowEx(
exStyle,
wndClassAtom,
None, # WindowName
style,
20, # x
900, # y
1920, # width
600, # height
None, # hWndParent
None, # hMenu
hInstance,
None # lpParam
)
# Show & update the window
win32gui.SetLayeredWindowAttributes(hWindow, 0x00ffffff, 255,
win32con.LWA_COLORKEY | win32con.LWA_ALPHA)
win32gui.SetWindowPos(hWindow, win32con.HWND_TOPMOST, 0, 0, 0, 0,
win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE |
win32con.SWP_SHOWWINDOW)
win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL)
win32gui.UpdateWindow(hWindow)
thr = threading.Thread(target=customDraw, args=(hWindow,))
thr.setDaemon(False)
thr.start()
# Dispatch messages
win32gui.PumpMessages()
def customDraw(hWindow):
win32gui.RedrawWindow(hWindow, None, None, win32con.RDW_INVALIDATE |
win32con.RDW_ERASE)
queue(("Dave: ", "Daves message was important"))
queue(("Chris: ", "Chris is asleep again"))
queue(("Suzy: ", "Suzy has had way to much cake"))
queue(("Sarah: ", "Sarah is shockingly beautiful"))
queue(("Steve: ", "Steve likes to eat dog treats"))
def wndProc(hWnd, message, wParam, lParam):
textFormat = win32con.DT_NOCLIP | win32con.DT_VCENTER | win32con.DT_EXPANDTABS
if message == win32con.WM_PAINT:
hDC, paintStruct = win32gui.BeginPaint(hWnd)
fontSize = 20
lf = win32gui.LOGFONT()
lf.lfFaceName = "Times New Roman"
lf.lfHeight = fontSize
lf.lfWeight = 300
lf.lfQuality = win32con.NONANTIALIASED_QUALITY
hf = win32gui.CreateFontIndirect(lf)
win32gui.SelectObject(hDC, hf)
if len(userAndMessage) > 4:
win32gui.SetTextColor(hDC,win32api.RGB(255,0,0))
rect = win32gui.GetClientRect(hWnd)
drawRect = win32gui.DrawText(hDC,userAndMessage[0][0],-1, rect,
win32con.DT_CALCRECT);
win32gui.DrawText(hDC, userAndMessage[0][0], -1, rect, textFormat)
win32gui.SetTextColor(hDC,win32api.RGB(240,240,240))
drawrect = win32gui.DrawText(hDC, userAndMessage[0][1], -1, rect,
win32con.DT_CALCRECT);
rect = (drawRect[1][0] + drawRect[1][2], drawRect[1][1], drawRect[1][2],
drawRect[1][3])
win32gui.DrawText(hDC, userAndMessage[0][1], -1, rect, textFormat)
#####################################################################################
win32gui.SetTextColor(hDC,win32api.RGB(255,0,0))
rect = (0, drawRect[1][1] + drawRect[1][3], drawRect[1][2], drawRect[1]
[3])
drawRect = win32gui.DrawText(hDC,userAndMessage[1][0],-1, rect,
win32con.DT_CALCRECT);
win32gui.DrawText(hDC, userAndMessage[1][0], -1, rect, textFormat)
win32gui.SetTextColor(hDC,win32api.RGB(240,240,240))
drawrect = win32gui.DrawText(hDC, userAndMessage[1][1], -1, rect,
win32con.DT_CALCRECT);
rect = (drawRect[1][0] + drawRect[1][2], drawRect[1][1], drawRect[1][2],
drawRect[1][3])
win32gui.DrawText(hDC, userAndMessage[1][1], -1, rect, textFormat)
#####################################################################################
win32gui.SetTextColor(hDC,win32api.RGB(255,0,0))
rect = (0, drawRect[1][1] + (drawRect[1][3] // 2), drawRect[1][2],
drawRect[1][3])
drawRect = win32gui.DrawText(hDC,userAndMessage[2][0],-1, rect,
win32con.DT_CALCRECT);
win32gui.DrawText(hDC, userAndMessage[2][0], -1, rect, textFormat)
win32gui.SetTextColor(hDC,win32api.RGB(240,240,240))
drawrect = win32gui.DrawText(hDC, userAndMessage[2][1], -1, rect,
win32con.DT_CALCRECT);
rect = (drawRect[1][0] + drawRect[1][2], drawRect[1][1], drawRect[1][2],
drawRect[1][3])
win32gui.DrawText(hDC, userAndMessage[2][1], -1, rect, textFormat)
#####################################################################################
win32gui.SetTextColor(hDC,win32api.RGB(255,0,0))
rect = (0, drawRect[1][1] + (drawRect[1][3] // 3), drawRect[1][2],
drawRect[1][3])
drawRect = win32gui.DrawText(hDC,userAndMessage[3][0],-1, rect,
win32con.DT_CALCRECT);
win32gui.DrawText(hDC, userAndMessage[3][0], -1, rect, textFormat)
win32gui.SetTextColor(hDC,win32api.RGB(240,240,240))
drawrect = win32gui.DrawText(hDC, userAndMessage[3][1], -1, rect,
win32con.DT_CALCRECT);
rect = (drawRect[1][0] + drawRect[1][2], drawRect[1][1], drawRect[1][2],
drawRect[1][3])
win32gui.DrawText(hDC, userAndMessage[3][1], -1, rect, textFormat)
#####################################################################################
win32gui.SetTextColor(hDC,win32api.RGB(255,0,0))
rect = (0, drawRect[1][1] + (drawRect[1][3] // 4), drawRect[1][2],
drawRect[1][3])
drawRect = win32gui.DrawText(hDC,userAndMessage[4][0],-1, rect,
win32con.DT_CALCRECT);
win32gui.DrawText(hDC, userAndMessage[4][0], -1, rect, textFormat)
win32gui.SetTextColor(hDC,win32api.RGB(240,240,240))
drawrect = win32gui.DrawText(hDC, userAndMessage[4][1], -1, rect,
win32con.DT_CALCRECT);
rect = (drawRect[1][0] + drawRect[1][2], drawRect[1][1], drawRect[1][2],
drawRect[1][3])
win32gui.DrawText(hDC, userAndMessage[4][1], -1, rect, textFormat)
win32gui.EndPaint(hWnd, paintStruct)
return 0
elif message == win32con.WM_DESTROY:
print('Being destroyed')
win32gui.PostQuitMessage(0)
return 0
else:
return win32gui.DefWindowProc(hWnd, message, wParam, lParam)
if __name__ == '__main__':
main()
再次感谢,乌龟