如何使用python for windows在所有其他窗口上方显示文本行

问题描述 投票:-3回答:1

我最初的问题与我需要完成的目标不符。回到问题后,我发现了一个可行的解决方案,如果其他用户正在寻找一种使用python for windows在屏幕上所有窗口上方显示文本的方法,则可以复制该解决方案。该代码现在可以正常运行,并且由于问题的标题已更改,因此可以为其他stackoverflow用户做出很好的贡献。已解决将代码另存为A

file.py

import win32api, win32con, win32gui, win32ui, timer, threading, time

windowText = 'Ecclesiastes'
hWindow = 0

def main():
    hInstance = win32api.GetModuleHandle()
    className = 'MyWindowClassName'
wndClass                = win32gui.WNDCLASS()
wndClass.style          = win32con.CS_HREDRAW | win32con.CS_VREDRAW
wndClass.lpfnWndProc    = wndProc
wndClass.hInstance      = hInstance
wndClass.hIcon          = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
wndClass.hCursor        = win32gui.LoadCursor(None, win32con.IDC_ARROW)
wndClass.hbrBackground  = win32gui.GetStockObject(win32con.WHITE_BRUSH)
wndClass.lpszClassName  = className
wndClassAtom = win32gui.RegisterClass(wndClass)

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,
    style,
    0, # x
    0, # y
    win32api.GetSystemMetrics(win32con.SM_CXSCREEN), # width
    win32api.GetSystemMetrics(win32con.SM_CYSCREEN), # height
    None, # hWndParent
    None, # hMenu
    hInstance,
    None # lpParam
)
    win32gui.SetLayeredWindowAttributes(hWindow, 0x00ffffff, 255, win32con.LWA_COLORKEY | win32con.LWA_ALPHA) ####### COLOR

win32gui.SetWindowPos(hWindow, win32con.HWND_TOPMOST, 0, 0, 0, 0,
    win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW)
thr = threading.Thread(target=customDraw, args=(hWindow))
thr.setDaemon(False)
thr.start()

win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL)
win32gui.UpdateWindow(hWindow)
timer.set_timer(10000, customDraw)
win32gui.PumpMessages()

counter = 0
def customDraw(timer_id, Time):
    global hWindow
    global counter
    global windowText
    if counter > 1589:
        counter = 0
    text = ["first line of text displayed over screen",
"second line of text displayed over screen",
"third line of text displayed over screen",
"fourth line of text displayed over screen",]
    windowText = text[counter]
    counter = counter + 1
    win32gui.InvalidateRect(hWindow, None, True)

def wndProc(hWnd, message, wParam, lParam):
    if message == win32con.WM_PAINT:
        hdc, paintStruct = win32gui.BeginPaint(hWnd)
        dpiScale = win32ui.GetDeviceCaps(hdc, win32con.LOGPIXELSX) / 60.0
        fontSize = 18
        lf = win32gui.LOGFONT()
        lf.lfFaceName = "Comic Sans"
        lf.lfHeight = int(round(dpiScale * fontSize))
        hf = win32gui.CreateFontIndirect(lf)
        win32gui.SelectObject(hdc, hf)
        rect = win32gui.GetClientRect(hWnd)
        win32gui.DrawText(hdc, windowText, -1, rect,
          win32con.DT_LEFT | win32con.DT_BOTTOM | win32con.DT_SINGLELINE
        )
        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)
        calrect = win32gui.DrawText(hdc, text, -1, rect, textformat | win32con.DT_CALCRECT);

        rect.top = rect.bottom - calcrect.bottom;
        win32gui.DrawText(hDC, text, -1, rect, textformat)

if __name__ == '__main__':
    main()

要运行该文件,可以轻松地批量运行此脚本。

@echo off
python c:\users\7\desktop\file.py

另存为startscript.bat

为了使python文件在每次使用此代码时都无需打开窗口即可运行,最好的解决方案是使用pyinstaller将> file.py打包为.exe。

安装pyinstaller:>

pip install pyinstaller

然后cd到您的file.py的位置运行此命令

pyinstaller --onefile --windowed --icon=youricon.ico file.py

将'youricon'更改为所需的新.exe文件的图标名称。

python windows
1个回答
0
投票

它说customDraw()要求其参数为TrueFalseNone。另外,您还应该import time

© www.soinside.com 2019 - 2024. All rights reserved.