C#實現QQ聊天窗口
本文實例為大傢分享瞭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(); } } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。