嵌入外部窗口(ing)
This commit is contained in:
parent
5ad94f20be
commit
80fb7ef2ce
2 changed files with 94 additions and 8 deletions
|
@ -1,8 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?eclipse-pydev version="1.0"?><pydev_project>
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Python35</pydev_property>
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.0</pydev_property>
|
||||
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
|
||||
<path>/${PROJECT_DIR_NAME}</path>
|
||||
</pydev_pathproperty>
|
||||
</pydev_project>
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?eclipse-pydev version="1.0"?><pydev_project>
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.0</pydev_property>
|
||||
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
|
||||
<path>/${PROJECT_DIR_NAME}</path>
|
||||
</pydev_pathproperty>
|
||||
</pydev_project>
|
||||
|
|
86
嵌入外部窗口/EmbedWidget.py
Normal file
86
嵌入外部窗口/EmbedWidget.py
Normal file
|
@ -0,0 +1,86 @@
|
|||
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('获取所有可用、可视窗口', self,
|
||||
clicked=self._getWindowList, maximumHeight=30))
|
||||
layout.addWidget(
|
||||
QLabel('双击列表中的项目则进行嵌入目标窗口到下方\n格式为:句柄|父句柄|标题|类名', 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):
|
||||
"""列表双击选择事件"""
|
||||
# 先移除掉item
|
||||
self.windowList.takeItem(self.windowList.indexFromItem(item).row())
|
||||
hwnd, phwnd, _, _ = item.text().split('|')
|
||||
# 开始嵌入
|
||||
|
||||
if self.layout().count() == 4:
|
||||
# 如果数量等于4说明之前已经嵌入了一个窗口,现在需要把它释放出来
|
||||
self.restore()
|
||||
hwnd, phwnd = int(hwnd), int(phwnd)
|
||||
widget = QWidget.createWindowContainer(QWindow.fromWinId(hwnd))
|
||||
widget.hwnd = hwnd # 窗口句柄
|
||||
widget.phwnd = phwnd # 父窗口句柄
|
||||
widget.style = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE) # 窗口样式
|
||||
widget.exstyle = win32gui.GetWindowLong(
|
||||
hwnd, win32con.GWL_EXSTYLE) # 窗口额外样式
|
||||
self.layout().addWidget(widget)
|
||||
|
||||
def restore(self):
|
||||
"""归还窗口"""
|
||||
widget = self.layout().itemAt(3).widget()
|
||||
win32gui.SetParent(widget.hwnd, widget.phwnd) # 让它返回它的父窗口
|
||||
win32gui.SetWindowLong(
|
||||
widget.hwnd, win32con.GWL_STYLE, widget.style) # 恢复样式
|
||||
win32gui.SetWindowLong(
|
||||
widget.hwnd, win32con.GWL_EXSTYLE, widget.exstyle) # 恢复样式
|
||||
win32gui.ShowWindow(widget.hwnd, win32con.SW_SHOW) # 显示窗口OF
|
||||
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_())
|
Loading…
Reference in a new issue