-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathchrome_manager.py
More file actions
5019 lines (4176 loc) · 232 KB
/
chrome_manager.py
File metadata and controls
5019 lines (4176 loc) · 232 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import os
import subprocess
import win32gui
import win32process
import win32con
import win32api
import win32com.client
import json
import requests
from typing import List, Dict, Optional
import math
import ctypes
from ctypes import wintypes
import threading
import time
import sys
import keyboard
import mouse
import webbrowser
import sv_ttk
import win32security
# 添加通知错误处理
try:
from win11toast import notify, toast
except ImportError:
# 简单的空函数替代
def toast(title, message, **kwargs):
pass
def notify(title, message, **kwargs):
pass
import re
import socket
import traceback
import wmi
import pythoncom # 添加pythoncom导入
import concurrent.futures
import random
# 添加滚轮钩子所需的结构体定义
class POINT(ctypes.Structure):
_fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
class MSLLHOOKSTRUCT(ctypes.Structure):
_fields_ = [
("pt", POINT),
("mouseData", wintypes.DWORD),
("flags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", ctypes.POINTER(wintypes.ULONG))
]
def is_admin():
# 检查是否具有管理员权限
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def run_as_admin():
# 以管理员权限重新运行程序
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
class ChromeManager:
def __init__(self):
"""初始化"""
# 记录启动时间用于性能分析
self.start_time = time.time()
# 默认设置值
self.show_chrome_tip = True # 是否显示Chrome后台运行提示
# 加载设置
self.settings = self.load_settings()
self.enable_cdp = True # 始终开启CDP
# 从设置中读取是否显示Chrome提示的设置
if 'show_chrome_tip' in self.settings:
self.show_chrome_tip = self.settings['show_chrome_tip']
# 滚轮钩子相关参数
self.wheel_hook_id = None
self.wheel_hook_proc = None
self.standard_wheel_delta = 120 # 标准滚轮增量值
self.last_wheel_time = 0
self.wheel_threshold = 0.05 # 秒,防止事件触发过于频繁
self.use_wheel_hook = True # 是否使用滚轮钩子
# 存储快捷方式编号和进程ID的映射关系
self.shortcut_to_pid = {}
# 存储进程ID和窗口编号的映射关系
self.pid_to_number = {}
if not is_admin():
if messagebox.askyesno("权限不足", "需要管理员权限才能运行同步功能。\n是否以管理员身份重新启动程序?"):
run_as_admin()
sys.exit()
# 确保settings.json文件存在
if not os.path.exists('settings.json'):
with open('settings.json', 'w', encoding='utf-8') as f:
json.dump({}, f, ensure_ascii=False, indent=4)
self.root = tk.Tk()
self.root.title("NoBiggie社区Chrome多窗口管理器 V2.0")
# 先隐藏主窗口,避免闪烁
self.root.withdraw()
# 随机数字输入相关配置 - 移动到root创建之后
self.random_min_value = tk.StringVar(value="1000")
self.random_max_value = tk.StringVar(value="2000")
self.random_overwrite = tk.BooleanVar(value=True)
self.random_delayed = tk.BooleanVar(value=False)
try:
icon_path = os.path.join(os.path.dirname(__file__), "app.ico")
if os.path.exists(icon_path):
self.root.iconbitmap(icon_path)
except Exception as e:
print(f"设置图标失败: {str(e)}")
# 设置固定的窗口大小
self.window_width = 700
self.window_height = 360
self.root.geometry(f"{self.window_width}x{self.window_height}")
self.root.resizable(False, False)
# 设置关闭事件处理
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
# 加载主题
sv_ttk.set_theme("light")
print(f"[{time.time() - self.start_time:.3f}s] 主题加载完成")
# 仅保存/加载窗口位置,不包括大小
last_position = self.load_window_position()
if last_position:
try:
# 直接使用返回的位置信息
self.root.geometry(f"{self.window_width}x{self.window_height}{last_position}")
except Exception as e:
print(f"应用窗口位置时出错: {e}")
self.window_list = None
self.windows = []
self.master_window = None
self.screens = [] # 初始化屏幕列表
# 从设置加载所有路径
self.shortcut_path = self.settings.get('shortcut_path', '')
self.cache_dir = self.settings.get('cache_dir', '')
self.icon_dir = self.settings.get('icon_dir', '')
self.screen_selection = self.settings.get('screen_selection', '')
print("初始化加载设置:", self.settings) # 调试输出
self.path_entry = None
# 初始化快捷键相关属性
self.shortcut_hook = None
self.current_shortcut = self.settings.get('sync_shortcut', None)
if self.current_shortcut:
self.set_shortcut(self.current_shortcut)
self.shell = win32com.client.Dispatch("WScript.Shell")
self.select_all_var = tk.StringVar(value="全部选择")
self.is_syncing = False
self.sync_button = None
self.mouse_hook_id = None
self.keyboard_hook = None
self.hook_thread = None
self.user32 = ctypes.WinDLL('user32', use_last_error=True)
self.sync_windows = []
self.chrome_drivers = {}
# 调试端口映射 - 将窗口号映射到调试端口
self.debug_ports = {}
# 基础调试端口
self.base_debug_port = 9222
self.DWMWA_BORDER_COLOR = 34
self.DWM_MAGIC_COLOR = 0x00FF0000
self.popup_mappings = {}
self.popup_monitor_thread = None
self.mouse_threshold = 3
self.last_mouse_position = (0, 0)
self.last_move_time = 0
self.move_interval = 0.016
# 创建样式
self.create_styles()
# 创建界面
self.create_widgets()
# 更新树形视图样式
self.update_treeview_style()
# 窗口尺寸已在初始化时固定,无需再次调整
# 在初始化时设置进程缓解策略
PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON = 0x100000000000
ctypes.windll.kernel32.SetProcessMitigationPolicy(
0, # ProcessSignaturePolicy
ctypes.byref(ctypes.c_ulonglong(PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON)),
ctypes.sizeof(ctypes.c_ulonglong)
)
# 检测Windows版本
self.win_ver = sys.getwindowsversion()
self.is_win11 = self.win_ver.build >= 22000
# 初始化系统托盘通知
try:
if self.is_win11:
# Windows 11使用toast通知
self.notify_func = toast
else:
# Windows 10使用win32gui通知
self.hwnd = win32gui.GetForegroundWindow()
self.notification_flags = win32gui.NIF_ICON | win32gui.NIF_INFO | win32gui.NIF_TIP
# 加载app.ico图标
try:
icon_path = os.path.join(os.path.dirname(__file__), "app.ico")
if os.path.exists(icon_path):
# 加载应用程序图标
icon_handle = win32gui.LoadImage(
0, icon_path, win32con.IMAGE_ICON,
0, 0, win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
)
else:
# 使用默认图标
icon_handle = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
except Exception as e:
print(f"加载托盘图标失败: {str(e)}")
icon_handle = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
self.notify_id = (
self.hwnd,
0,
self.notification_flags,
win32con.WM_USER + 20,
icon_handle,
"Chrome多窗口管理器"
)
# 先注册托盘图标
try:
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, self.notify_id)
except Exception as e:
print(f"注册托盘图标失败: {str(e)}")
except Exception as e:
print(f"初始化通知功能失败: {str(e)}")
# 创建右键菜单
self.context_menu = tk.Menu(self.root, tearoff=0)
self.context_menu.add_command(label="剪切", command=self.cut_text)
self.context_menu.add_command(label="复制", command=self.copy_text)
self.context_menu.add_command(label="粘贴", command=self.paste_text)
self.context_menu.add_separator()
self.context_menu.add_command(label="全选", command=self.select_all_text)
# 保存当前焦点的文本框引用
self.current_text_widget = None
# 添加CDP WebSocket连接池
#self.ws_connections = {}
#self.ws_lock = threading.Lock()
#self.scroll_sync_enabled = True # 添加滚轮同步控制标志
# 安排延迟初始化
self.root.after(100, self.delayed_initialization)
print(f"[{time.time() - self.start_time:.3f}s] __init__ 完成, 已安排延迟初始化")
def create_styles(self):
style = ttk.Style()
default_font = ('Microsoft YaHei UI', 9)
style.configure('Small.TEntry',
padding=(4, 0),
font=default_font
)
style.configure('TButton', font=default_font)
style.configure('TLabel', font=default_font)
style.configure('TEntry', font=default_font)
style.configure('Treeview', font=default_font)
style.configure('Treeview.Heading', font=default_font)
style.configure('TLabelframe.Label', font=default_font)
style.configure('TNotebook.Tab', font=default_font)
# 链接样式
style.configure('Link.TLabel',
foreground='#0d6efd',
cursor='hand2',
font=('Microsoft YaHei UI', 9, 'underline')
)
def update_treeview_style(self):
"""更新Treeview组件的样式,此方法应在window_list初始化后调用"""
if self.window_list:
self.window_list.tag_configure("master",
background="#0d6efd",
foreground="white")
def create_widgets(self):
"""创建界面元素"""
main_frame = ttk.Frame(self.root)
main_frame.pack(fill=tk.X, padx=10, pady=5)
upper_frame = ttk.Frame(main_frame)
upper_frame.pack(fill=tk.X)
arrange_frame = ttk.LabelFrame(upper_frame, text="自定义排列")
arrange_frame.pack(side=tk.RIGHT, fill=tk.Y, padx=(3, 0))
manage_frame = ttk.LabelFrame(upper_frame, text="窗口管理")
manage_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 5))
# 创建两行按钮区域
button_rows = ttk.Frame(manage_frame)
button_rows.pack(fill=tk.X)
# 第一行:基本操作按钮
first_row = ttk.Frame(button_rows)
first_row.pack(fill=tk.X)
ttk.Button(first_row, text="导入窗口", command=self.import_windows, style='Accent.TButton').pack(side=tk.LEFT, padx=2)
select_all_label = ttk.Label(first_row, textvariable=self.select_all_var, style='Link.TLabel')
select_all_label.pack(side=tk.LEFT, padx=5)
select_all_label.bind('<Button-1>', self.toggle_select_all)
ttk.Button(first_row, text="自动排列", command=self.auto_arrange_windows).pack(side=tk.LEFT, padx=2)
ttk.Button(first_row, text="关闭选中", command=self.close_selected_windows).pack(side=tk.LEFT, padx=2)
self.sync_button = ttk.Button(
first_row,
text="▶ 开始同步",
command=self.toggle_sync,
style='Accent.TButton'
)
self.sync_button.pack(side=tk.LEFT, padx=5)
# 添加设置按钮
ttk.Button(
first_row,
text="🔗 设置",
command=self.show_settings_dialog,
width=8
).pack(side=tk.LEFT, padx=2)
list_frame = ttk.Frame(manage_frame)
list_frame.pack(fill=tk.BOTH, expand=True, pady=(2, 0))
# 创建窗口列表
self.window_list = ttk.Treeview(list_frame,
columns=("select", "number", "title", "master", "hwnd"),
show="headings",
height=4,
style='Accent.Treeview'
)
self.window_list.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.window_list.heading("select", text="选择")
self.window_list.heading("number", text="窗口序号")
self.window_list.heading("title", text="页面标题")
self.window_list.heading("master", text="主控")
self.window_list.heading("hwnd", text="")
self.window_list.column("select", width=50, anchor="center")
self.window_list.column("number", width=60, anchor="center")
self.window_list.column("title", width=260)
self.window_list.column("master", width=50, anchor="center")
self.window_list.column("hwnd", width=0, stretch=False) # 隐藏hwnd列
self.window_list.tag_configure("master", background="lightblue")
self.window_list.bind('<Button-1>', self.on_click)
# 添加右键菜单功能
self.window_list_menu = tk.Menu(self.root, tearoff=0)
self.window_list_menu.add_command(label="关闭此窗口", command=self.close_selected_window)
self.window_list.bind('<Button-3>', self.show_window_list_menu)
# 添加滚动条
scrollbar = ttk.Scrollbar(list_frame, orient=tk.VERTICAL, command=self.window_list.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.window_list.configure(yscrollcommand=scrollbar.set)
params_frame = ttk.Frame(arrange_frame)
params_frame.pack(fill=tk.X, padx=5, pady=2)
left_frame = ttk.Frame(params_frame)
left_frame.pack(side=tk.LEFT, padx=(0, 5))
right_frame = ttk.Frame(params_frame)
right_frame.pack(side=tk.LEFT)
ttk.Label(left_frame, text="起始X坐标").pack(anchor=tk.W)
self.start_x = ttk.Entry(left_frame, width=8, style='Small.TEntry')
self.start_x.pack(fill=tk.X, pady=(0, 2))
self.start_x.insert(0, "0")
self.setup_right_click_menu(self.start_x)
ttk.Label(left_frame, text="窗口宽度").pack(anchor=tk.W)
self.window_width = ttk.Entry(left_frame, width=8, style='Small.TEntry')
self.window_width.pack(fill=tk.X, pady=(0, 2))
self.window_width.insert(0, "500")
self.setup_right_click_menu(self.window_width)
ttk.Label(left_frame, text="水平间距").pack(anchor=tk.W)
self.h_spacing = ttk.Entry(left_frame, width=8, style='Small.TEntry')
self.h_spacing.pack(fill=tk.X, pady=(0, 2))
self.h_spacing.insert(0, "0")
self.setup_right_click_menu(self.h_spacing)
ttk.Label(right_frame, text="起始Y坐标").pack(anchor=tk.W)
self.start_y = ttk.Entry(right_frame, width=8, style='Small.TEntry')
self.start_y.pack(fill=tk.X, pady=(0, 2))
self.start_y.insert(0, "0")
self.setup_right_click_menu(self.start_y)
ttk.Label(right_frame, text="窗口高度").pack(anchor=tk.W)
self.window_height = ttk.Entry(right_frame, width=8, style='Small.TEntry')
self.window_height.pack(fill=tk.X, pady=(0, 2))
self.window_height.insert(0, "400")
self.setup_right_click_menu(self.window_height)
ttk.Label(right_frame, text="垂直间距").pack(anchor=tk.W)
self.v_spacing = ttk.Entry(right_frame, width=8, style='Small.TEntry')
self.v_spacing.pack(fill=tk.X, pady=(0, 2))
self.v_spacing.insert(0, "0")
self.setup_right_click_menu(self.v_spacing)
for widget in left_frame.winfo_children() + right_frame.winfo_children():
if isinstance(widget, ttk.Entry):
widget.pack_configure(pady=(0, 2))
bottom_frame = ttk.Frame(arrange_frame)
bottom_frame.pack(fill=tk.X, padx=5, pady=2)
row_frame = ttk.Frame(bottom_frame)
row_frame.pack(side=tk.LEFT)
ttk.Label(row_frame, text="每行窗口数").pack(anchor=tk.W)
self.windows_per_row = ttk.Entry(row_frame, width=8, style='Small.TEntry')
self.windows_per_row.pack(pady=(2, 0))
self.windows_per_row.insert(0, "5")
self.setup_right_click_menu(self.windows_per_row)
ttk.Button(bottom_frame, text="自定义排列",
command=self.custom_arrange_windows,
style='Accent.TButton'
).pack(side=tk.RIGHT, pady=(15, 0))
bottom_frame = ttk.Frame(self.root)
bottom_frame.pack(fill=tk.X, padx=10, pady=(5, 0))
self.tab_control = ttk.Notebook(bottom_frame)
self.tab_control.pack(side=tk.LEFT, fill=tk.X, expand=True)
# 打开窗口标签
open_window_tab = ttk.Frame(self.tab_control)
self.tab_control.add(open_window_tab, text="打开窗口")
# 简化布局结构,移除多余的嵌套frame
numbers_frame = ttk.Frame(open_window_tab)
numbers_frame.pack(fill=tk.X, padx=10, pady=10) # 统一顶部边距
ttk.Label(numbers_frame, text="窗口编号:").pack(side=tk.LEFT)
self.numbers_entry = ttk.Entry(numbers_frame, width=20)
self.numbers_entry.pack(side=tk.LEFT, padx=5)
self.setup_right_click_menu(self.numbers_entry)
settings = self.load_settings()
if 'last_window_numbers' in settings:
self.numbers_entry.insert(0, settings['last_window_numbers'])
self.numbers_entry.bind('<Return>', lambda e: self.open_windows())
ttk.Button(
numbers_frame,
text="打开窗口",
command=self.open_windows,
style='Accent.TButton'
).pack(side=tk.LEFT, padx=5)
# 添加示例文字
ttk.Label(numbers_frame, text="示例: 1-5 或 1,3,5").pack(side=tk.LEFT, padx=5)
# 批量打开网页标签
url_tab = ttk.Frame(self.tab_control)
self.tab_control.add(url_tab, text="批量打开网页")
url_frame = ttk.Frame(url_tab)
url_frame.pack(fill=tk.X, padx=10, pady=10) # 统一边距
ttk.Label(url_frame, text="网址:").pack(side=tk.LEFT)
self.url_entry = ttk.Entry(url_frame, width=20)
self.url_entry.pack(side=tk.LEFT, padx=5)
self.url_entry.insert(0, "www.google.com")
self.url_entry.bind('<Return>', lambda e: self.batch_open_urls())
ttk.Button(
url_frame,
text="批量打开",
command=self.batch_open_urls,
style='Accent.TButton' # 设置蓝色风格
).pack(side=tk.LEFT, padx=5)
# 添加几个常用网站快速打开按钮
twitter_button = ttk.Button(
url_frame,
text="Twitter",
command=lambda: self.set_quick_url("https://twitter.com"),
style='Quick.TButton', # 使用自定义样式
width=8
)
twitter_button.pack(side=tk.LEFT, padx=2)
discord_button = ttk.Button(
url_frame,
text="Discord",
command=lambda: self.set_quick_url("https://discord.com/channels/@me"),
style='Quick.TButton', # 使用自定义样式
width=8
)
discord_button.pack(side=tk.LEFT, padx=2)
gmail_button = ttk.Button(
url_frame,
text="Gmail",
command=lambda: self.set_quick_url("https://mail.google.com"),
style='Quick.TButton', # 使用自定义样式
width=8
)
gmail_button.pack(side=tk.LEFT, padx=2)
# 标签页管理标签
tab_manage_tab = ttk.Frame(self.tab_control)
self.tab_control.add(tab_manage_tab, text="标签页管理")
tab_manage_frame = ttk.Frame(tab_manage_tab)
tab_manage_frame.pack(fill=tk.X, padx=10, pady=10)
ttk.Button(
tab_manage_frame,
text="仅保留当前标签页",
command=self.keep_only_current_tab,
width=20,
style='Accent.TButton' # 应用蓝色风格
).pack(side=tk.LEFT, padx=5)
ttk.Button(
tab_manage_frame,
text="仅保留新标签页",
command=self.keep_only_new_tab,
width=20,
style='Accent.TButton' # 应用蓝色风格
).pack(side=tk.LEFT, padx=5)
# 添加随机数字输入标签
random_number_tab = ttk.Frame(self.tab_control)
self.tab_control.add(random_number_tab, text="批量文本输入")
# 简化界面,只添加两个按钮
buttons_frame = ttk.Frame(random_number_tab)
buttons_frame.pack(fill=tk.X, padx=10, pady=10)
ttk.Button(
buttons_frame,
text="随机数字输入",
command=self.show_random_number_dialog,
width=20,
style='Accent.TButton' # 应用蓝色风格
).pack(side=tk.LEFT, padx=10)
ttk.Button(
buttons_frame,
text="指定文本输入",
command=self.show_text_input_dialog,
width=20,
style='Accent.TButton' # 应用蓝色风格
).pack(side=tk.LEFT, padx=10)
# 批量创建环境标签
env_create_tab = ttk.Frame(self.tab_control)
self.tab_control.add(env_create_tab, text="批量创建环境")
# 统一框架布局
input_row = ttk.Frame(env_create_tab)
input_row.pack(fill=tk.X, padx=10, pady=10) # 统一边距
# 环境编号
ttk.Label(input_row, text="创建编号:").pack(side=tk.LEFT)
self.env_numbers = ttk.Entry(input_row, width=20)
self.env_numbers.pack(side=tk.LEFT, padx=5)
self.setup_right_click_menu(self.env_numbers)
# 创建按钮
ttk.Button(
input_row,
text="开始创建",
command=self.create_environments,
style='Accent.TButton' # 设置蓝色风格
).pack(side=tk.LEFT, padx=5)
# 示例文字
ttk.Label(input_row, text="示例: 1-5,7,9-12").pack(side=tk.LEFT, padx=5)
# 替换图标标签页
icon_tab = ttk.Frame(self.tab_control)
self.tab_control.add(icon_tab, text="替换图标")
icon_frame = ttk.Frame(icon_tab)
icon_frame.pack(fill=tk.X, padx=10, pady=10) # 统一边距
ttk.Label(icon_frame, text="窗口编号:").pack(side=tk.LEFT)
self.icon_window_numbers = ttk.Entry(icon_frame, width=20)
self.icon_window_numbers.pack(side=tk.LEFT, padx=5)
ttk.Button(
icon_frame,
text="替换图标",
command=self.set_taskbar_icons,
style='Accent.TButton' # 设置蓝色风格
).pack(side=tk.LEFT, padx=5)
# 示例文字
ttk.Label(icon_frame, text="示例: 1-5,7,9-12").pack(side=tk.LEFT, padx=5)
# 底部按钮框架 - 在所有标签页设置完成后添加
footer_frame = ttk.Frame(self.root)
footer_frame.pack(side=tk.BOTTOM, fill=tk.X, padx=10, pady=5)
# 添加左侧的超链接
donate_frame = ttk.Frame(footer_frame)
donate_frame.pack(side=tk.LEFT)
donate_label = ttk.Label(
donate_frame,
text="铸造一个看上去没什么用的NFT 0.1SOL(其实就是打赏啦 😁)",
cursor="hand2",
foreground="black"
# 移除字体设置,使用系统默认字体
)
donate_label.pack(side=tk.LEFT)
donate_label.bind("<Button-1>", lambda e: webbrowser.open("https://truffle.wtf/project/Devilflasher"))
author_frame = ttk.Frame(footer_frame)
author_frame.pack(side=tk.RIGHT)
ttk.Label(author_frame, text="Compiled by Devilflasher").pack(side=tk.LEFT)
ttk.Label(author_frame, text=" ").pack(side=tk.LEFT)
twitter_label = ttk.Label(
author_frame,
text="Twitter",
cursor="hand2",
font=("Arial", 9)
)
twitter_label.pack(side=tk.LEFT)
twitter_label.bind("<Button-1>", lambda e: webbrowser.open("https://x.com/DevilflasherX"))
ttk.Label(author_frame, text=" ").pack(side=tk.LEFT)
telegram_label = ttk.Label(
author_frame,
text="Telegram",
cursor="hand2",
font=("Arial", 9)
)
telegram_label.pack(side=tk.LEFT)
telegram_label.bind("<Button-1>", lambda e: webbrowser.open("https://t.me/devilflasher0"))
def toggle_select_all(self, event=None):
#切换全选状态
try:
items = self.window_list.get_children()
if not items:
return
current_text = self.select_all_var.get()
if current_text == "全部选择":
for item in items:
self.window_list.set(item, "select", "√")
else:
for item in items:
self.window_list.set(item, "select", "")
# 更新按钮状态
self.update_select_all_status()
except Exception as e:
print(f"切换全选状态失败: {str(e)}")
def update_select_all_status(self):
# 更新全选状态
try:
# 获取所有项目
items = self.window_list.get_children()
if not items:
self.select_all_var.set("全部选择")
return
# 检查是否全部选中
selected_count = sum(1 for item in items if self.window_list.set(item, "select") == "√")
# 根据选中数量设置按钮文本
if selected_count == len(items):
self.select_all_var.set("取消全选")
else:
self.select_all_var.set("全部选择")
except Exception as e:
print(f"更新全选状态失败: {str(e)}")
def on_click(self, event):
# 处理点击事件
try:
region = self.window_list.identify_region(event.x, event.y)
if region == "cell":
column = self.window_list.identify_column(event.x)
item = self.window_list.identify_row(event.y)
if column == "#1": # 选择列
current = self.window_list.set(item, "select")
self.window_list.set(item, "select", "" if current == "√" else "√")
# 更新全选按钮状态
self.update_select_all_status()
elif column == "#4": # 主控列
self.set_master_window(item)
except Exception as e:
print(f"处理点击事件失败: {str(e)}")
def set_master_window(self, item):
"""设置主控窗口"""
try:
# 如果正在同步,先停止同步
if self.is_syncing:
self.stop_sync()
# 确保按钮状态更新
self.sync_button.configure(text="▶ 开始同步", style='Accent.TButton')
self.is_syncing = False
# 显示通知
self.show_notification("同步已关闭", "切换主控窗口,同步已停止")
# 清除其他窗口的主控状态和标题
for i in self.window_list.get_children():
values = self.window_list.item(i)['values']
if values and len(values) >= 5:
hwnd = int(values[4])
title = values[2]
# 移除所有主控标记
if "★" in title or "[主控]" in title:
new_title = title.replace("[主控]", "").strip()
new_title = new_title.replace("★", "").strip()
win32gui.SetWindowText(hwnd, new_title)
# 更新列表中显示的标题
self.window_list.set(i, "title", new_title)
# 恢复默认边框颜色
try:
# 使用 LoadLibrary 显式加载 dwmapi.dll
dwmapi = ctypes.WinDLL("dwmapi.dll")
# 定义参数类型
DWMWA_BORDER_COLOR = 34
color = ctypes.c_uint(0) # 默认颜色
# 恢复默认边框颜色
dwmapi.DwmSetWindowAttribute(
hwnd,
DWMWA_BORDER_COLOR,
ctypes.byref(color),
ctypes.sizeof(ctypes.c_int)
)
# 强制刷新窗口
win32gui.SetWindowPos(
hwnd,
0,
0, 0, 0, 0,
win32con.SWP_NOMOVE |
win32con.SWP_NOSIZE |
win32con.SWP_NOZORDER |
win32con.SWP_FRAMECHANGED
)
except Exception as e:
print(f"重置窗口边框颜色失败: {str(e)}")
self.window_list.set(i, "master", "")
self.window_list.item(i, tags=())
# 设置新的主控窗口
values = self.window_list.item(item)['values']
self.master_window = int(values[4])
# 设置主控标记和蓝色背景
self.window_list.set(item, "master", "√")
self.window_list.item(item, tags=("master",))
# 修改窗口标题和边框颜色
title = values[2]
if not "[主控]" in title and not "★" in title:
new_title = f"★ [主控] {title} ★"
win32gui.SetWindowText(self.master_window, new_title)
self.window_list.set(item, "title", new_title)
try:
# 加载 dwmapi.dll
dwmapi = ctypes.WinDLL("dwmapi.dll")
# 设置窗口边框颜色为红色
color = ctypes.c_uint(0x0000FF) # 红色 (BGR格式)
dwmapi.DwmSetWindowAttribute(
self.master_window,
34, # DWMWA_BORDER_COLOR
ctypes.byref(color),
ctypes.sizeof(ctypes.c_int)
)
# 强制刷新窗口
win32gui.SetWindowPos(
self.master_window,
0,
0, 0, 0, 0,
win32con.SWP_NOMOVE |
win32con.SWP_NOSIZE |
win32con.SWP_NOZORDER |
win32con.SWP_FRAMECHANGED
)
except Exception as e:
print(f"设置主控窗口边框颜色失败: {str(e)}")
except Exception as e:
print(f"设置主控窗口失败: {str(e)}")
def toggle_sync(self, event=None):
# 切换同步状态
if not self.window_list.get_children():
messagebox.showinfo("提示", "请先导入窗口!")
return
# 获取选中的窗口
selected = []
for item in self.window_list.get_children():
if self.window_list.set(item, "select") == "√":
selected.append(item)
if not selected:
messagebox.showinfo("提示", "请选择要同步的窗口!")
return
# 检查主控窗口
master_items = [item for item in self.window_list.get_children()
if self.window_list.set(item, "master") == "√"]
if not master_items:
# 如果没有主控窗口,设置第一个选中的窗口为主控
self.set_master_window(selected[0])
# 切换同步状态
if not self.is_syncing:
try:
self.start_sync(selected)
self.sync_button.configure(text="■ 停止同步", style='Accent.TButton')
self.is_syncing = True
print("同步已开启")
# 使用after方法异步显示通知
self.root.after(10, lambda: self.show_notification("同步已开启", "Chrome多窗口同步功能已启动"))
except Exception as e:
print(f"开启同步失败: {str(e)}")
# 确保状态正确
self.is_syncing = False
self.sync_button.configure(text="▶ 开始同步", style='Accent.TButton')
# 重新显示错误消息
messagebox.showerror("错误", str(e))
else:
try:
self.stop_sync()
self.sync_button.configure(text="▶ 开始同步", style='Accent.TButton')
self.is_syncing = False
print("同步已停止")
# 使用after方法异步显示通知
self.root.after(10, lambda: self.show_notification("同步已关闭", "Chrome多窗口同步功能已停止"))
except Exception as e:
print(f"停止同步失败: {str(e)}")
def show_notification(self, title, message):
"""显示系统通知"""
try:
if self.is_win11:
# Windows 11 使用toast通知
try:
# 使用线程来显示通知
def show_toast():
try:
self.notify_func(
title,
message,
duration="short",
app_id="Chrome多开管理工具",
on_dismissed=lambda x: None # 忽略关闭回调
)
except Exception:
pass
threading.Thread(target=show_toast).start()
except TypeError:
# 如果上面的方法失败,尝试使用另一种调用方式
def show_toast_alt():
try:
self.notify_func({
"title": title,
"message": message,
"duration": "short",
"app_id": "Chrome多开管理工具",
"on_dismissed": lambda x: None
})
except Exception:
pass
threading.Thread(target=show_toast_alt).start()
else:
# Windows 10 使用win32gui通知
try:
# 确保托盘图标已注册
if not hasattr(self, 'notify_id'):
self.hwnd = win32gui.GetForegroundWindow()
self.notification_flags = win32gui.NIF_ICON | win32gui.NIF_INFO | win32gui.NIF_TIP
# 加载app.ico图标
try:
icon_path = os.path.join(os.path.dirname(__file__), "app.ico")
if os.path.exists(icon_path):
# 加载应用程序图标
icon_handle = win32gui.LoadImage(
0, icon_path, win32con.IMAGE_ICON,
0, 0, win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
)
else:
# 使用默认图标
icon_handle = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
except Exception as e:
print(f"加载托盘图标失败: {str(e)}")
icon_handle = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
self.notify_id = (
self.hwnd,
0,
self.notification_flags,
win32con.WM_USER + 20,
icon_handle,
"Chrome多窗口管理器"
)
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, self.notify_id)
# 获取当前图标句柄
icon_handle = self.notify_id[4]
# 准备通知数据
notify_data = (
self.hwnd,
0,
self.notification_flags,
win32con.WM_USER + 20,
icon_handle,
"Chrome多窗口管理器", # 托盘提示
message, # 通知内容
1000, # 1秒 = 1000毫秒
title, # 通知标题
win32gui.NIIF_INFO # 通知类型
)
# 显示通知
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, notify_data)
except Exception as e:
print(f"Windows 10 通知显示失败: {str(e)}")
except Exception as e:
print(f"显示通知失败: {str(e)}")
def start_sync(self, selected_items):
try:
# 确保主控窗口存在
if not self.master_window:
raise Exception("未设置主控窗口")
# 清除之前可能的同步状态
if hasattr(self, 'is_sync') and self.is_sync: