C#实现QQ聊天窗口

作者:Just?Do?Its 时间:2023-07-06 02:43:56 

本文实例为大家分享了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();
        }
    }
}

来源:https://blog.csdn.net/liu991029/article/details/105463767

标签:C#,聊天窗口
0
投稿

猜你喜欢

  • 完美解决IDEA Ctrl+Shift+f快捷键突然无效的问题

    2021-11-23 06:36:19
  • Java实战之酒店人事管理系统的实现

    2021-06-02 16:44:37
  • JDK 7 新特性小结实例代码解析

    2022-04-18 13:03:07
  • Java编程实现基于用户的协同过滤推荐算法代码示例

    2022-09-06 12:44:24
  • Intent传递对象之Serializable和Parcelable的区别

    2022-05-27 04:02:42
  • Java掩码的几种使用例举

    2022-02-11 11:08:04
  • MyBatis注解CRUD与执行流程深入探究

    2023-07-03 06:19:44
  • C#实现将Email地址转成图片显示的方法

    2022-09-26 09:35:36
  • 通过java.util.TreeMap源码加强红黑树的理解

    2021-07-27 08:45:59
  • Android自定义UI实现微信语音

    2022-04-18 10:47:41
  • Android中FontMetrics的几个属性全面讲解

    2023-11-14 14:57:20
  • C# 调用 JavaWebservice服务遇到的问题汇总

    2023-04-23 04:00:49
  • Android仿美团淘宝实现多级下拉列表菜单功能

    2022-07-24 18:42:18
  • 深入了解Hadoop如何实现序列化

    2023-10-13 10:33:43
  • Spring自动注入失败的解决方法

    2022-08-13 03:41:31
  • IntelliJ IDEA运行SpringBoot项目的详细步骤

    2022-04-29 00:59:08
  • Android 中通过ViewDragHelper实现ListView的Item的侧拉划出效果

    2021-08-14 18:37:46
  • WPF开发技巧之花式控件功能扩展详解

    2022-07-13 05:56:52
  • C#使用文件流FileStream和内存流MemoryStream操作底层字节数组byte[]

    2023-09-04 00:30:55
  • spring boot devtools在Idea中实现热部署方法

    2023-07-20 02:31:25
  • asp之家 软件编程 m.aspxhome.com