C#實現(xiàn)QQ聊天窗口
本文實例為大家分享了C#實現(xiàn)QQ聊天窗口的具體代碼,供大家參考,具體內(nèi)容如下
分析
- 需要兩個TextBox,一個用于顯示消息,一個用于編輯消息
- 需要四個按鈕,分別控制關(guān)閉程序,清空正在編輯的消息,發(fā)送消息,抖動
原理
1、在TextBox2中編輯消息并發(fā)送,在TextBox1中顯示所發(fā)送的消息的同時使TextBox2中的消息清空
2、發(fā)送的第一條消息TextBox1先保存并顯示,發(fā)送第二條消息時將TextBox1事先的消息先打印出來接著顯示第二條消息
3、抖動原理:使窗口的left以及top發(fā)生變化(圍繞窗體左上角為坐標(biāo)原點考慮)加上Thread線程和for循環(huán)從而實現(xiàn)窗口抖動效果
程序中用到的重要屬性
ReadOnly屬性:設(shè)置文本為只讀(true)textBox1.ReadOnly=true;
TabIndex屬性:設(shè)置光標(biāo)默認(rèn)出現(xiàn)在哪里(0)textBox1.TabIndex=0;
Multiline屬性:設(shè)置文本框可多行輸入textBox1.Multiline=true;
Datetime:獲取當(dāng)前時間
“\r\n” 換行
AcceptButton屬性:獲取或設(shè)置當(dāng)用戶按Enter鍵時所單擊的窗體上的按鈕this.AcceptButton = button2;
Thread類:創(chuàng)建和控制線程Thread.Sleep(10);//設(shè)置執(zhí)行完上一步停留時間
還需要創(chuàng)建命名空間using System.Threading;
Trim方法:textBox2.Text.Trim()=="")//Trim:移除當(dāng)前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;//設(shè)置多線程 namespace Test_QQ_chat_windows { ? ? public partial class Form1 : Form ? ? { ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? {? ? ? ? ? ? ? //設(shè)置聊天窗口居中 ? ? ? ? ? ? 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");//設(shè)置窗體背景圖 ? ? ? ? ? ? this.BackgroundImageLayout = ImageLayout.Stretch;//設(shè)置窗體背景圖拉伸 ? ? ? ? ? ? //textBox1設(shè)置只讀 ? ? ? ? ? ? textBox1.ReadOnly = true; ? ? ? ? ? ? //設(shè)置發(fā)送按鈕可以用Enter鍵來觸發(fā) ? ? ? ? ? ? this.AcceptButton = button2; ? ? ? ? ? ? this.Opacity = 0.8;//設(shè)置窗體透明度為0.2 ? ? ? ? ? ? textBox1.BackColor = Color.DeepSkyBlue; ? ? ? ? ? ? textBox2.BackColor = Color.DeepPink; ? ? ? ? } ? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? this.BackColor = Color.DeepSkyBlue;//設(shè)置窗體背景顏色 ? ? ? ? ? ? textBox1.Text += "深夜食堂(36522224)" + DateTime.Now + "\r\n"+ "\r\n" + "您發(fā)送了一個窗口抖動" + "\r\n"+"\r\n"; ? ? ? ? ? ? textBox2.Text = "";//設(shè)置發(fā)送內(nèi)容后textBox2中無內(nèi)容 ? ? ? ? ? ? //窗口抖動 ? ? ? ? ? ? int x = this.Left; ? ? ? ? ? ? int y = this.Top; ? ? ? ? ? ? for (int i = 0; i <= 3; i++)//設(shè)置抖動次數(shù) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? this.Location = new Point(x - 3, y); ? ? ? ? ? ? ? ? Thread.Sleep(10);//設(shè)置執(zhí)行完上一步停留時間 ? ? ? ? ? ? ? ? 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) ? ? ? ? { ? ? ? ? ? ? //消息發(fā)送 ? ? ? ? ? ? //判斷textbox2中有無內(nèi)容 ? ? ? ? ? ? if (textBox2.Text == ""||textBox2.Text.Trim()=="")//Trim:移除當(dāng)前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 = "";//設(shè)置發(fā)送內(nèi)容后textBox2中無內(nèi)容 ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? 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) ? ? ? ? { ? ? ? ? ? ? //設(shè)置起始點在最后消息處 ? ? ? ? ? ? this.textBox1.SelectionStart = this.textBox1.Text.Length; ? ? ? ? ? ? //內(nèi)容滾動到最后消息處 ? ? ? ? ? ? this.textBox1.ScrollToCaret(); ? ? ? ? } ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ftp服務(wù)器搭建部署與C#實現(xiàn)ftp文件的上傳的示例
本文主要介紹了ftp服務(wù)器搭建部署與C#實現(xiàn)ftp文件的上傳的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07C#開發(fā)Winform實現(xiàn)窗體間相互傳值
這篇文章介紹了C#開發(fā)Winform實現(xiàn)窗體間相互傳值的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03c# AES字節(jié)數(shù)組加密解密流程及代碼實現(xiàn)
這篇文章主要介紹了c# AES字節(jié)數(shù)組加密解密流程及代碼實現(xiàn),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11