一: 单个timer实现气泡碰撞
1):首先设置气泡的大小,颜色透明度,以及画圆
this.FormBorderStyle = FormBorderStyle.None;
this.Size = new Size(200, 200);
this.BackColor = Color.Red;
this.Opacity = 0.6;
this.Location = new Point(0, 0);
GraphicsPath path = new GraphicsPath();
path.AddEllipse(0, 0, 200, 200);
this.Region = new Region(path);
2):设置初始值:
int x = 10;
int y = 10;
3);单个timer实现过程
private void timer1_Tick(object sender, EventArgs e)
{
this.Left += x;
this.Top += y;
if (this.Top + this.Height >= Screen.PrimaryScreen.WorkingArea.Height || this.Top <= 0)
{
y *= -1;
Form2 f2 = new Form2();
f2.Show();
f2.BackColor = Color.Blue;
}
if (this.Left +this.Width >=Screen.PrimaryScreen.WorkingArea.Width ||this.Left <=0)
{
x *= -1; ;
}
}
二:验证码案例
通过button和label与控件有关联的文本设置验证码
验证码代码:
private void Form1_Load(object sender, EventArgs e)
{
Text = “验证码”;
this.BackColor = Color.Red;
label1.BackColor = Color.Green;
}
private void button1_Click(object sender, EventArgs e)
{
string ai = "";//验证码是随机的字符串。就是随机函数的对象
Random p = new Random();//表示伪随机数生成器
for (int i = 0; i < 4; i++)
{
int type = p.Next(1, 3);
if (type ==0)//大写字母
{
ai += ((char)p.Next(97, 123)).ToString();
this.BackColor = Color.Red;
}
if (type ==1)//小写字母
{
ai += ((char)p.Next(65, 91)).ToString();
}
if (type ==2)//数字
{
ai += p.Next(0, 10).ToString();
}
label1.Text = ai;
}
label1.ForeColor = Color.FromArgb(p.Next(0, 255), p.Next(0, 255), p.Next(0, 255));//字体颜色
}
三:鼠标光标定位
this.textBox1.Focus();
//选择文本框的文本范围
this.textBox1.Select(this.textBox1.TextLength, 0);
//将控件内容滚动到当前插入符号位置
this.textBox1.ScrollToCaret();
四:聊天窗口
用两个控件:textbox和button实现
首先:设置textbox1,2的位置,及功能,再设置button的功能,使其拥有相对应可操作的行为
private void Form2_Load(object sender, EventArgs e)
{
this.Width = 600;
this.Height = 500;
textBox1.Multiline = true; // Multiline 表示的是 是否可以多行显示
textBox1.Height = 200;
textBox1.ReadOnly = true;// ReadOnly 设置是否只读
textBox1.Width = 600;
textBox2.Multiline = true;
textBox2.Height = 100;
textBox2.Width = textBox1.Width; // 获取TextBox1的宽度 设置给 textBox2的属性width
this.AcceptButton = button1;//设置回车键发送消息
}
private void button1_Click(object sender, EventArgs e)
{
//消息发送
if (textBox2.Text != "")
{
//声明str,接收发送的信息
// String str = "";
// str += "罗东波" + DateTime.Now + "\r\n" + textBox2.Text + "\r\n";
//将新发送的信息每次都显示在最上面一行
// textBox1.Text = textBox1.Text.Insert(0, str);
textBox1.Text += "罗东波" + DateTime.Now + "\r\n" + textBox2.Text + "\r\n";
textBox2.Text = "";
}
else
{
MessageBox.Show("不能发送空的信息,请重新输入...");
}
}
private void button2_Click(object sender, EventArgs e)
{
//窗口抖动
int x = this.Left;
int y = this.Top;
for (int i = 1; i <= 2; i++)
{
textBox2.Text = "";
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
//设置光标定位
//为控件设置焦点
this.textBox1.Focus();
//选择文本框的文本范围
this.textBox1.Select(this.textBox1.TextLength, 0);
//将控件内容滚动到当前插入符号位置
this.textBox1.ScrollToCaret();
}