C#制作闪动的窗体
本文讲述如何使用C#创建闪动的窗体。
新建Windows窗体应用程序,添加按钮点击事件。全部程序如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace _2_fun_with_if_else
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
while (Visible) // 关闭窗体时,停止循环
{
for (int c = 0; c < 254 && Visible; c++)
{
this.BackColor = Color.FromArgb(c, 255 - c, c); // 此方法指定三个数字:red/green/blue.
Application.DoEvents(); // 此语句使操作系统能够在程序之外执行其他操作。否则
// 程序将占用所有CPU周期
Thread.Sleep(3); // 此语句在循环中插入3毫秒的延迟。
}
for (int c = 254; c >= 0 && Visible; c--)
{
this.BackColor = Color.FromArgb(c, 255 - c, c);
Application.DoEvents();
Thread.Sleep(3);
}
}
}
}
}
运行后效果如图: