C#的事例应用

这篇博客介绍了C#的应用实例,包括使用单个timer实现气泡碰撞效果,详细讲解了设置气泡属性和碰撞逻辑;探讨了验证码的创建,展示了Form1_Load事件中设置界面背景和验证码文字的颜色;最后,讨论了如何利用textbox和button创建简单的聊天窗口,调整控件位置并定义交互行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一: 单个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();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值