-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPySide2_Customized_Window.py
More file actions
1097 lines (1029 loc) · 53.3 KB
/
Copy pathPySide2_Customized_Window.py
File metadata and controls
1097 lines (1029 loc) · 53.3 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 sys
if sys.platform != 'win32': raise Exception('Windows, ReactOS or Wine is required.')
try:
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
SIDEVER = 2
except: raise ImportError('Cannot load PySide2.')
import ctypes
try: import winreg
except: import _winreg as winreg
from ctypes.wintypes import MSG, POINT, RECT
__all__ = ['CustomizedWindow', 'BlurWindow']
user32 = ctypes.windll.user32
WM_SIZE, WM_SHOWWINDOW, WM_SETTINGCHANGE, WM_STYLECHANGED, WM_NCCALCSIZE, WM_NCHITTEST, WM_NCPAINT, WM_NCLBUTTONDOWN, WM_NCLBUTTONUP, WM_SYSCOMMAND, WM_ENTERSIZEMOVE, WM_EXITSIZEMOVE, WM_DPICHANGED, WM_DWMCOMPOSITIONCHANGED = 0x5, 0x18, 0x1a, 0x7d, 0x83, 0x84, 0x85, 0xa1, 0xa2, 0x112, 0x231, 0x232, 0x2e0, 0x31e
SC_SIZE, SC_MOVE, SC_MINIMIZE, SC_MAXIMIZE, SC_CLOSE, SC_RESTORE = 0xf000, 0xf010, 0xf020, 0xf030, 0xf060, 0xf120
HTCLIENT, HTCAPTION, HTMINBUTTON, HTMAXBUTTON, HTCLOSE = 0x1, 0x2, 0x8, 0x9, 0x14
HTLEFT, HTRIGHT, HTTOP, HTTOPLEFT, HTTOPRIGHT, HTBOTTOM, HTBOTTOMLEFT, HTBOTTOMRIGHT, HTBORDER = range(0xa, 0x13)
SPI_SETNONCLIENTMETRICS, SPI_SETWORKAREA = 0x2a, 0x2f
SWP_NOSIZE, SWP_NOMOVE, SWP_NOZORDER, SWP_FRAMECHANGED, SWP_NOSENDCHANGING = 0x1, 0x2, 0x4, 0x20, 0x400
VK_LBUTTON = 0x1
ISWINE = hasattr(ctypes.windll.ntdll, 'wine_get_version')
class LOGFONT(ctypes.Structure):
_fields_ = [
('lfHeight', ctypes.c_long),
('lfWidth', ctypes.c_long),
('lfEscapement', ctypes.c_long),
('lfOrientation', ctypes.c_long),
('lfWeight', ctypes.c_long),
('lfItalic', ctypes.c_byte),
('lfUnderline', ctypes.c_byte),
('lfStrikeOut', ctypes.c_byte),
('lfCharSet', ctypes.c_byte),
('lfOutPrecision', ctypes.c_byte),
('lfClipPrecision', ctypes.c_byte),
('lfQuality', ctypes.c_byte),
('lfPitchAndFamily', ctypes.c_byte),
('lfFaceName', ctypes.c_wchar * 32)]
class NONCLIENTMETRICS(ctypes.Structure):
_fields_ = [
('cbSize', ctypes.c_ulong),
('iBorderWidth', ctypes.c_long),
('iScrollWidth', ctypes.c_long),
('iScrollHeight', ctypes.c_long),
('iCaptionWidth', ctypes.c_long),
('iCaptionHeight', ctypes.c_long),
('lfCaptionFont', LOGFONT),
('iSmCaptionWidth', ctypes.c_long),
('iSmCaptionHeight', ctypes.c_long),
('lfSmCaptionFont', LOGFONT),
('iMenuWidth', ctypes.c_long),
('iMenuHeight', ctypes.c_long),
('lfMenuFont', LOGFONT),
('lfStatusFont', LOGFONT),
('lfMessageFont', LOGFONT),
('iPaddedBorderWidth', ctypes.c_long)]
class STYLESTRUCT(ctypes.Structure): _fields_ = [('styleOld', ctypes.c_ulong), ('styleNew', ctypes.c_ulong)]
class NCCALCSIZE_PARAMS(ctypes.Structure): _fields_ = [('rgrc', RECT * 3), ('lppos', ctypes.POINTER(ctypes.c_void_p))]
class MONITORINFO(ctypes.Structure): _fields_ = [('cbSize', ctypes.c_ulong), ('rcMonitor', RECT), ('rcWork', RECT), ('dwFlags', ctypes.c_ulong)]
class APPBARDATA(ctypes.Structure): _fields_ = [('cbSize', ctypes.c_ulong), ('hWnd', ctypes.c_void_p), ('uCallbackMessage', ctypes.c_ulong), ('uEdge', ctypes.c_ulong), ('rc', RECT), ('lParam', ctypes.c_long)]
class WindowCompositionAttribute(ctypes.Structure): _fields_ = [('Attribute', ctypes.c_long), ('Data', ctypes.POINTER(ctypes.c_long)), ('SizeOfData', ctypes.c_ulong)]
class ACCENT_POLICY(ctypes.Structure): _fields_ = [('AccentState', ctypes.c_ulong), ('AccentFlags', ctypes.c_ulong), ('GradientColor', ctypes.c_ulong), ('AnimationId', ctypes.c_ulong)]
class DWM_BLURBEHIND(ctypes.Structure): _fields_ = [('dwFlags', ctypes.c_ulong), ('fEnable', ctypes.c_long), ('hRgnBlur', ctypes.c_void_p), ('fTransitionOnMaximized', ctypes.c_long)]
class MARGINS(ctypes.Structure): _fields_ = [('cxLeftWidth', ctypes.c_long), ('cxRightWidth', ctypes.c_long), ('cyTopHeight', ctypes.c_long), ('cyBottomHeight', ctypes.c_long)]
class WINDOWCOMPOSITIONATTRIBDATA(ctypes.Structure): _fields_ = [('Attribute', ctypes.c_ulong), ('Data', ctypes.POINTER(ACCENT_POLICY)), ('SizeOfData', ctypes.c_ulong)]
class Win10BlurEffect:
def __init__(self):
self.WCA_ACCENT_POLICY, self.ACCENT_ENABLE_BLURBEHIND, self.ACCENT_ENABLE_ACRYLICBLURBEHIND = 19, 3, 4
self.accentPolicy = ACCENT_POLICY()
self.winCompAttrData = WINDOWCOMPOSITIONATTRIBDATA()
self.winCompAttrData.Attribute = self.WCA_ACCENT_POLICY
self.winCompAttrData.SizeOfData = ctypes.sizeof(self.accentPolicy)
self.winCompAttrData.Data = ctypes.pointer(self.accentPolicy)
def __initAP(self, a, b, c, d):
b = ctypes.c_ulong(int(b, base=16))
d = ctypes.c_ulong(d)
accentFlags = ctypes.c_ulong(0x2 | 0x20 | 0x40 | 0x80 | 0x100 | 0x200 if c else 0x2)
self.accentPolicy.AccentState = a
self.accentPolicy.GradientColor = b
self.accentPolicy.AccentFlags = accentFlags
self.accentPolicy.AnimationId = d
def __mainFunc(self, hwnd): return user32.SetWindowCompositionAttribute(hwnd, ctypes.byref(self.winCompAttrData))
def setAeroEffect(self, hwnd, gradientColor='007F7F7F', isEnableShadow=False, animationId=0):
self.__initAP(self.ACCENT_ENABLE_BLURBEHIND, gradientColor, isEnableShadow, animationId)
return self.__mainFunc(hwnd)
def setAcrylicEffect(self, hwnd, gradientColor='007F7F7F', isEnableShadow=False, animationId=0):
self.__initAP(self.ACCENT_ENABLE_ACRYLICBLURBEHIND, gradientColor, isEnableShadow, animationId)
return self.__mainFunc(hwnd)
def disableEffect(self, hwnd):
self.__initAP(0, '007F7F7F', 0, 0)
return self.__mainFunc(hwnd)
class SystemVBoxLyt(QVBoxLayout):
def __init__(self, parent):
super(SystemVBoxLyt, self).__init__()
self.setContentsMargins(*[0] * 4)
self.setSpacing(0)
class SystemHBoxLyt(QHBoxLayout):
def __init__(self, parent):
super(SystemHBoxLyt, self).__init__()
self.parent = parent
self.isttlbarlyt, self.isttliconlyt = map(isinstance, [self] * 2, [TtlBarLyt, TtlIconLyt])
self.updateMgn = lambda: self.setContentsMargins(*[parent._CustomizedWindow__ttlicon_mgn if self.isttliconlyt else 0] * 4)
self.updateMgn()
self.setSpacing(0)
class TtlBarLyt(SystemHBoxLyt): pass
class TtlIconLyt(SystemHBoxLyt): pass
class SystemLblBtnBase(QAbstractButton):
def __init__(self, parent):
super(SystemLblBtnBase, self).__init__(parent)
self.parent = parent
self.parentattr = lambda a: getattr(parent, '_CustomizedWindow__' + a)
self.setFocusPolicy(Qt.NoFocus)
self.setMouseTracking(True)
self.PM = lambda a: user32.PostMessageW(self.parent.hwnd(), WM_SYSCOMMAND, a, 0)
def sendNCHTTST(self, x, y): user32.SendMessageW(self.parent.hwnd(), WM_NCHITTEST, 0, ctypes.c_long(ctypes.c_ushort(x).value | ctypes.c_ulong(ctypes.c_ushort(y).value).value << 16).value)
def cursorPos(self):
pos = getGlobalCursorPos(self.parent.hwnd())
return [pos, pos.x, pos.y]
def handleMME(self):
x, y = self.cursorPos()[1:3]
if user32.GetKeyState(VK_LBUTTON) in [0, 1]: self.sendNCHTTST(x, y)
def enterEvent(self, *a): self.handleMME()
def leaveEvent(self, *a): self.handleMME()
def mouseMoveEvent(self, *a): self.handleMME()
def mousePressEvent(self, event):
if event.button() == 1:
lnr = self.parentattr('last_nchttst_res')
pos, x, y = self.cursorPos()
self.sendNCHTTST(x, y)
user32.SendMessageW(self.parent.hwnd(), WM_NCLBUTTONDOWN, lnr, ctypes.byref(pos))
if lnr == HTCAPTION:
user32.ReleaseCapture()
self.PM(SC_MOVE | HTCAPTION)
if lnr in [HTLEFT, HTRIGHT, HTTOP, HTTOPLEFT, HTTOPRIGHT, HTBOTTOM, HTBOTTOMLEFT, HTBOTTOMRIGHT]:
user32.ReleaseCapture()
self.PM(SC_SIZE | (lnr - 0x9))
def mouseReleaseEvent(self, event):
if event.button() == 1:
pos, x, y = self.cursorPos()
user32.SendMessageW(self.parent.hwnd(), WM_NCLBUTTONUP, self.parentattr('last_nchttst_res'), ctypes.byref(pos))
def mouseDoubleClickEvent(self, event):
if event.button() == 1:
pos, x, y = self.cursorPos()
self.sendNCHTTST(x, y)
if self.parentattr('hasmaxbtn') and self.parentattr('last_nchttst_res') == HTCAPTION: user32.ReleaseCapture(), user32.SendMessageW(self.parent.hwnd(), WM_NCLBUTTONUP, HTMAXBUTTON, ctypes.byref(pos))
class MenuBtn(SystemLblBtnBase):
def __init__(self, parent):
super(MenuBtn, self).__init__(parent)
self.isminbtn, self.ismaxbtn, self.isclsbtn = map(isinstance, [self] * 3, [MinBtn, MaxBtn, CloseBtn])
self.updateSize = lambda: self.setFixedSize(*list(map(self.parentattr, ['menubtn_w', 'ttl_h'])))
self.updateSize()
self.setFocusPolicy(Qt.NoFocus)
self.setMouseTracking(True)
self.bgclr = Qt.transparent
def paintEvent(self, *a):
self.updateSize()
w, h = self.width(), self.height()
parent = self.parent
dpi, rdpi = parent.dpi(), parent.realdpi()
isdarktheme = parent.isDarkTheme()
isactivewindow = parent.hwnd() == user32.GetForegroundWindow()
ISMAX, ISFULL = user32.IsZoomed(parent.hwnd()), parent.isFullScreen()
hasminbtn, hasmaxbtn = map(self.parentattr, ['hasminbtn', 'hasmaxbtn'])
painter, path = QPainter(self), QPainterPath()
painter.setBrush(self.bgclr)
painter.setPen(Qt.NoPen)
painter.drawRect(self.rect())
painter.setBrush(Qt.NoBrush)
isdisabled = (self.isminbtn and not hasminbtn) or (self.ismaxbtn and not hasmaxbtn)
pen = QPen(self.parentattr('menubtnclr_%s_%s' % ('d' if isdarktheme else 'l', 'ac' if isactivewindow and not isdisabled else 'in')))
penwidth = int(1.35 * dpi / 96.0)
pen.setWidth(penwidth)
painter.setPen(pen)
f1, f2 = lambda n: int(n * w), lambda n: int(n * h)
f3, f4 = path.moveTo, path.lineTo
if rdpi >= 143: painter.setRenderHint(QPainter.Antialiasing)
if self.isminbtn:
f3(f1(0.391), f2(0.500))
f4(f1(0.609), f2(0.500))
elif self.ismaxbtn:
if ISMAX:
f3(f1(0.402), f2(0.406))
f4(f1(0.559), f2(0.406))
f4(f1(0.559), f2(0.656))
f4(f1(0.402), f2(0.656))
f4(f1(0.402), f2(0.406))
f3(f1(0.441), f2(0.344))
f4(f1(0.598), f2(0.344))
f4(f1(0.598), f2(0.594))
else:
f3(f1(0.402), f2(0.344))
f4(f1(0.598), f2(0.344))
f4(f1(0.598), f2(0.656))
f4(f1(0.402), f2(0.656))
f4(f1(0.402), f2(0.344))
elif self.isclsbtn:
f3(f1(0.402), f2(0.344))
f4(f1(0.598), f2(0.656))
f3(f1(0.598), f2(0.344))
f4(f1(0.402), f2(0.656))
painter.drawPath(path)
class MinBtn(MenuBtn): pass
class MaxBtn(MenuBtn): pass
class CloseBtn(MenuBtn): pass
class SystemLbl(SystemLblBtnBase):
def __init__(self, parent):
super(SystemLbl, self).__init__(parent)
self.isbglbl, self.isttlbar, self.isclientarealbl, self.isttltextlbl, self.isttliconcontainerlbl, self.isttliconlbl = map(isinstance, [self] * 6, [BgLbl, TtlBar, ClientAreaLbl, TtlTextLbl, TtlIconContainerLbl, TtlIconLbl])
if self.isttlbar: self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed), self.setFixedHeight(self.parentattr('ttl_h'))
elif self.isbglbl or self.isclientarealbl or self.isttltextlbl or self.isttliconlbl: self.setSizePolicy(*[QSizePolicy.Expanding] * 2)
elif self.isttliconcontainerlbl: self.setFixedSize(*[self.parentattr('ttl_h')] * 2)
self.bgclr = Qt.transparent
self.draw = True
self.isMax = lambda: user32.IsZoomed(parent.hwnd())
self.maxWithMgn = lambda: self.isMax() and not parent.isFullScreen() and self.parentattr('maxwithmgn')
def paintEvent(self, *a):
parent = self.parent
isdarktheme = parent.isDarkTheme()
isblurwindow = self.parentattr('isblurwindow')
isaeroenabled = isAeroEnabled()
isactivewindow = parent.hwnd() == user32.GetForegroundWindow()
NMAXFULL = not (self.isMax() or parent.isFullScreen())
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
bgclr = Qt.transparent
ttl_h = self.parentattr('ttl_h')
f1 = lambda pen: (painter.setBrush(Qt.NoBrush), painter.setPen(pen))
f2 = lambda n: QColor(*[6 if isdarktheme else 249] * 3 + [n])
if self.isbglbl: bgclr = QColor(127, 127, 127, 1) if (isblurwindow and isaeroenabled) else QColor(*[58 if isdarktheme else 197] * 3)
elif self.isttlbar:
self.setFixedHeight(ttl_h)
if isblurwindow:
if self.draw:
bgclr = QLinearGradient(*[0] * 3 + [self.height()])
list(map(bgclr.setColorAt, [0, 1], [f2(107), f2(197)]))
else: bgclr = f2(self.parentattr('caopacity'))
else: bgclr = f2(255)
elif self.isclientarealbl:
self.placeCA()
bgclr = f2(self.parentattr('caopacity'))
elif self.isttltextlbl:
pen = QPen(self.parentattr('ttltextclr_%s_%s' % ('d' if isdarktheme else 'l', 'ac' if isactivewindow else 'in')))
f1(pen)
font = QFont(self.parentattr('captionfont'))
font.setPixelSize(self.parentattr('ttl_fontsize'))
painter.setFont(font)
if self.draw: painter.drawText(self.rect(), Qt.AlignVCenter, parent.windowTitle())
elif self.isttliconcontainerlbl:
self.setFixedSize(*[ttl_h] * 2)
elif self.isttliconlbl:
icon = QPixmap.fromImage(parent.windowIcon().pixmap(self.size()).toImage()).scaled(self.size())
if self.draw: painter.drawPixmap(0, 0, icon)
painter.setBrush(bgclr)
painter.setPen(Qt.NoPen)
painter.drawRect(self.rect())
if (self.isttlbar or self.isclientarealbl) and (NMAXFULL or self.maxWithMgn()):
pen = QPen(QColor(*[127] * 3))
grey_bd_w = int(2 * parent.dpi() / 96.0)
pen.setWidth(grey_bd_w)
f1(pen)
painter.drawRect(0, -grey_bd_w if self.isclientarealbl else 0, self.width(), self.height() + grey_bd_w * (2 if self.isttlbar else 1))
def placeCA(self):
parent = self.parent
bd_w = self.parentattr('bd_w')
ttl_h = self.parentattr('ttl_h')
NMAXFULL = not (self.isMax() or parent.isFullScreen())
WITHMARGIN = NMAXFULL or self.maxWithMgn()
parent.clientArea.setGeometry(QRect(*[bd_w * int(WITHMARGIN), 0, parent.width() - bd_w * (2 if WITHMARGIN else 0), parent.height() - ttl_h - bd_w * int(WITHMARGIN)]))
class BgLbl(SystemLbl): pass
class TtlBar(SystemLbl): pass
class ClientAreaLbl(SystemLbl): pass
class TtlTextLbl(SystemLbl): pass
class TtlIconContainerLbl(SystemLbl): pass
class TtlIconLbl(SystemLbl): pass
def sCW(obj): return super(CustomizedWindow, obj)
def GetWindowsSystemVersion():
dwMajor, dwMinor, dwBuildNumber = [ctypes.c_ulong() for i in range(3)]
ctypes.windll.ntdll.RtlGetNtVersionNumbers(ctypes.byref(dwMajor), ctypes.byref(dwMinor), ctypes.byref(dwBuildNumber))
return (dwMajor.value, dwMinor.value, dwBuildNumber.value & ~0xf0000000)
def GetRC(hwnd):
rc = RECT()
user32.GetWindowRect(hwnd, ctypes.byref(rc))
return rc
def GetMgn(hwnd):
f1 = lambda n: user32.GetWindowLongW(hwnd, n)
crc, wrc = GetRC(hwnd), GetRC(hwnd)
user32.AdjustWindowRectEx(ctypes.byref(wrc), f1(-16), 0, f1(-20))
return [crc.left - wrc.left, wrc.right - crc.right, crc.top - wrc.top, wrc.bottom - crc.bottom]
def clXYWH(hwnd, x=0, y=0, w=0, h=0):
m = GetMgn(hwnd)
return [x + m[0], y + m[2], w - sum(m[0:2]), h - sum(m[2:4])]
def wdXYWH(hwnd, x=0, y=0, w=0, h=0):
m = GetMgn(hwnd)
return [x - m[0], y - m[2], w + sum(m[0:2]), h + sum(m[2:4])]
def isAeroEnabled():
try:
assert not ISWINE
pfEnabled = ctypes.c_ulong()
ctypes.windll.dwmapi.DwmIsCompositionEnabled(ctypes.byref(pfEnabled))
return pfEnabled.value
except: return 0
def isdarktheme():
try: return not winreg.QueryValueEx(winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize'), 'AppsUseLightTheme')[0]
except: return False
def getMonitorRectForWindow(hwnd, workarea=False):
hMon = user32.MonitorFromWindow(hwnd, 2)
monInf = MONITORINFO()
monInf.cbSize = ctypes.sizeof(MONITORINFO)
user32.GetMonitorInfoW(hMon, ctypes.byref(monInf))
return getattr(monInf, 'rcWork' if workarea else 'rcMonitor')
def gethwnd(window):
hwnd = window.winId()
if type(hwnd) != int:
try:
f = ctypes.pythonapi.PyCapsule_GetPointer
f.restype, f.argtypes = ctypes.c_void_p, [ctypes.py_object, ctypes.c_char_p]
hwnd = f(hwnd, None)
except:
f = ctypes.pythonapi.PyCObject_AsVoidPtr
f.restype, f.argtypes = ctypes.c_void_p, [ctypes.py_object]
hwnd = f(hwnd)
return hwnd
def getGlobalCursorPos(hwnd):
pos = POINT()
try:
user32.GetPhysicalCursorPos(ctypes.byref(pos))
user32.PhysicalToLogicalPoint(hwnd, ctypes.byref(pos))
except: user32.GetCursorPos(ctypes.byref(pos))
return pos
def getdpiforwindow(hwnd):
n = 96
try:
nx, ny = [ctypes.c_ulong()] * 2
monitor_h = user32.MonitorFromWindow(hwnd, 2)
ctypes.windll.shcore.GetDpiForMonitor(monitor_h, 0, ctypes.byref(nx), ctypes.byref(ny))
n = nx.value
except:
aware = user32.IsProcessDPIAware() if hasattr(user32, 'IsProcessDPIAware') else True
if aware:
hDC = user32.GetDC(None)
n = ctypes.windll.gdi32.GetDeviceCaps(hDC, 88)
user32.ReleaseDC(None, hDC)
return n
def getautohidetbpos(rc=RECT(*[0] * 4)):
ABM_GETAUTOHIDEBAR, ABM_GETAUTOHIDEBAREX = 0x7, 0xb
data = APPBARDATA()
data.cbSize = ctypes.sizeof(APPBARDATA)
data.rc = rc
shell32 = ctypes.windll.shell32
tb_rc = GetRC(user32.FindWindowA(b'Shell_TrayWnd', None))
for i in range(4):
data.uEdge = i
if shell32.SHAppBarMessage(ABM_GETAUTOHIDEBAREX, ctypes.byref(data)): return i
else:
if (tb_rc.left == rc.left) + (tb_rc.top == rc.top) + (tb_rc.right == rc.right) + (tb_rc.bottom == rc.bottom) >= 2:
if shell32.SHAppBarMessage(ABM_GETAUTOHIDEBAR, ctypes.byref(data)): return i
return 4
def setwin11blur(hwnd, material=2):
E_21H2, E_22H2, V_21H2, V_22H2 = 1029, 38, 1, material
return list(map(ctypes.windll.dwmapi.DwmSetWindowAttribute, [hwnd] * 2, [E_21H2, E_22H2], [ctypes.byref(ctypes.c_long(V_21H2)), ctypes.byref(ctypes.c_long(V_22H2))], [ctypes.sizeof(ctypes.c_long)] * 2))
def getcaptionfont():
res = NONCLIENTMETRICS()
res.cbSize = ctypes.sizeof(NONCLIENTMETRICS)
user32.SystemParametersInfoW(0x29, res.cbSize, ctypes.byref(res), 0)
return res.lfCaptionFont.lfFaceName
class SplashScreen(QSplashScreen):
def __init__(self, parent):
super(SplashScreen, self).__init__()
self.__hwnd = gethwnd(self)
dpi = parent.dpi()
sc = QApplication.screens()[0].size() if hasattr(QApplication, 'screens') else QDesktopWidget().screenGeometry(0)
f1 = lambda n: int(n * dpi / 96.0)
f2 = lambda a, b: [(a.width() - b.width()) // 2, (a.height() - b.height()) // 2]
self.resize(*[f1(500)] * 2)
self.move(*f2(sc, self))
self.mainlbl = QLabel(self)
bgclr, bdclr = ['#000000', '#AFAFAF'] if parent.isDarkTheme() else ['#FFFFFF', '#505050']
self.mainlbl.setStyleSheet('background: %s; border: %dpx solid %s' % (bgclr, f1(2), bdclr))
self.mainlbl.resize(self.width(), self.height())
self.mainlbl.move(*f2(self, self.mainlbl))
self.iconlbl = QLabel(self)
iconsize = [f1(175)] * 2
pixmap = QPixmap.fromImage(parent.windowIcon().pixmap(*iconsize).toImage()).scaled(*iconsize)
self.iconlbl.setPixmap(pixmap)
self.iconlbl.resize(*iconsize)
self.iconlbl.move(*f2(self, self.iconlbl))
shadow = QGraphicsBlurEffect(self)
shadow.setBlurRadius(f1(10))
self.mainlbl.setGraphicsEffect(shadow)
class CustomizedWindow(QWidget):
'''A customized window based on PySideX.'''
def __init__(self):
self.__isblurwindow = isinstance(self, BlurWindow)
self.__blurmtrl = 0
self.__ncsizeinited, self.__windowinited = [False] * 2
self.__flashinnextmessage_inf = [False, 0, 0, 0, 0]
self.__maxwithmgn = False
self.__last_nchttst_res = 1
sCW(self).__init__()
self.__hwnd = gethwnd(self)
hwnd = self.hwnd()
self.setAttribute(Qt.WA_TranslucentBackground, True) if SIDEVER == 1 else self.setStyleSheet('CustomizedWindow{background: rgba(0, 0, 0, 0)}')
self.__updtnc = lambda sendmsg=False: user32.SetWindowPos(hwnd, None, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED | (0 if sendmsg else SWP_NOSENDCHANGING))
self.__SWL = getattr(user32, 'SetWindowLongPtrW' if hasattr(user32, 'SetWindowLongPtrW') else 'SetWindowLongW')
self.__WNDPROC = ctypes.WINFUNCTYPE(ctypes.c_long, ctypes.c_long, ctypes.c_ulong, ctypes.c_long, ctypes.c_long)
self.__BasicMHAddr = ctypes.cast(self.__WNDPROC(self.__BasicMH), ctypes.c_void_p)
getattr(user32, 'GetWindowLongPtrW' if hasattr(user32, 'GetWindowLongPtrW') else 'GetWindowLongW')(hwnd, -4)
self.__handle_setWindowFlags(False)
if isAeroEnabled(): self.__setDWMEffect(self.__isblurwindow)
self.__rdpi = getdpiforwindow(hwnd)
self.__hdpisfroundingpolicy = 3
self.__hdpiscalingenabled = SIDEVER >= 6 or (hasattr(Qt, 'AA_EnableHighDpiScaling') and QApplication.testAttribute(Qt.AA_EnableHighDpiScaling))
if hasattr(Qt, 'HighDpiScaleFactorRoundingPolicy'):
QHDSFRP = Qt.HighDpiScaleFactorRoundingPolicy
QADSFRP = QApplication.highDpiScaleFactorRoundingPolicy()
policy_dict = {QHDSFRP.Ceil: 1, QHDSFRP.Floor: 2, QHDSFRP.PassThrough: 3, QHDSFRP.Round: 4, QHDSFRP.RoundPreferFloor: 5}
self.__hdpisfroundingpolicy = 3 if hasattr(QHDSFRP, 'Unset') and QADSFRP == QHDSFRP.Unset else policy_dict[QADSFRP]
dpi = self.dpi()
self.__caopacity = 127 if self.__isblurwindow else 255
self.__thmclr = 0
self.__isdarktheme = isdarktheme()
self.__ttltextclr_l_ac, self.__ttltextclr_d_ac, self.__ttltextclr_l_in, self.__ttltextclr_d_in, self.__menubtnclr_l_ac, self.__menubtnclr_d_ac, self.__menubtnclr_l_in, self.__menubtnclr_d_in = [Qt.black, Qt.white, QColor(*[99] * 3), QColor(*[155] * 3)] * 2
self.__updatedpiconstants()
self.__inminbtn, self.__inmaxbtn, self.__inclsbtn, self.__inttlbar, self.__inbd_t, self.__inbd_l, self.__inbd_b, self.__inbd_r = [False] * 8
self.__captionfont = getcaptionfont()
self.__mgn_l, self.__mgn_t, self.__mgn_r, self.__mgn_b = [0] * 4
self.__bgLbl = BgLbl(self)
self.__mainLyt = SystemVBoxLyt(self)
self.setLayout(self.__mainLyt)
self.__mainLyt.addWidget(self.__bgLbl)
self.__bgLyt = SystemVBoxLyt(self)
self.__bgLbl.setLayout(self.__bgLyt)
self.__ttlBar = TtlBar(self)
self.__ttlBarLyt = TtlBarLyt(self)
self.__ttlBar.setLayout(self.__ttlBarLyt)
self.__clientAreaLbl = ClientAreaLbl(self)
self.clientArea = QWidget(self.__clientAreaLbl)
self.__ttlIconLyt = TtlIconLyt(self)
self.__ttlIconContainerLbl = TtlIconContainerLbl(self)
self.__ttlIconContainerLbl.setLayout(self.__ttlIconLyt)
self.__ttlIconLbl = TtlIconLbl(self)
self.__ttlIconLyt.addWidget(self.__ttlIconLbl)
self.__ttlTextLbl = TtlTextLbl(self)
self.__minBtn, self.__maxBtn, self.__clsBtn = MinBtn(self), MaxBtn(self), CloseBtn(self)
list(map(self.__bgLyt.addWidget, [self.__ttlBar, self.__clientAreaLbl]))
list(map(self.__ttlBarLyt.addWidget, [self.__ttlIconContainerLbl, self.__ttlTextLbl, self.__minBtn, self.__maxBtn, self.__clsBtn]))
self.setDarkTheme(0)
if SIDEVER != 1: self.windowHandle().screenChanged.connect(lambda: self.__updtnc(True))
def resize(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1:
w, h = [a[0].width(), a[0].height()] if len(a) == 1 else a
return sobj.resize(*clXYWH(self.hwnd(), w=w, h=h)[2:4])
except: pass
return sobj.resize(*a)
def setGeometry(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1:
x, y, w, h = [a[0].x(), a[0].y(), a[0].width(), a[0].height()] if len(a) == 1 else a
return sobj.setGeometry(*clXYWH(self.hwnd(), x, y, w, h))
except: pass
return sobj.setGeometry(*a)
def setFixedSize(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1:
w, h = [a[0].width(), a[0].height()] if len(a) == 1 else a
return sobj.setFixedSize(*clXYWH(self.hwnd(), w=w, h=h)[2:4])
except: pass
return sobj.setFixedSize(*a)
def setFixedWidth(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1: return sobj.setFixedWidth(clXYWH(self.hwnd(), w=a[0])[2])
except: pass
return sobj.setFixedWidth(*a)
def setFixedHeight(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1: return sobj.setFixedHeight(clXYWH(self.hwnd(), h=a[0])[3])
except: pass
return sobj.setFixedHeight(*a)
def setMaximumSize(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1:
w, h = [a[0].width(), a[0].height()] if len(a) == 1 else a
return sobj.setMaximumSize(*clXYWH(self.hwnd(), w=w, h=h)[2:4])
except: pass
return sobj.setMaximumSize(*a)
def setMaximumWidth(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1: return sobj.setMaximumWidth(clXYWH(self.hwnd(), w=a[0])[2])
except: pass
return sobj.setMaximumWidth(*a)
def setMaximumHeight(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1: return sobj.setMaximumHeight(clXYWH(self.hwnd(), h=a[0])[3])
except: pass
return sobj.setMaximumHeight(*a)
def setMinimumSize(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1:
w, h = [a[0].width(), a[0].height()] if len(a) == 1 else a
return sobj.setMinimumSize(*clXYWH(self.hwnd(), w=w, h=h)[2:4])
except: pass
return sobj.setMinimumSize(*a)
def setMinimumWidth(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1:
bestw = clXYWH(self.hwnd(), w=a[0])[2]
return sobj.setMinimumWidth(bestw if bestw >= 0 else a[0])
except: pass
return sobj.setMinimumWidth(*a)
def setMinimumHeight(self, *a):
sobj = sCW(self)
try:
if SIDEVER == 1:
besth = clXYWH(self.hwnd(), h=a[0])[3]
return sobj.setMinimumHeight(besth if besth >= 0 else a[0])
except: pass
return sobj.setMinimumHeight(*a)
def maximumSize(self, *a):
res = sCW(self).maximumSize(*a)
if SIDEVER == 1:
w, h = res.width(), res.height()
if w != 16777215: res.setWidth(wdXYWH(self.hwnd(), w=w)[2])
if h != 16777215: res.setHeight(wdXYWH(self.hwnd(), h=h)[3])
return res
def maximumWidth(self, *a):
res = sCW(self).maximumWidth(*a)
if SIDEVER == 1:
if res != 16777215: res = wdXYWH(self.hwnd(), w=res)[2]
return res
def maximumHeight(self, *a):
res = sCW(self).maximumHeight(*a)
if SIDEVER == 1:
if res != 16777215: res = wdXYWH(self.hwnd(), h=res)[3]
return res
def minimumSize(self, *a):
res = sCW(self).minimumSize(*a)
if SIDEVER == 1:
w, h = res.width(), res.height()
if w != 0: res.setWidth(wdXYWH(self.hwnd(), w=w)[2])
if h != 0: res.setHeight(wdXYWH(self.hwnd(), h=h)[3])
return res
def minimumWidth(self, *a):
res = sCW(self).minimumWidth(*a)
if SIDEVER == 1:
if res != 0: res = wdXYWH(self.hwnd(), w=res)[2]
return res
def minimumHeight(self, *a):
res = sCW(self).minimumHeight(*a)
if SIDEVER == 1:
if res != 0: res = wdXYWH(self.hwnd(), h=res)[3]
return res
def x(self, *a):
res = sCW(self).x(*a)
if SIDEVER == 1:
if res != 0: res = clXYWH(self.hwnd(), x=res)[0]
return res
def y(self, *a):
res = sCW(self).y(*a)
if SIDEVER == 1:
if res != 0: res = clXYWH(self.hwnd(), y=res)[1]
return res
def dpi(self):
'''DPI divided by 96.0 is the scale factor of PySideX UI.
Example:
DPI = window.dpi()
window.resize(int(400.0 * DPI / 96.0), int(175.0 * DPI / 96.0))'''
return self.__getdpibyrealdpi(self.realdpi())
def realdpi(self):
'''REALDPI divided by 96.0 is the scale factor of System UI.'''
return self.__rdpi
def hwnd(self):
'''HWND is the window handle of this window.'''
return self.__hwnd
def isDarkTheme(self):
'''Detect whether dark theme is enabled or not.
You can use 'setDarkTheme' to change setting.'''
return self.__isdarktheme
def setDarkTheme(self, themecolour=0):
'''themecolour=0: Auto; themecolour=1: Light; themecolour=2: Dark'''
self.__thmclr = themecolour
try: self.__isdarktheme = {0: isdarktheme(), 1: False, 2: True}[themecolour]
except:
ErrorType = ValueError if type(themecolour) == int else TypeError
raise ErrorType('Parameter themecolour must be 0, 1 or 2.')
[i.update() for i in [self.__minBtn, self.__maxBtn, self.__clsBtn, self.__ttlBar, self.__clientAreaLbl]]
hwnd = self.hwnd()
try: list(map(ctypes.windll.dwmapi.DwmSetWindowAttribute, [hwnd] * 2, [19, 20], [ctypes.byref(ctypes.c_long(self.isDarkTheme()))] * 2, [ctypes.sizeof(ctypes.c_long(self.isDarkTheme()))] * 2))
except: pass
user32.SetWindowPos(hwnd, None, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER)
def getWindowSizeByClientSize(self, size, dpimode=0):
'''size=[width, height]: The size of client area
dpimode=0: Use PySideX DPI; dpimode=1: Use system DPI
Example:
client_size = [int(400 * window.dpi() / 96.0), int(175 * window.dpi() / 96.0)]
window_size = window.getWindowSizeByClientSize(client_size)
window.resize(*window_size)'''
try: assert type(size[0]) == int and type(size[1]) == int and len(size) == 2
except:
ErrorType = ValueError if type(size) == list else TypeError
raise ErrorType('Parameter size must be a list composed of two integers.')
if dpimode not in [0, 1]:
ErrorType = ValueError if type(dpimode) == int else TypeError
raise ErrorType('Parameter dpimode must be 0 or 1.')
NMAXFULL = not (user32.IsZoomed(self.hwnd()) or self.isFullScreen())
f1 = lambda a: getattr(self, '_CustomizedWindow__' + ('real_' if dpimode else '') + a)
bd_w, ttl_h = f1('bd_w'), f1('ttl_h')
return [int(bd_w * (2 if NMAXFULL else 0) + size[0]), int(bd_w * int(NMAXFULL) + ttl_h + size[1])]
def isAeroEnabled(self):
'''Detect whether Aero is enabled or not.'''
return isAeroEnabled()
def setTitleTextColour(self, colour, theme=0, state=0):
'''colour=0: Default; colour=Qt....: Qt.GlobalColor; colour=QColor(...): QColor
theme=0: Auto; theme=1: Light; theme=2: Dark
state=0: All; state=1: Active; state=2: Inactive'''
if theme not in [0, 1, 2]:
ErrorType = ValueError if type(theme) == int else TypeError
raise ErrorType('Parameter theme must be 0, 1 or 2.')
if state not in [0, 1, 2]:
ErrorType = ValueError if type(state) == int else TypeError
raise ErrorType('Parameter state must be 0, 1 or 2.')
setlightclr, setdarkclr = theme in [0, 1], theme in [0, 2]
setactiveclr, setinactiveclr = state in [0, 1], state in [0, 2]
clr_l_ac, clr_d_ac, clr_l_in, clr_d_in = self.__ttltextclr_l_ac, self.__ttltextclr_d_ac, self.__ttltextclr_l_in, self.__ttltextclr_d_in
if colour == 0:
if setlightclr:
if setactiveclr: clr_l_ac = Qt.black
if setinactiveclr: clr_l_in = QColor(*[99] * 3)
if setdarkclr:
if setactiveclr: clr_d_ac = Qt.white
if setinactiveclr: clr_d_in = QColor(*[155] * 3)
elif type(colour) in [Qt.GlobalColor, QColor]:
if setlightclr:
if setactiveclr: clr_l_ac = colour
if setinactiveclr: clr_l_in = colour
if setdarkclr:
if setactiveclr: clr_d_ac = colour
if setinactiveclr: clr_d_in = colour
else:
ErrorType = ValueError if type(colour) == int else TypeError
raise ErrorType('Parameter colour must be 0, %s or %s.' % (Qt.GlobalColor, QColor))
self.__ttltextclr_l_ac, self.__ttltextclr_d_ac, self.__ttltextclr_l_in, self.__ttltextclr_d_in = clr_l_ac, clr_d_ac, clr_l_in, clr_d_in
self.__ttlTextLbl.update()
def setMenuButtonColour(self, colour, theme=0, state=0):
'''colour=0: Default; colour=Qt....: Qt.GlobalColor; colour=QColor(...): QColor
theme=0: Auto; theme=1: Light; theme=2: Dark
state=0: All; state=1: Active; state=2: Inactive'''
if theme not in [0, 1, 2]:
ErrorType = ValueError if type(theme) == int else TypeError
raise ErrorType('Parameter theme must be 0, 1 or 2.')
if state not in [0, 1, 2]:
ErrorType = ValueError if type(state) == int else TypeError
raise ErrorType('Parameter state must be 0, 1 or 2.')
setlightclr, setdarkclr = theme in [0, 1], theme in [0, 2]
setactiveclr, setinactiveclr = state in [0, 1], state in [0, 2]
clr_l_ac, clr_d_ac, clr_l_in, clr_d_in = self.__menubtnclr_l_ac, self.__menubtnclr_d_ac, self.__menubtnclr_l_in, self.__menubtnclr_d_in
if colour == 0:
if setlightclr:
if setactiveclr: clr_l_ac = Qt.black
if setinactiveclr: clr_l_in = QColor(*[99] * 3)
if setdarkclr:
if setactiveclr: clr_d_ac = Qt.white
if setinactiveclr: clr_d_in = QColor(*[155] * 3)
elif type(colour) in [Qt.GlobalColor, QColor]:
if setlightclr:
if setactiveclr: clr_l_ac = colour
if setinactiveclr: clr_l_in = colour
if setdarkclr:
if setactiveclr: clr_d_ac = colour
if setinactiveclr: clr_d_in = colour
else:
ErrorType = ValueError if type(colour) == int else TypeError
raise ErrorType('Parameter colour must be 0, %s or %s.' % (Qt.GlobalColor, QColor))
self.__menubtnclr_l_ac, self.__menubtnclr_d_ac, self.__menubtnclr_l_in, self.__menubtnclr_d_in = clr_l_ac, clr_d_ac, clr_l_in, clr_d_in
[i.update() for i in [self.__minBtn, self.__maxBtn, self.__clsBtn]]
def setBlurMaterial(self, material):
'''This function is only avaliable in BlurWindow.
material=0: Glass; material=1: Acrylic; material=2: Mica; material=3: MicaVariant'''
if isinstance(self, BlurWindow):
if material in [0, 1, 2, 3]:
self.__blurmtrl = material
self.__setDWMEffect(self.__isblurwindow)
else:
ErrorType = ValueError if type(material) == int else TypeError
raise ErrorType('Parameter material must be 0, 1, 2 or 3.')
else: raise Exception('This function is only avaliable in BlurWindow.')
def setClientAreaBackgroundOpacity(self, opacity):
'''This function is only avaliable in BlurWindow.
opacity=0: transparent; opacity=255: opaque'''
if isinstance(self, BlurWindow):
if type(opacity) == int and 0 <= opacity <= 255: self.__caopacity = opacity
else:
ErrorType = ValueError if type(opacity) == int else TypeError
raise ErrorType('Parameter opacity must be an integer not below 0 and not above 255.')
else: raise Exception('This function is only avaliable in BlurWindow.')
def setWindowTitle(self, arg__1):
sCW(self).setWindowTitle(arg__1)
self.__ttlTextLbl.update()
def setWindowIcon(self, icon):
sCW(self).setWindowIcon(icon)
self.__ttlIconLbl.update()
def setWindowFlag(self, arg__1, on=True):
sCW(self).setWindowFlag(arg__1, on)
self.__handle_setWindowFlags()
def setWindowFlags(self, type):
sCW(self).setWindowFlags(type)
self.__handle_setWindowFlags()
def __handle_setWindowFlags(self, updtnc=True):
self.__hwnd = gethwnd(self)
hwnd = self.hwnd()
windowlong = user32.GetWindowLongW(hwnd, -16)
BasicMHAddr = self.__BasicMHAddr
self.__orig_BasicMH = getattr(user32, 'GetWindowLong%sW' % ('Ptr' if hasattr(user32, 'GetWindowLongPtrW') else ''))(hwnd, -4)
self.__orig_BasicMHFunc = self.__WNDPROC(self.__orig_BasicMH)
self.__ncsizeinited, self.__windowinited = [False] * 2
if SIDEVER == 1:
self.__SWL(hwnd, -4, BasicMHAddr.value)
self.__hasresizablebd, self.__hasminbtn, self.__hasmaxbtn = windowlong & 0x40000, windowlong & 0x20000, windowlong & 0x10000
if isAeroEnabled(): self.__setDWMEffect(self.__isblurwindow)
if updtnc: self.__updtnc()
def __setMBS(self, button, state=1):
bgclr1, bgclr2, bgclr3 = [Qt.transparent] * 3
f1 = lambda n: QColor(*[255 if self.isDarkTheme() else 0] * 3 + [n])
_minmaxbg1, _minmaxbg2 = map(f1, [25, 50])
if button == 1: bgclr1 = {1: _minmaxbg1, 2: _minmaxbg2}[state]
elif button == 2: bgclr2 = {1: _minmaxbg1, 2: _minmaxbg2}[state]
elif button == 3: bgclr3 = QColor(255, 0, 0, {0: 0, 1: 199, 2: 99}[state])
self.__minBtn.bgclr, self.__maxBtn.bgclr, self.__clsBtn.bgclr = bgclr1, bgclr2, bgclr3
[i.update() for i in [self.__ttlTextLbl, self.__minBtn, self.__maxBtn, self.__clsBtn]]
def MessageHandler(self, hwnd, message, wParam, lParam):
'''Example:
class MyOwnWindow(BlurWindow):
|->|...
|->|def MessageHandler(self, hwnd, message, wParam, lParam):
|->||->|print(hwnd, message, wParam, lParam)
|->||->|...'''
pass
def __BasicMH(self, hwnd, message, wParam, lParam):
PM = lambda a: user32.PostMessageW(hwnd, WM_SYSCOMMAND, a, 0)
try:
dpi, rdpi = self.dpi(), self.realdpi()
real_bd_w, real_ttl_h, real_menubtn_w = self.__real_bd_w, self.__real_ttl_h, self.__real_menubtn_w
mgn_l, mgn_t, mgn_r, mgn_b = self.__mgn_l, self.__mgn_t, self.__mgn_r, self.__mgn_b
flashinf = self.__flashinnextmessage_inf
except: dpi, rdpi, real_bd_w, real_ttl_h, real_menubtn_w, mgn_l, mgn_t, mgn_r, mgn_b, flashinf = [96] * 2 + [0] * 7 + [[False, 0, 0, 0, 0]]
if user32.IsZoomed(hwnd) and mgn_t < -2: mgn_t = (mgn_l + mgn_r) // 2 - (0 if mgn_l == mgn_r else 1)
try: resizable_h, resizable_v = self.minimumWidth() != self.maximumWidth(), self.minimumHeight() != self.maximumHeight()
except RuntimeError: resizable_h, resizable_v = [True] * 2
resizable_hv = resizable_h and resizable_v
windowrc = GetRC(hwnd)
windowx, windowy = windowrc.left, windowrc.top
w, h = windowrc.right - windowx, windowrc.bottom - windowy
globalpos = getGlobalCursorPos(hwnd)
x, y = [(lParam & 65535) - windowx, (lParam >> 16) - windowy] if message == WM_NCHITTEST else [globalpos.x - windowx, globalpos.y - windowy]
inttlbar = mgn_t <= y < real_ttl_h + mgn_t
f1 = lambda a: w - mgn_r - a * real_menubtn_w <= x < w - mgn_r - (a - 1) * real_menubtn_w and inttlbar
inminbtn, inmaxbtn, inclsbtn = f1(3), f1(2), f1(1)
inbd_t, inbd_l, inbd_b, inbd_r = y <= real_bd_w, x <= real_bd_w, h - y <= real_bd_w, w - x <= real_bd_w
self.__inminbtn, self.__inmaxbtn, self.__inclsbtn, self.__inttlbar, self.__inbd_t, self.__inbd_l, self.__inbd_b, self.__inbd_r = inminbtn, inmaxbtn, inclsbtn, inttlbar, inbd_t, inbd_l, inbd_b, inbd_r
if message == WM_SIZE:
if flashinf[0]:
user32.SetWindowPos(hwnd, None, flashinf[1], flashinf[2], flashinf[3], flashinf[4], SWP_NOZORDER | SWP_NOSENDCHANGING | 0x20)
self.__flashinnextmessage_inf = [False, 0, 0, 0, 0]
self.update()
if message == WM_NCCALCSIZE:
rc = (ctypes.cast(lParam, ctypes.POINTER(NCCALCSIZE_PARAMS)).contents.rgrc[0] if wParam else ctypes.cast(lParam, ctypes.POINTER(RECT)).contents)
ISMAX, ISFULL = user32.IsZoomed(hwnd), self.isFullScreen()
self.__mgn_l, self.__mgn_r, self.__mgn_t, self.__mgn_b = [0] * 4
if ISMAX:
width, height = rc.right - rc.left, rc.bottom - rc.top
bestrc = getMonitorRectForWindow(hwnd, not ISFULL)
maxmgn_list = [bestrc.left - rc.left, bestrc.top - rc.top, rc.right - bestrc.right, rc.bottom - bestrc.bottom]
autohidetbpos = getautohidetbpos(bestrc)
autohidetbwidth_list = [2 if i == autohidetbpos and not ISFULL else 0 for i in range(4)]
f1 = lambda n: autohidetbwidth_list[n]
f2 = lambda i: maxmgn_list[i] + f1(i)
rcleft, rctop, rcright, rcbottom = bestrc.left + f1(0), bestrc.top + f1(1), rc.right, rc.bottom
if maxmgn_list[2] >= 0 and resizable_h:
rcright = bestrc.right - f1(2)
rc.left, rc.right = rcleft, rcright
if maxmgn_list[3] >= 0 and resizable_v:
rcbottom = bestrc.bottom - f1(3)
rc.top, rc.bottom = rctop, rcbottom
if not (maxmgn_list[2] >= 0 and resizable_h and maxmgn_list[3] >= 0 and resizable_v): self.__maxwithmgn, self.__flashinnextmessage_inf = True, [True, rcleft, rctop, width, height]
else: self.__maxwithmgn, self.__mgn_l, self.__mgn_t, self.__mgn_r, self.__mgn_b = False, f2(0), f2(1), f2(2), f2(3)
__ncsizeinited = self.__ncsizeinited
if not __ncsizeinited:
self.__ncsizeinited = True
if SIDEVER != 1: return 0
else: return 0
if message == WM_NCPAINT:
if not isAeroEnabled(): return 0
if message == WM_NCHITTEST:
ISMAX, ISFULL = user32.IsZoomed(hwnd), self.isFullScreen()
isLBtnDown = user32.GetKeyState(VK_LBUTTON) not in [0, 1]
if (resizable_h or resizable_v) and self.__hasresizablebd and not (ISMAX or ISFULL):
if resizable_hv:
if inbd_t and inbd_l: res = HTTOPLEFT
elif inbd_t and inbd_r: res = HTTOPRIGHT
elif inbd_b and inbd_l: res = HTBOTTOMLEFT
elif inbd_b and inbd_r: res = HTBOTTOMRIGHT
if not 'res' in vars():
if resizable_h:
if inbd_l: res = HTLEFT
elif inbd_r: res = HTRIGHT
if resizable_v:
if inbd_t: res = HTTOP
elif inbd_b: res = HTBOTTOM
if not 'res' in vars():
if inminbtn:
if self.__hasminbtn:
if not isLBtnDown: self.__setMBS(1, 1)
res = HTMINBUTTON
else: res = HTBORDER
elif inmaxbtn:
if self.__hasmaxbtn:
if not isLBtnDown: self.__setMBS(2, 1)
res = HTMAXBUTTON
else: res = HTBORDER
elif inclsbtn:
if not isLBtnDown: self.__setMBS(3, 1)
res = HTCLOSE
elif inttlbar: res = HTCAPTION
if not 'res' in vars(): res = HTBORDER if (inbd_t or inbd_l or inbd_b or inbd_r) and not (ISMAX or ISFULL) else HTCLIENT
if res not in [HTMINBUTTON, HTMAXBUTTON, HTCLOSE] and not isLBtnDown: self.__setMBS(0)
self.__last_nchttst_res = res
return res
if message == WM_ENTERSIZEMOVE:
try:
assert self.__acryliconw10enabled
w10be = Win10BlurEffect()
w10be.disableEffect(hwnd)
w10be.setAeroEffect(hwnd)
except: pass
if message == WM_EXITSIZEMOVE:
try:
assert self.__acryliconw10enabled
w10be = Win10BlurEffect()
w10be.disableEffect(hwnd)
w10be.setAcrylicEffect(hwnd)
except: pass
if message == WM_SHOWWINDOW:
if not self.__windowinited:
f1 = lambda a: user32.SetWindowPos(hwnd, None, windowx + a, windowy + a, w, h, SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOSENDCHANGING)
f1(1), f1(0)
self.__windowinited = True
if message == WM_NCLBUTTONDOWN:
if wParam in [HTMINBUTTON, HTMAXBUTTON, HTCLOSE]:
self.__setMBS({HTMINBUTTON: 1, HTMAXBUTTON: 2, HTCLOSE: 3}[wParam], 2)
return 0
if wParam == HTCAPTION:
ISMAX, ISFULL = user32.IsZoomed(hwnd), self.isFullScreen()
f1 = lambda: user32.SetForegroundWindow(hwnd)
if ISFULL:
f1()
return 0
try: ctypes.windll.dwmapi.DwmSetIconicThumbnail
except:
if ISMAX:
f1()
return 0
if message == WM_NCLBUTTONUP:
self.__setMBS(0)
if wParam == HTMINBUTTON: PM(SC_MINIMIZE)
elif wParam == HTMAXBUTTON:
if user32.IsZoomed(hwnd): PM(SC_RESTORE)
elif self.isFullScreen(): PM(SC_RESTORE)
else: PM(SC_MAXIMIZE)
elif wParam == HTCLOSE: PM(SC_CLOSE)
if message == WM_DPICHANGED:
rc = RECT.from_address(lParam)
orig_rdpi = self.__rdpi
rdpi = wParam >> 16
self.__rdpi = rdpi
if not self.__hdpiscalingenabled:
bestx, besty, bestw, besth = rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top
if not resizable_h: self.setFixedWidth(bestw)
else:
self.setMinimumWidth(int(self.minimumWidth() * rdpi / orig_rdpi))
if self.maximumWidth() < 16777215: self.setMaximumWidth(int(self.maximumWidth() * rdpi / orig_rdpi))
if not resizable_v: self.setFixedHeight(besth)
else:
self.setMinimumHeight(int(self.minimumHeight() * rdpi / orig_rdpi))
if self.maximumHeight() < 16777215: self.setMaximumHeight(int(self.maximumHeight() * rdpi / orig_rdpi))
user32.SetWindowPos(hwnd, None, bestx, besty, bestw, besth, SWP_NOZORDER)
self.__updatedpiconstants()
self.__ttlIconLyt.updateMgn()
[i.update() for i in [self.__minBtn, self.__maxBtn, self.__clsBtn, self.__ttlTextLbl, self.__ttlIconLbl, self.__clientAreaLbl]]
self.__updtnc()
if message == WM_SETTINGCHANGE:
lParam_string = ctypes.c_wchar_p(lParam).value
if wParam == SPI_SETWORKAREA or lParam_string == 'TraySettings': self.__updtnc()
if wParam == SPI_SETNONCLIENTMETRICS:
self.__captionfont = getcaptionfont()
self.__ttlTextLbl.update()
if lParam_string == 'ImmersiveColorSet': self.setDarkTheme(self.__thmclr), self.__bgLbl.update()
if message == WM_DWMCOMPOSITIONCHANGED:
if isAeroEnabled(): self.__setDWMEffect(self.__isblurwindow)
self.__bgLbl.update()
if message == WM_STYLECHANGED:
if self.__ncsizeinited:
nl = user32.GetWindowLongW(hwnd, -16)
self.__hasresizablebd, self.__hasminbtn, self.__hasmaxbtn = nl & 0x40000, nl & 0x20000, nl & 0x10000
messagehandlerres = self.MessageHandler(hwnd, message, wParam, lParam)
if messagehandlerres != None: return messagehandlerres
if SIDEVER == 1: return self.__orig_BasicMHFunc(hwnd, message, wParam, lParam)
def nativeEvent(self, eventType, msg):
'''For PySide2/6, you should define MessageHandler instead of nativeEvent.'''
_msg = MSG.from_address(msg.__int__())
basicmhres = self.__BasicMH(*list(map(getattr, [_msg] * 4, ['hWnd', 'message', 'wParam', 'lParam'])))
if basicmhres != None: return True, basicmhres
return sCW(self).nativeEvent(eventType, msg)
def __setDWMEffect(self, blur=False):
hwnd = self.hwnd()
self.__acryliconw10enabled = False
try:
dwmapi = ctypes.windll.dwmapi
f1 = lambda n: dwmapi.DwmExtendFrameIntoClientArea(hwnd, ctypes.byref(MARGINS(0, 0, 0, n)))
if blur:
w11_21h2_blur_code, w11_22h2_blur_code = setwin11blur(hwnd, {0: 3, 1: 3, 2: 2, 3: 4}[self.__blurmtrl])
try:
if w11_22h2_blur_code and w11_21h2_blur_code and self.__blurmtrl in [1, 2, 3]:
assert GetWindowsSystemVersion() >= (10, 0, 17134)
w10be = Win10BlurEffect()
w10be.disableEffect(hwnd)
w10_blur_code = w10be.setAcrylicEffect(hwnd)