窗口抖动

在网上看了看大牛的代码,自己琢磨了一下,然后自己写写并改进该进,这个可以用来恶搞的!得意

#include<time.h>
#include<windows.h>
#include<iostream>
#include<string>
using namespace std;
void StartShake()
{
	srand((unsigned)time(NULL));
	int sysX=GetSystemMetrics(SM_CXSCREEN);
	int sysY=GetSystemMetrics(SM_CYSCREEN);
	while(TRUE)
	{
		HWND hWnd=GetDesktopWindow();
		for(hWnd=GetWindow(hWnd,GW_CHILD);hWnd;hWnd=GetWindow(hWnd,GW_HWNDNEXT))
		{
			RECT rect;
			long style=GetWindowLong(hWnd,GWL_STYLE);
			if(style & WS_VISIBLE)
			{
				GetWindowRect(hWnd,&rect);
				int cX,cY,posX,posY;
				bool flag=!(rand()%2);
				cX=rect.right-rect.left;
				cY=rect.bottom-rect.top;
				if(flag)
				{
					posX=rect.left+rand()%10*5;
                    posY=rect.top+rand()%10*5;
				}
				else
				{
					posX=rect.left-rand()%10*5;
                    posY=rect.top-rand()%10*5;
				}
				if(posX<0)  posX=0;
                if(posY<0)  posY=0;
                if(posX+cX>sysX)
                    posX=sysX-cX;
                if(posY+cY>sysY)
                    posY=sysY-cY;
				SetWindowPos(hWnd,HWND_NOTOPMOST,posX,posY,rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOACTIVATE );
			}
		}
	}
}
int main()
{
	cout<<"这回比较厉害,静候17秒吧。。。。"<<endl; Sleep(1000);
	cout<<"奇迹马上出现。。。。。。"<<endl;
	Sleep(10000); 
	cout<<"  5"<<endl;Sleep(1000);
	cout<<"  4"<<endl;Sleep(1000);
	cout<<"  3"<<endl;Sleep(1000);
	cout<<"  2"<<endl;Sleep(1000);
	cout<<"  1"<<endl;Sleep(1000);
	cout<<"见证奇迹的时刻!"<<endl;Sleep(1000);
	StartShake();
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Java窗口抖动,可以使用Java的AWT和Swing库中的特定方法。以下是一个简单的示例程序,可以让窗口在屏幕上抖动: ```java import java.awt.*; import javax.swing.*; public class WindowShaker { public static void shake(Window window) { final int SHAKE_DISTANCE = 5; //抖动的距离 final int SHAKE_DURATION = 1000; //抖动的持续时间 final Point startPosition = window.getLocationOnScreen(); //窗口的起始位置 final long startTime = System.currentTimeMillis(); //抖动开始时间 Timer shakeTimer = new Timer(25, event -> { long elapsed = System.currentTimeMillis() - startTime; double waveOffset = (elapsed / (double) SHAKE_DURATION) * Math.PI; int yOffset = (int) (Math.sin(waveOffset) * SHAKE_DISTANCE); window.setLocation(startPosition.x, startPosition.y + yOffset); if (elapsed >= SHAKE_DURATION) { ((Timer) event.getSource()).stop(); window.setLocation(startPosition); } }); shakeTimer.start(); } public static void main(String[] args) { JFrame frame = new JFrame("Window Shaker"); frame.setSize(300, 200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton shakeButton = new JButton("Shake!"); shakeButton.addActionListener(event -> shake(frame)); frame.add(shakeButton, BorderLayout.CENTER); frame.setVisible(true); } } ``` 在上面的示例程序中,我们使用了Java的Timer类来定期更新窗口的位置,使之抖动。在抖动的过程中,我们使用了正弦函数来计算窗口的偏移量,以产生平滑的抖动效果。当抖动完成后,我们将窗口的位置还原到起始位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值