本文实例为大家分享了C#实现QQ聊天窗口的具体代码,供大家参考,具体内容如下
分析
- 需要两个TextBox,一个用于显示消息,一个用于编辑消息
- 需要四个按钮,分别控制关闭程序,清空正在编辑的消息,发送消息,抖动
原理
1、在TextBox2中编辑消息并发送,在TextBox1中显示所发送的消息的同时使TextBox2中的消息清空
2、发送的第一条消息TextBox1先保存并显示,发送第二条消息时将TextBox1事先的消息先打印出来接着显示第二条消息
3、抖动原理:使窗口的left以及top发生变化(围绕窗体左上角为坐标原点考虑)加上Thread线程和for循环从而实现窗口抖动效果
程序中用到的重要属性
ReadOnly属性:设置文本为只读(true)textBox1.ReadOnly=true;
TabIndex属性:设置光标默认出现在哪里(0)textBox1.TabIndex=0;
Multiline属性:设置文本框可多行输入textBox1.Multiline=true;
Datetime:获取当前时间
“\r\n” 换行
AcceptButton属性:获取或设置当用户按Enter键时所单击的窗体上的按钮this.AcceptButton = button2;
Thread类:创建和控制线程Thread.Sleep(10);//设置执行完上一步停留时间
还需要创建命名空间using System.Threading;
Trim方法:textBox2.Text.Trim()=="")//Trim:移除当前textBox2对象位置前后的空白字符
具体代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading;//设置多线程 namespace Test_QQ_chat_windows { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //设置聊天窗口居中 this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2; this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2; //聊天窗口命名 this.Text = "和Know正在聊天"; this.BackgroundImage = Image.FromFile("../../img/timg.jpg");//设置窗体背景图 this.BackgroundImageLayout = ImageLayout.Stretch;//设置窗体背景图拉伸 //textBox1设置只读 textBox1.ReadOnly = true; //设置发送按钮可以用Enter键来触发 this.AcceptButton = button2; this.Opacity = 0.8;//设置窗体透明度为0.2 textBox1.BackColor = Color.DeepSkyBlue; textBox2.BackColor = Color.DeepPink; } private void button1_Click(object sender, EventArgs e) { this.BackColor = Color.DeepSkyBlue;//设置窗体背景颜色 textBox1.Text += "深夜食堂(36522224)" + DateTime.Now + "\r\n"+ "\r\n" + "您发送了一个窗口抖动" + "\r\n"+"\r\n"; textBox2.Text = "";//设置发送内容后textBox2中无内容 //窗口抖动 int x = this.Left; int y = this.Top; for (int i = 0; i <= 3; i++)//设置抖动次数 { this.Location = new Point(x - 3, y); Thread.Sleep(10);//设置执行完上一步停留时间 this.Location = new Point(x - 3, y - 3); Thread.Sleep(10); this.Location = new Point(x, y - 3); Thread.Sleep(10); this.Location = new Point(x + 3, y - 3); Thread.Sleep(10); this.Location = new Point(x + 3, y); Thread.Sleep(10); this.Location = new Point(x + 3, y + 3); Thread.Sleep(10); this.Location = new Point(x, y + 3); Thread.Sleep(10); this.Location = new Point(x - 3, y + 3); Thread.Sleep(10); this.Location = new Point(x - 3, y); Thread.Sleep(10); this.Location = new Point(x, y); } } private void button2_Click(object sender, EventArgs e) { //消息发送 //判断textbox2中有无内容 if (textBox2.Text == ""||textBox2.Text.Trim()=="")//Trim:移除当前textBox2对象位置前后的空白字符) { MessageBox.Show("输入不能为空值,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { textBox1.Text += "深夜食堂(36522224)" + DateTime.Now + "\r\n" + "\r\n" + textBox2.Text + "\r\n"+ "\r\n"; textBox2.Text = "";//设置发送内容后textBox2中无内容 } } private void button3_Click(object sender, EventArgs e) { this.Close(); } private void button4_Click(object sender, EventArgs e) { textBox2.Text = ""; } private void textBox1_TextChanged(object sender, EventArgs e) { //设置起始点在最后消息处 this.textBox1.SelectionStart = this.textBox1.Text.Length; //内容滚动到最后消息处 this.textBox1.ScrollToCaret(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。