0

所以我发现了这个例子,它真的很酷,我想在应用程序中做类似的事情。我发现的问题是,是的,您可以嵌入窗口,是的,您可以单击内容,但无法在窗口中输入内容。因此,例如,如果我打开 chrome 或边缘窗口并选择将其嵌入,我将无法在嵌入式浏览器中键入内容。

from PyQt5.QtGui import QWindow
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QListWidget, QLabel
import win32con
import win32gui


class Window(QWidget):

    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)
        self.resize(800, 600)
        layout = QVBoxLayout(self)

        self.myhwnd = int(self.winId())

        layout.addWidget(QPushButton('get windows', self,clicked=self._getWindowList, maximumHeight=30))
        layout.addWidget(QLabel('load', self, maximumHeight=30))
        self.windowList = QListWidget(self, itemDoubleClicked=self.onItemDoubleClicked, maximumHeight=200)
        layout.addWidget(self.windowList)

    def closeEvent(self, event):
        if self.layout().count() == 4:
            self.restore()
        super(Window, self).closeEvent(event)

    def _getWindowList(self):
        self.windowList.clear()
        win32gui.EnumWindows(self._enumWindows, None)

    def onItemDoubleClicked(self, item):
        self.windowList.takeItem(self.windowList.indexFromItem(item).row())
        hwnd, phwnd, _, _ = item.text().split('|')

        if self.layout().count() == 4:
            self.restore()
        hwnd, phwnd = int(hwnd), int(phwnd)
        style = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE)
        exstyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
        print('save', hwnd, style, exstyle)

        widget = QWidget.createWindowContainer(QWindow.fromWinId(hwnd))
        widget.hwnd = hwnd
        widget.phwnd = phwnd
        widget.style = style
        widget.exstyle = exstyle
        self.layout().addWidget(widget)

    def restore(self):
        widget = self.layout().itemAt(3).widget()
        print('restore', widget.hwnd, widget.style, widget.exstyle)
        win32gui.SetParent(widget.hwnd, widget.phwnd)
        win32gui.SetWindowLong( widget.hwnd, win32con.GWL_STYLE, widget.style | win32con.WS_VISIBLE)
        win32gui.SetWindowLong( widget.hwnd, win32con.GWL_EXSTYLE, widget.exstyle)
        win32gui.ShowWindow( widget.hwnd, win32con.SW_SHOW)
        widget.close()
        self.layout().removeWidget(widget)
        widget.deleteLater()

    def _enumWindows(self, hwnd, _):
        if hwnd == self.myhwnd:
            return
        if win32gui.IsWindow(hwnd) and win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
            phwnd = win32gui.GetParent(hwnd)
            title = win32gui.GetWindowText(hwnd)
            name = win32gui.GetClassName(hwnd)
            self.windowList.addItem('{0}|{1}|\t{2}\t|\t{3}'.format(hwnd, phwnd, title, name))


if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

现在我的问题是如何将焦点更改为键盘的嵌入式窗口?当鼠标在窗口中时,键盘仍然聚焦在父窗口中。

4

0 回答 0