今天在优快云论坛上看到一个帖子,闲来无事就顺手做了一个,现在与大家分享。
由于.net的语法想实现控制其他窗体,不用WindowsAPI是无法实现的,所以需要用到如下API:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
下面就是调用了,直接上代码~~
private void Form2_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 100;
timer1.Start();
button1_Click(sender,e);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Tick += (sender1, e1) =>
{
findWindows();
};
}
private void findWindows()
{
RECT rect = new RECT();
Random rd = new Random();
int intZ = rd.Next(10,20);
intptr = GetForegroundWindow();
GetWindowRect(intptr, out rect);
MoveWindow(intptr, rect.Left - intZ, rect.Top - intZ, rect.Right - rect.Left, rect.Bottom - rect.Top, true);
Thread.Sleep(intZ);
MoveWindow(intptr, rect.Left, rect.Top - intZ, rect.Right - rect.Left, rect.Bottom - rect.Top, true);
Thread.Sleep(intZ);
MoveWindow(intptr, rect.Left + intZ, rect.Top + intZ, rect.Right - rect.Left, rect.Bottom - rect.Top, true);
Thread.Sleep(intZ);
MoveWindow(intptr, rect.Left+intZ, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, true);
Thread.Sleep(intZ);
MoveWindow(intptr, rect.Left + intZ, rect.Top + intZ, rect.Right - rect.Left, rect.Bottom - rect.Top, true);
Thread.Sleep(intZ);
MoveWindow(intptr, rect.Left, rect.Top + intZ, rect.Right - rect.Left, rect.Bottom - rect.Top, true);
Thread.Sleep(intZ);
MoveWindow(intptr, rect.Left-intZ, rect.Top + intZ, rect.Right - rect.Left, rect.Bottom - rect.Top, true);
Thread.Sleep(intZ);
MoveWindow(intptr, rect.Left - intZ, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, true);
Thread.Sleep(intZ);
MoveWindow(intptr, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, true);
}
最后附上今天本游戏的源码,免费下载~还附带了一个今天上午一起做的鼠标无法点击到控件的小游戏~

本文分享了一个使用.NET控制其他窗体并实现窗口移动的小游戏,通过调用API来操作窗口位置,同时附带了源代码和一个额外的游戏。详细介绍了如何利用.NET框架和WindowsAPI来实现窗口的控制与移动。
428

被折叠的 条评论
为什么被折叠?



