C#實現(xiàn)簡化QQ聊天窗口
更新時間:2022年02月11日 14:20:19 作者:小白你咋讓人拴住了
這篇文章主要為大家詳細介紹了C#實現(xiàn)簡化QQ聊天窗口,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#實現(xiàn)簡化QQ聊天窗口的具體代碼,供大家參考,具體內(nèi)容如下
如圖樣式,詳細步驟如下
整個窗體設置
private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? this.BackColor = Color.Chocolate;//設置窗體背景顏色 ? ? ? ? ? ? this.Text = "與張某正在聊天...";//設置窗體文本內(nèi)容 ? ? ? ? ? ? this.Size = new Size(450,400);//設置窗體大小 ? ? ? ? ? ? //設置窗體在工作區(qū)居中顯示 ? ? ? ? ? ? this.Location = new ?Point(Screen.PrimaryScreen.WorkingArea.Width/2-this.Width/2,Screen.PrimaryScreen.WorkingArea.Height/2-this.Height/2) ; ? ? ? ? }
添加兩個textbox分別為聊天內(nèi)容與輸入框;
添加兩個button分別為抖一抖與發(fā)送;
抖動事件
private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? //抖動事件 ? ? ? ? ? ? int x = this.Left; ? ? ? ? ? ? int y = this.Top; ? ? ? ? ? ? for (int n = 0; n < 3; n++) ? ? ? ? ? ? { ? ?//添加using System.Threading; ? ? ? ? ? ? ? ? this.Location = new Point(x - 3, y); ? ? ? ? ? ? ? ? Thread.Sleep(20);//掛起20毫秒 ? ? ? ? ? ? ? ? this.Location = new Point(x - 3, y - 3); ? ? ? ? ? ? ? ? Thread.Sleep(20); ? ? ? ? ? ? ? ? this.Location = new Point(x, y - 3); ? ? ? ? ? ? ? ? Thread.Sleep(20); ? ? ? ? ? ? ? ? this.Location = new Point(x + 3, y - 3); ? ? ? ? ? ? ? ? Thread.Sleep(20); ? ? ? ? ? ? ? ? this.Location = new Point(x + 3, y + 3); ? ? ? ? ? ? ? ? Thread.Sleep(20); ? ? ? ? ? ? ? ? this.Location = new Point(x, y + 3); ? ? ? ? ? ? ? ? Thread.Sleep(20); ? ? ? ? ? ? ? ? this.Location = new Point(x - 3, y + 3); ? ? ? ? ? ? ? ? Thread.Sleep(20); ? ? ? ? ? ? ? ? this.Location = new Point(x - 3, y); ? ? ? ? ? ? ? ? Thread.Sleep(20); ? ? ? ? ? ? ? ? this.Location = new Point(x, y); ? ? ? ? ? ? } ? ? ? ? }
發(fā)送事件
private void button2_Click(object sender, EventArgs e) ? ? ? ? { ? ?//發(fā)送時間 ? ? ? ? ? ? if (textBox2.Text!="")//當輸入欄不為空內(nèi)容時 ? ? ? ? ? ? { ? //textbox1內(nèi)容等于textbox1原本內(nèi)容(聊天記錄)+現(xiàn)在的時間+發(fā)話人+textbox2的輸入內(nèi)容 ? ? ? ? ? ? ? ? textBox1.Text = textBox1.Text + DateTime.Now + "\r\n" + "李某:"+textBox2.Text+"\r\n"; ? ? ? ? ? ? ? ? textBox2.Text= "";//清空輸出框 ? ? ? ? ? ? } ? ? ? ? }
添加滾動條
private void textBox1_TextChanged(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //在textbox1屬性設置scrollbars滾動條顯示 ? ? ? ? ? ? //滾輪顯示最后一行 ? ? ? ? ? ? this.textBox1.SelectionStart = this.textBox1.Text.Length; ? ? ? ? ? ? this.textBox1.ScrollToCaret(); ? ? ? ? ? ? //設置lcon類型圖標 ? ? ? ? }
添加鍵盤事件
(Enter實現(xiàn)發(fā)送功能)
private void textBox2_KeyDown(object sender, KeyEventArgs e) ? ? ? ? { ?//在輸入框內(nèi)添加鍵盤事件,Enter實現(xiàn)發(fā)送功能 ? ? ? ? ? ? if (e.KeyCode == Keys.Enter) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? button2_Click(sender, e); ? ? ? ? ? ? } ? ? ? ? }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
無法從 int? 轉換為 int 運行時出現(xiàn)錯誤
無法從"int?"轉換為"int" ,在運行時會出現(xiàn)錯誤,通過強制類型轉換(int)便可解決2014-05-05C#實現(xiàn)ComboBox控件顯示出多個數(shù)據(jù)源屬性的方法
這篇文章主要介紹了C#實現(xiàn)ComboBox控件顯示出多個數(shù)據(jù)源屬性的方法,實例分析了ComboBox控件的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09