参考下:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace 活动窗体震动 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } internal struct RECT { public int left; public int top; public int right; public int bottom; } [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] internal static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)] internal static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)] internal static extern void MoveWindow(IntPtr hwnd,int X,int Y,int nWidth,int nHeight,bool bRepaint); IntPtr id; RECT Rect = new RECT(); private void timer1_Tick(object sender, EventArgs e) { id = GetForegroundWindow(); Random myRandom = new Random(); GetWindowRect(id, ref Rect); MoveWindow(id, myRandom.Next(1024), myRandom.Next(768), Rect.right-Rect.left, Rect.bottom-Rect.top, true); }
用个while循环就可以了啊
可看看myqq
参考
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 活动窗体震动
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} internal struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
} [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
internal static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
internal static extern void MoveWindow(IntPtr hwnd,int X,int Y,int nWidth,int nHeight,bool bRepaint);
IntPtr id;
RECT Rect = new RECT();
private void timer1_Tick(object sender, EventArgs e)
{
id = GetForegroundWindow();
Random myRandom = new Random();
GetWindowRect(id, ref Rect);
MoveWindow(id, myRandom.Next(1024), myRandom.Next(768), Rect.right-Rect.left, Rect.bottom-Rect.top, true);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
int recordx = this.Left;
int recordy = this.Top;
Random random = new Random();
for (int i = 0; i < 30; i++)
{
int x = random.Next(10);
int y = random.Next(10);
if (x % 2 == 0)
{
this.Left = this.Left + x;
}
else
{
this.Left = this.Left - x;
}
if (y % 2 == 0)
{
this.Top = this.Top + y;
}
else
{
this.Top = this.Top - y;
} }
this.Left = recordx;
this.Top = recordy;
}通过控制for 循环,x,y的随机种子,还有this.Left = recordx;
this.Top = recordy; 是否在循环里,可以达到不同的效果