C#仿QQ聊天窗口

本文實例為大傢分享瞭C#實現QQ聊天窗口的具體代碼,供大傢參考,具體內容如下

效果圖:

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 聊天窗口
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int x, y;// x為窗體left,y為窗體top
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2, Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2);
            x = this.Left;
            y = this.Top;
            this.AcceptButton = button2; //按回車發送
            textBox1.ReadOnly = true; // 禁止輸入
            textBox1.ScrollBars = ScrollBars.Vertical; //顯示垂直滾動條
            textBox1.Font = new Font("", 16);
            textBox2.Font = new Font("", 16); 
        }
        int t = 5, space = 10;// t為暫停時間,space為移動距離

        //抖動
        private void button1_Click(object sender, EventArgs e)
        {  // 循環抖動
            for (int i = 0; i <5; i++)
            {  // 改變窗體位置
                this.Location = new Point(x - space, y);
                Thread.Sleep(t);
                this.Location = new Point(x - space, y - space);
                Thread.Sleep(t);
                this.Location = new Point(x, y - space);
                Thread.Sleep(t);
                this.Location = new Point(x + space, y - space);
                Thread.Sleep(t);
                this.Location = new Point(x + space, y);
                Thread.Sleep(t);
                this.Location = new Point(x + space, y + space);
                Thread.Sleep(t);
                this.Location = new Point(x, y + space);
                Thread.Sleep(t);
                this.Location = new Point(x - space, y + space);
                Thread.Sleep(t);
                this.Location = new Point(x - space, y);
                Thread.Sleep(t);
                this.Location = new Point(x, y);
            }
        }
        // 發送
        private void button2_Click(object sender, EventArgs e)
        { 
            // 記錄超出文本框高度 最新消息顯示
            textBox1.AppendText(DateTime.Now + "\r\n" + textBox2.Text + "\r\n");
            textBox2.Text = " ";// 文本框清空
        }

    }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: