突然想起來flash有碰撞反彈飄動as控制的效果,所以想起來用c#也來做一個案頭飄動碰撞反彈無標題列表單。有點像中了惡意病毒廣告效果。
主要代碼如下(使用了一timer控制項和一Button(為了我自己控制),表單的BorderStyle設定為None):
複製代碼 代碼如下: int ScreenWidth = SystemInformation.PrimaryMonitorMaximizedWindowSize.Width;
int ScreenHeight = SystemInformation.PrimaryMonitorMaximizedWindowSize.Height;
private int speedX = 4;
private int speedY = 3;
private bool canMove = true;
int myswitch = 1;//為了我可以控制停止所以添加的飄與停的切換開關
private void timer1_Tick(object sender, EventArgs e)
{
if (canMove)
{
this.DesktopLocation = new Point(this.DesktopLocation.X + speedX, this.DesktopLocation.Y + speedY);
if (this.DesktopLocation.X + this.Width >= ScreenWidth || this.DesktopLocation.X < 0)
{
speedX = -speedX;
}
if (this.DesktopLocation.Y + this.Height >= ScreenHeight || this.DesktopLocation.Y < 0)
{
speedY = -speedY;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
myswitch *= -1;
if (myswitch == -1)
{
canMove = false;
//button1.Text = "飄動";
}
else
{
canMove = true;
//button1.Text = "停止";
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_DoubleClick(object sender, EventArgs e)
{
Application.Exit();
}
暫寫這麼多,有時間把它再增強下更像惡意廣告。~