近期舍友常用我电脑,突然想整点花活恶搞一下,类似这种:

python恶搞程序代码免费 python搞怪代码_python恶搞程序代码免费


于是就有了以下代码:

参考文章:


https://www.jb51.net/article/186353.htm

import win32api,win32con,win32gui
from threading import Thread
import random
import time

class windowtroops(object):
    def __init__(self,num,content,title,life):
        self.content = content
        self.title = title
        self.life = life
        self.num = num
        self.jg = 0.12
        self.x_length = 10 #横向步长
        self.y_length = 0   #纵向步长
        self.startpoint_x = 600 #起始点横坐标
        self.startpoint_y = 600 #起始点纵坐标
        self.windowsWidth = 320 #重置后的窗口宽度
        self.windowsHeight = 160 #重置后的窗口高度
        self.namelist=[]
        
        self.random_flag = 0
        
        self.exit_thread_num = 0
        
    def __call__(self):
        #显示窗口阵列
        self.Createdwindows()
        time.sleep(0.5)
        if self.random_flag == 0:
            self.reset_window_pos()
        else:
            self.reset_random()
    def resetWindow(self,point_x,point_y,w,h):
        #设置重置后的窗口参数:起始窗口的 横坐标、纵坐标、宽度、高度
        self.startpoint_x = point_x 
        self.startpoint_y = point_y 
        self.windowsWidth = w
        self.windowsHeight = h
    
    def setStep(self,numx,numy):
        #设置x方向步长,设置y方向步长
        self.x_length = numx
        self.y_length = numy
        
    def start(self):
        self.tempnameINstart = ""
        for title_person in self.namelist:
            self.tempnameINstart = title_person
            Thread(target=self.setSpeed,  daemon=True).start() 
            self.windowOFF(self.tempnameINstart)
        
    def setSpeed(self):
        win32api.MessageBox(0,self.content,self.tempnameINstart) 
        # win32api.MessageBox(0,content,title)

    def windowOFF(self,name):
        self.tempnameINwindowOFF = name
        Thread(target=self.setSpeedOFF, args=(self.tempnameINwindowOFF,)).start()
    def setSpeedOFF(self,title):
        self.jg = self.jg + 0.02
        
        time.sleep(self.life+self.jg)
        self.exit_thread_num = self.exit_thread_num + 1
        # print(self.exit_thread_num)
        tid = win32gui.FindWindow(None, title)
        tid = win32gui.FindWindow(None, title)
        win32gui.PostMessage(tid, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        
        if self.exit_thread_num >= self.num-2:
            #清除为正常关闭的窗口
            self.Destroywindows()
    
    def Createdwindows(self): 
        #创建指定个数的弹窗,标题,内容,显示时间
        for i in range(self.num):
            self.namelist.append(self.title+str(i))
        # self.start()
        Thread(target=self.start,  daemon=True).start() 
    
    def Destroywindows(self):
        #销毁指定标题的所有窗体
        for name in self.namelist:
            tid = win32gui.FindWindow(None, name)
            if tid:
                time.sleep(0.01)
                win32gui.PostMessage(tid, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        # while(tid):
        #     tid = win32gui.FindWindow(None, self.title)
        #     time.sleep(0.01)
        #     win32gui.PostMessage(tid, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        #     print(tid)

    def reset_random(self):
        #重新设置指定标题弹窗的位置
        #函数原文
        targetTitle = self.title
        hWndList = []  
        win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)  
        temp_ins = 0
       
        for hwnd in hWndList:
            # clsname = win32gui.GetClassName(hwnd)
            title = win32gui.GetWindowText(hwnd)
            
            if (title.find(targetTitle) >= 0):    
                temp_ins = temp_ins + 1
                x = random.randint(0, 1850)
                y = random.randint(5, 1000)
                win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, x,y, self.windowsWidth, self.windowsHeight,win32con.SWP_SHOWWINDOW)
                time.sleep(0.01)
                
    def reset_window_pos(self):  
        #重新设置指定标题弹窗的位置
        #函数原文
        targetTitle = self.title
        hWndList = []  
        win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)  
        temp_ins = 0
       
        for hwnd in hWndList:
            # clsname = win32gui.GetClassName(hwnd)
            title = win32gui.GetWindowText(hwnd)
            
            if (title.find(targetTitle) >= 0):    
                temp_ins = temp_ins + 1
                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)
                time.sleep(0.01)
                
#定义一个实例测试一下

dh1 = windowtroops(40, "System firewall warning","Process encountered an error.",12.5)
#生成弹窗的数量:40个
#弹窗的标题设置为:Process encountered an error. 
#弹窗的内容为:System firewall warning
#弹窗的显示时间:12.5秒  (理论上是12.5s,有卡顿的话就不一定了,12.5秒左右吧)

dh1.setStep(-23, 0)
#设置窗口阵列偏移为:左右方向上-向左偏移23个像素,上下方向上偏移。(所以这里的最终效果是会显示横着一排弹窗)

dh1.resetWindow(0, 600, 320, 160)
#设置第一个弹窗的坐标为(0,600)
#设置弹窗的宽:320 高:160

dh1()
#开始显示弹窗

dh1.Destroywindows()
#销毁所有弹窗(按理说弹窗会到时间自己销毁,但有时候会有几个残余,加入此句保证弹窗全部关闭)

我是在Spyder中测试的,直接在Spyder中运行会很卡,直接在系统命令行中运行就流畅多了

python恶搞程序代码免费 python搞怪代码_python恶搞程序代码免费_02

单一实例效果看起来太枯燥了,所以多来几个:

dh1 = windowtroops(40, "System firewall warning","Process encountered an error.",12)
dh1.setStep(23, 0)
dh1.resetWindow(0, 600, 320, 160)
dh1()
# time.sleep(0.8)

dh2 = windowtroops(30, "System firewall warning2","Process encountered an error2.",8.5)
dh2.setStep(-13, 10)
dh2.resetWindow(800, 0, 320, 160)
dh2()

dh3 = windowtroops(30, "System firewall warning3","Process encountered an error3.",8)
dh3.setStep(23, 20)
dh3.resetWindow(25, 0, 320, 160)
dh3()

dh4 = windowtroops(40, "System firewall warning3","Process encountered an error4.",5)
dh4.setStep(0, -12)
dh4.resetWindow(700, 500, 320, 160)
dh4()
# time(7)
dh1.Destroywindows()
dh2.Destroywindows()
dh3.Destroywindows()
dh4.Destroywindows()

dh1.setStep(8, 20)
dh1.random_flag = 1 #设置弹窗出现位置为随机,不设置默认为阵列的形式
dh1.jg = 1.2  #这里是间隔时间,不设置的话会使用默认值。如果没有间隔时间弹窗会同时消失,加上间隔时间弹窗会一个接一个的消失,增加视觉效果
dh2.setStep(8, 20)
dh2.random_flag = 1
dh2.jg = 1.2
dh3.setStep(-20, -10)
dh3.resetWindow(1800, 600, 320, 160)

dh1.num  = 30
dh1.jg = 1.5
dh1.life = 6
dh1()
dh3()
dh2.num = 50
dh2()
dh1.Destroywindows()
dh3.Destroywindows()
dh2.Destroywindows()

效果如下:

python恶搞程序代码免费 python搞怪代码_python恶搞程序代码免费_03