Qt5全屏时强制保持在顶部

2024-09-27 00:14:51 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试使用Qt编写一个应用程序,该应用程序将在屏幕上放置水印。 使用下面的标志可以使我的窗口显示在所有内容之上,除非用户在Windows照片中使用全屏模式

self.setWindowFlags(
        Qt.WindowTransparentForInput | Qt.WindowStaysOnTopHint |
        Qt.FramelessWindowHint | Qt.Tool | Qt.MaximizeUsingFullscreenGeometryHint)

在上述情况下,是否有可能强制车窗保持在顶部?i、 e.使用user32而不重写不同框架的所有内容


Tags: 用户self应用程序内容屏幕标志windows模式
1条回答
网友
1楼 · 发布于 2024-09-27 00:14:51

在windows上,您可以使用gdi32user32在桌面上绘制,如果您只需要水印,您可以尝试这种方法

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWebEngineWidgets import QWebEngineView

from ctypes.wintypes import HDC, HWND, RECT

from ctypes import windll

GetDC = windll.user32.GetDC
ReleaseDC = windll.user32.ReleaseDC
SetTextColor = windll.gdi32.SetTextColor
DrawText = windll.user32.DrawTextW

class Watermark(QtCore.QObject):
    def __init__(self, parent = None):
        super().__init__(parent)
        self._text = None
        self._visible = False
        timer = QtCore.QTimer()
        timer.setInterval(10)
        timer.timeout.connect(self.onTimeout)
        timer.start()
        self._timer = timer
        
    def setText(self, text):
        self._text = text

    def show(self):
        self._visible = True
    
    def onTimeout(self):
        if not self._visible or self._text is None:
            return
        rect = RECT()
        hwnd = 0
        hdc = GetDC(hwnd)
        rect.left = 0
        rect.top = 0
        rect.bottom = 100
        rect.right = 100
        SetTextColor(hdc, 0x00000000)
        DT_SINGLELINE = 0x00000020
        DT_NOCLIP = 0x00000100
        DrawText(hdc, self._text, -1, rect, DT_SINGLELINE | DT_NOCLIP) 
        ReleaseDC(hwnd, hdc)

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    watermark = Watermark()
    watermark.setText("This stays on top")
    watermark.show()
    app.exec()

相关问题 更多 >

    热门问题