【旧文搬运】python实现MessageBox弹窗阵列动画

相关涉及内容:MessageBox弹窗位置自定义;MessageBox弹窗自动关闭(销毁)

  1 import win32api,win32con,win32gui
  2 from threading import Thread
  3 import random
  4 import time
  5 
  6 class windowtroops(object):
  7     def __init__(self,num,content,title,life):
  8         self.content = content
  9         self.title = title
 10         self.life = life
 11         self.num = num
 12         self.jg = 0.12
 13         self.x_length = 10 #横向步长
 14         self.y_length = 0   #纵向步长
 15         self.startpoint_x = 600 #起始点横坐标
 16         self.startpoint_y = 600 #起始点纵坐标
 17         self.windowsWidth = 320 #重置后的窗口宽度
 18         self.windowsHeight = 160 #重置后的窗口高度
 19         self.namelist=[]
 20         
 21         self.random_flag = 0
 22         
 23         self.exit_thread_num = 0
 24         
 25     def __call__(self):
 26         #显示窗口阵列
 27         self.Createdwindows()
 28         time.sleep(0.5)
 29         if self.random_flag == 0:
 30             self.reset_window_pos()
 31         else:
 32             self.reset_random()
 33     def resetWindow(self,point_x,point_y,w,h):
 34         #设置重置后的窗口参数:起始窗口的 横坐标、纵坐标、宽度、高度
 35         self.startpoint_x = point_x 
 36         self.startpoint_y = point_y 
 37         self.windowsWidth = w
 38         self.windowsHeight = h
 39     
 40     def setStep(self,numx,numy):
 41         #设置x方向步长,设置y方向步长
 42         self.x_length = numx
 43         self.y_length = numy
 44         
 45     def start(self):
 46         self.tempnameINstart = ""
 47         for title_person in self.namelist:
 48             self.tempnameINstart = title_person
 49             Thread(target=self.setSpeed,  daemon=True).start() 
 50             self.windowOFF(self.tempnameINstart)
 51         
 52     def setSpeed(self):
 53         win32api.MessageBox(0,self.content,self.tempnameINstart) 
 54         # win32api.MessageBox(0,content,title)
 55 
 56     def windowOFF(self,name):
 57         self.tempnameINwindowOFF = name
 58         Thread(target=self.setSpeedOFF, args=(self.tempnameINwindowOFF,)).start()
 59     def setSpeedOFF(self,title):
 60         self.jg = self.jg + 0.02
 61         
 62         time.sleep(self.life+self.jg)
 63         self.exit_thread_num = self.exit_thread_num + 1
 64         # print(self.exit_thread_num)
 65         tid = win32gui.FindWindow(None, title)
 66         tid = win32gui.FindWindow(None, title)
 67         win32gui.PostMessage(tid, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
 68         
 69         if self.exit_thread_num >= self.num-2:
 70             #清除为正常关闭的窗口
 71             self.Destroywindows()
 72     
 73     def Createdwindows(self): 
 74         #创建指定个数的弹窗,标题,内容,显示时间
 75         for i in range(self.num):
 76             self.namelist.append(self.title+str(i))
 77         # self.start()
 78         Thread(target=self.start,  daemon=True).start() 
 79     
 80     def Destroywindows(self):
 81         #销毁指定标题的所有窗体
 82         for name in self.namelist:
 83             tid = win32gui.FindWindow(None, name)
 84             if tid:
 85                 time.sleep(0.01)
 86                 win32gui.PostMessage(tid, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
 87         # while(tid):
 88         #     tid = win32gui.FindWindow(None, self.title)
 89         #     time.sleep(0.01)
 90         #     win32gui.PostMessage(tid, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
 91         #     print(tid)
 92 
 93     def reset_random(self):
 94         #重新设置指定标题弹窗的位置
 95         #函数原文https://blog.csdn.net/bodybo/article/details/80664878
 96         targetTitle = self.title
 97         hWndList = []  
 98         win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)  
 99         temp_ins = 0
100        
101         for hwnd in hWndList:
102             # clsname = win32gui.GetClassName(hwnd)
103             title = win32gui.GetWindowText(hwnd)
104             
105             if (title.find(targetTitle) >= 0):    
106                 temp_ins = temp_ins + 1
107                 x = random.randint(0, 1850)
108                 y = random.randint(5, 1000)
109                 win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, x,y, self.windowsWidth, self.windowsHeight,win32con.SWP_SHOWWINDOW)
110                 time.sleep(0.01)
111                 
112     def reset_window_pos(self):  
113         #重新设置指定标题弹窗的位置
114         #函数原文https://blog.csdn.net/bodybo/article/details/80664878
115         targetTitle = self.title
116         hWndList = []  
117         win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)  
118         temp_ins = 0
119        
120         for hwnd in hWndList:
121             # clsname = win32gui.GetClassName(hwnd)
122             title = win32gui.GetWindowText(hwnd)
123             
124             if (title.find(targetTitle) >= 0):    
125                 temp_ins = temp_ins + 1
126                 win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, self.startpoint_x+self.x_length*temp_ins, self.startpoint_y+self.y_length*temp_ins, self.windowsWidth, self.windowsHeight,win32con.SWP_SHOWWINDOW)
127                 time.sleep(0.01)
128                 
129 #定义一个实例测试一下
130 
131 dh1 = windowtroops(40, "System firewall warning","Process encountered an error.",12.5)
132 #生成弹窗的数量:40个
133 #弹窗的标题设置为:Process encountered an error. 
134 #弹窗的内容为:System firewall warning
135 #弹窗的显示时间:12.5秒  (理论上是12.5s,有卡顿的话就不一定了,12.5秒左右吧)
136 
137 dh1.setStep(-23, 0)
138 #设置窗口阵列偏移为:左右方向上-向左偏移23个像素,上下方向上偏移。(所以这里的最终效果是会显示横着一排弹窗)
139 
140 dh1.resetWindow(0, 600, 320, 160)
141 #设置第一个弹窗的坐标为(0,600)
142 #设置弹窗的宽:320 高:160
143 
144 dh1()
145 #开始显示弹窗
146 
147 dh1.Destroywindows()
148 #销毁所有弹窗(按理说弹窗会到时间自己销毁,但有时候会有几个残余,加入此句保证弹窗全部关闭)

————————————————
我在CSDN上发布了完整文章,详细使用方法请跳转至原文。
原文链接:https://blog.csdn.net/qq_39554087/article/details/123952425

posted @ 2022-04-10 20:35  精神小黑猫  阅读(252)  评论(1)    收藏  举报