1. 程式人生 > 其它 >【舊文搬運】python實現MessageBox彈窗陣列動畫

【舊文搬運】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