Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80fb7ef

Browse files
AdministratorAdministrator
Administrator
authored and
Administrator
committed
嵌入外部窗口(ing)
1 parent 5ad94f2 commit 80fb7ef

File tree

2 files changed

+94
-8
lines changed

2 files changed

+94
-8
lines changed

‎.pydevproject‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<?eclipse-pydev version="1.0"?><pydev_project>
3-
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Python35</pydev_property>
4-
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.0</pydev_property>
5-
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
6-
<path>/${PROJECT_DIR_NAME}</path>
7-
</pydev_pathproperty>
8-
</pydev_project>
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
4+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.0</pydev_property>
5+
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
6+
<path>/${PROJECT_DIR_NAME}</path>
7+
</pydev_pathproperty>
8+
</pydev_project>

‎嵌入外部窗口/EmbedWidget.py‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from PyQt5.QtGui import QWindow
2+
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QListWidget,\
3+
QLabel
4+
import win32con
5+
import win32gui
6+
7+
8+
class Window(QWidget):
9+
10+
def __init__(self, *args, **kwargs):
11+
super(Window, self).__init__(*args, **kwargs)
12+
self.resize(800, 600)
13+
layout = QVBoxLayout(self)
14+
15+
self.myhwnd = int(self.winId()) # 自己的句柄
16+
17+
layout.addWidget(QPushButton('获取所有可用、可视窗口', self,
18+
clicked=self._getWindowList, maximumHeight=30))
19+
layout.addWidget(
20+
QLabel('双击列表中的项目则进行嵌入目标窗口到下方\n格式为:句柄|父句柄|标题|类名', self, maximumHeight=30))
21+
self.windowList = QListWidget(
22+
self, itemDoubleClicked=self.onItemDoubleClicked, maximumHeight=200)
23+
layout.addWidget(self.windowList)
24+
25+
def closeEvent(self, event):
26+
"""窗口关闭"""
27+
if self.layout().count() == 4:
28+
self.restore()
29+
super(Window, self).closeEvent(event)
30+
31+
def _getWindowList(self):
32+
"""清空原来的列表"""
33+
self.windowList.clear()
34+
win32gui.EnumWindows(self._enumWindows, None)
35+
36+
def onItemDoubleClicked(self, item):
37+
"""列表双击选择事件"""
38+
# 先移除掉item
39+
self.windowList.takeItem(self.windowList.indexFromItem(item).row())
40+
hwnd, phwnd, _, _ = item.text().split('|')
41+
# 开始嵌入
42+
43+
if self.layout().count() == 4:
44+
# 如果数量等于4说明之前已经嵌入了一个窗口,现在需要把它释放出来
45+
self.restore()
46+
hwnd, phwnd = int(hwnd), int(phwnd)
47+
widget = QWidget.createWindowContainer(QWindow.fromWinId(hwnd))
48+
widget.hwnd = hwnd # 窗口句柄
49+
widget.phwnd = phwnd # 父窗口句柄
50+
widget.style = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE) # 窗口样式
51+
widget.exstyle = win32gui.GetWindowLong(
52+
hwnd, win32con.GWL_EXSTYLE) # 窗口额外样式
53+
self.layout().addWidget(widget)
54+
55+
def restore(self):
56+
"""归还窗口"""
57+
widget = self.layout().itemAt(3).widget()
58+
win32gui.SetParent(widget.hwnd, widget.phwnd) # 让它返回它的父窗口
59+
win32gui.SetWindowLong(
60+
widget.hwnd, win32con.GWL_STYLE, widget.style) # 恢复样式
61+
win32gui.SetWindowLong(
62+
widget.hwnd, win32con.GWL_EXSTYLE, widget.exstyle) # 恢复样式
63+
win32gui.ShowWindow(widget.hwnd, win32con.SW_SHOW) # 显示窗口OF
64+
widget.close()
65+
self.layout().removeWidget(widget) # 从布局中移出
66+
widget.deleteLater()
67+
68+
def _enumWindows(self, hwnd, _):
69+
"""遍历回调函数"""
70+
if hwnd == self.myhwnd:
71+
return # 防止自己嵌入自己
72+
if win32gui.IsWindow(hwnd) and win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
73+
phwnd = win32gui.GetParent(hwnd)
74+
title = win32gui.GetWindowText(hwnd)
75+
name = win32gui.GetClassName(hwnd)
76+
self.windowList.addItem(
77+
'{0}|{1}|\t标题:{2}\t|\t类名:{3}'.format(hwnd, phwnd, title, name))
78+
79+
80+
if __name__ == '__main__':
81+
import sys
82+
from PyQt5.QtWidgets import QApplication
83+
app = QApplication(sys.argv)
84+
w = Window()
85+
w.show()
86+
sys.exit(app.exec_())

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /