|
1 | 1 | # from fbs_runtime.application_context.PyQt5 import ApplicationContext |
| 2 | +import time |
2 | 3 | from PyQt5 import QtWidgets |
3 | 4 | from PyQt5 import QtCore |
| 5 | +from PyQt5.QtWidgets import QApplication,QMessageBox |
| 6 | +from PyQt5.QtNetwork import QLocalSocket,QLocalServer |
4 | 7 | import sys, os |
5 | 8 | import mainWindowGUI as mainWindowGUI |
6 | 9 | import mineSweeperGUI as mineSweeperGUI |
7 | 10 | import ctypes |
8 | 11 | from ctypes import wintypes |
9 | | - |
10 | 12 | os.environ["QT_FONT_DPI"] = "96" |
11 | 13 |
|
| 14 | +def on_new_connection(localServer:QLocalServer): |
| 15 | + """当新连接进来时,接受连接并将文件路径传递给主窗口""" |
| 16 | + socket = localServer.nextPendingConnection() |
| 17 | + if socket: |
| 18 | + socket.readyRead.connect(lambda: on_ready_read(socket)) |
| 19 | + |
| 20 | +def on_ready_read(socket:QLocalSocket): |
| 21 | + """从socket读取文件路径并传递给主窗口""" |
| 22 | + if socket and socket.state() == QLocalSocket.ConnectedState: |
| 23 | + # 读取文件路径并调用打开文件 |
| 24 | + socket.waitForReadyRead(500) |
| 25 | + file_path = socket.readAll().data().decode() |
| 26 | + for win in QApplication.topLevelWidgets(): |
| 27 | + if isinstance(win, mainWindowGUI.MainWindow): |
| 28 | + win.dropFileSignal.emit(file_path) |
| 29 | + socket.disconnectFromServer() # 断开连接 |
| 30 | + |
| 31 | + |
12 | 32 |
|
13 | 33 | def find_window(class_name, window_name): |
14 | 34 | """ |
@@ -37,26 +57,38 @@ def find_window(class_name, window_name): |
37 | 57 |
|
38 | 58 | if __name__ == "__main__": |
39 | 59 | # QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) |
40 | | - |
41 | | - app = QtWidgets.QApplication (sys.argv) |
42 | | - mainWindow = mainWindowGUI.MainWindow() |
43 | | - ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv) |
44 | | - ui.mainWindow.show() |
45 | | - ui.mainWindow.game_setting_path = ui.game_setting_path |
46 | | - |
47 | | - _translate = QtCore.QCoreApplication.translate |
48 | | - hwnd = find_window(None, _translate("MainWindow", "元扫雷")) |
49 | | - |
50 | | - SetWindowDisplayAffinity = ctypes.windll.user32.SetWindowDisplayAffinity |
51 | | - ui.disable_screenshot = lambda: ... if SetWindowDisplayAffinity(hwnd, 0x00000011) else 1/0 |
52 | | - ui.enable_screenshot = lambda: ... if SetWindowDisplayAffinity(hwnd, 0x00000000) else 1/0 |
53 | | - |
54 | | - |
55 | | - |
56 | | - |
57 | | - sys.exit(app.exec_()) |
58 | | - ... |
59 | | - |
| 60 | + try: |
| 61 | + app = QtWidgets.QApplication (sys.argv) |
| 62 | + serverName = "MineSweeperServer" |
| 63 | + socket = QLocalSocket() |
| 64 | + socket.connectToServer(serverName) |
| 65 | + if socket.waitForConnected(500): |
| 66 | + if len(sys.argv) == 2: |
| 67 | + filePath = sys.argv[1] |
| 68 | + socket.write(filePath.encode()) |
| 69 | + socket.flush() |
| 70 | + time.sleep(0.5) |
| 71 | + app.quit() |
| 72 | + else: |
| 73 | + localServer = QLocalServer() |
| 74 | + localServer.listen(serverName) |
| 75 | + localServer.newConnection.connect(lambda: on_new_connection(localServer=localServer)) |
| 76 | + mainWindow = mainWindowGUI.MainWindow() |
| 77 | + ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv) |
| 78 | + ui.mainWindow.show() |
| 79 | + ui.mainWindow.game_setting_path = ui.game_setting_path |
| 80 | + |
| 81 | + _translate = QtCore.QCoreApplication.translate |
| 82 | + hwnd = find_window(None, _translate("MainWindow", "元扫雷")) |
| 83 | + |
| 84 | + SetWindowDisplayAffinity = ctypes.windll.user32.SetWindowDisplayAffinity |
| 85 | + ui.disable_screenshot = lambda: ... if SetWindowDisplayAffinity(hwnd, 0x00000011) else 1/0 |
| 86 | + ui.enable_screenshot = lambda: ... if SetWindowDisplayAffinity(hwnd, 0x00000000) else 1/0 |
| 87 | + |
| 88 | + sys.exit(app.exec_()) |
| 89 | + ... |
| 90 | + except Exception as e: |
| 91 | + pass |
60 | 92 |
|
61 | 93 | # 最高优先级 |
62 | 94 | # 计时器快捷键切换 |
|
0 commit comments