Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Как сделать так, чтобы форма бегала по экрану. Visual Studio C#

Roman Script Ученик (122), на голосовании 8 месяцев назад
Мне нужен код, к примеру как в вирусах/программ-шуток., форма бывало бегала по экрану. Плавно и беспрерывно. Нужно, чтобы при загрузке формы, форма бегала по экрану. или "Убегающая форма".
Голосование за лучший ответ
PuF Профи (576) 9 месяцев назад
using System;
using System.Drawing;
using System.Windows .Forms;

public partial class RunawayForm : Form
{
private Timer timer;
private Random random;

public RunawayForm()
{
InitializeComponent();
InitializeForm();
}

private void InitializeForm()
{
Width = 200;
Height = 100;
FormBorderStyle = FormBorderStyle.None;
StartPosition = FormStartPosition.Manual;
Location = GetRandomLocation();

timer = new Timer();
timer.Interval = 10;
timer.Tick += Timer_Tick;

random = new Random();
}

private Point GetRandomLocation()
{
return new Point( random.Next (Screen.PrimaryScreen.Bounds.Width - Width),
random.Next (Screen.PrimaryScreen.Bounds.Height - Height));
}

private void Timer_Tick(object sender, EventArgs e)
{
MoveForm();
}

private void MoveForm()
{
Location = new Point(Location.X + random.Next (-5, 6), Location.Y + random.Next (-5, 6));

if (Location.X < 0)
Location = new Point(0, Location.Y);
else if (Location.X + Width > Screen.PrimaryScreen.Bounds.Width)
Location = new Point(Screen.PrimaryScreen.Bounds.Width - Width, Location.Y);

if (Location.Y < 0)
Location = new Point(Location.X, 0);
else if (Location.Y + Height > Screen.PrimaryScreen.Bounds.Height)
Location = new Point(Location.X, Screen.PrimaryScreen.Bounds.Height - Height);
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
timer.Start();
}

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run (new RunawayForm());
}
}
Jurijus Zaksas Искусственный Интеллект (440727) 9 месяцев назад
Меняй ее координаты по таймеру и адью:

 int dx = 20; 
int dy = 10;
private void timer1_Tick(object sender, EventArgs e)
{
if (Location.X < 0 || (Location.X + Size.Width) > Screen.PrimaryScreen.Bounds.Width)
dx *= -1;
if (Location.Y < 0 || (Location.Y + Size.Height) > Screen.PrimaryScreen.Bounds.Height)
dy *= -1;
Location = new Point(Location.X + dx, Location.Y + dy);
}
Похожие вопросы