【C#】聊天气泡和搜索框的实现

气泡虽然是用的别人写好的方法,自己做的东西很少,大多是学习
但是完成了学习编程以来一直的梦想,做一个好看点的聊天界面

思路:借鉴自C# winform socket 初学者示例

大佬的示例下载链接:
https://pan.baidu.com/s/1i6r0Sa1JNp0-BCMEX1L6Hw

提取码:v751

注释及说明:https://www.cnblogs.com/qiaoke/p/6358050.html

界面组成:头像+气泡

头像使用PictureBox,绘制成圆形图片
气泡使用圆角矩形+尖角,创建panel使用bitemap填充

最终效果:
在这里插入图片描述

界面布局:panel+pictureBox

在这里插入图片描述

主要方法:

绘制圆角矩形和圆图片:(学习自上面示例)

        public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
        {
            //四边圆角
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(x, y, radius, radius, 180, 90);
            gp.AddArc(width - radius, y, radius, radius, 270, 90);
            gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
            gp.AddArc(x, height - radius, radius, radius, 90, 90);
            gp.CloseAllFigures();
            return gp;
        }      

        //绘制圆图片
        private Image CutEllipse(Image img, Rectangle rec, Size size)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
                {
                    br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.FillEllipse(br, new Rectangle(Point.Empty, size));
                }
            }     
            return bitmap;
        }

搜索框界面思路:

窗体的resize属性修改窗体大小
根据目前的窗体大小调整

窗体大小先设置为不可更改,以后在完善

		private void button2_Click(object sender, EventArgs e)
        {
            if(Form1.form1.Size.Width == 654)
            {
                Form1.form1.Size = new System.Drawing.Size(954, 558);
                Form1.form1.REDI_HISTORY.Visible = true;
                Form1.form1.LAB_SEARCH.Visible = true;
                Form1.form1.EDI_SEARCH.Visible = true;
                Form1.form1.BTN_SEARCH.Visible = true;

                Form1.form1.EDI_SEARCH.Focus();

                //显示在最下方
                Form1.form1.REDI_HISTORY.Select(Form1.form1.REDI_HISTORY.TextLength, 0);
                Form1.form1.REDI_HISTORY.ScrollToCaret();
            }
            else
            {
                Form1.form1.REDI_HISTORY.Visible = false;
                Form1.form1.LAB_SEARCH.Visible = false;
                Form1.form1.EDI_SEARCH.Visible = false;
                Form1.form1.BTN_SEARCH.Visible = false;
                Form1.form1.Size = new System.Drawing.Size(654, 558);

                EDI_SEARCH.Clear();
                Form1.form1.REDI_MESSAGE.Focus();
            }
        }
搜索框界面布局:

在这里插入图片描述

搜索框最终效果:

服务端使用mono放在Linux下
在这里插入图片描述
文字搜索
在这里插入图片描述
数字搜索
在这里插入图片描述

搜索框代码实现:

倒排索引什么的不会弄,所以只能用正则将就一下了…

数字的搜素区别不大,只是正则表达式变一下
"\w{3}:\d{4}/\d{1,2}/\d{1,2} \d{1,2}:\d{2}:\d{2}"

				List<string> info = new List<string>();
                //string pattern = @"\w{0,}[" + searchInfo + @"]{" + searchInfo.Length + @"}\w{0,}";
                string pattern = @"\w{0,}" + searchInfo + @"\w{0,}";

                foreach (Match each in Regex.Matches(history, pattern))
                {
                    info.Add(each.Value);
                }

                //获取索引
                List<int> indexList = new List<int>();
                for (int i=0;i<info.Count;++i)
                {
                    indexList.Add(getIndex(info[i], history));
                }                     

                //获取用户名和时间信息
                ArrayList nameAndTime = new ArrayList();
                ArrayList tempList = new ArrayList();
                for (int i = 0; i < indexList.Count; ++i)
                {
                    if((int)indexList[i] == -1)
                    {
                        continue;
                    }
                    string temp = history.Substring(0, (int)indexList[i]);
                    foreach (Match each in Regex.Matches(temp, @"\w{3}\:\d{4}\/\d{1,2}\/\d{1,2} \d{2}\:\d{2}\:\d{2}"))
                    {
                        tempList.Add(each.Value);
                    }
                    nameAndTime.Add(tempList[tempList.Count - 1]);
                    tempList.Clear();
                }

                //刷新历史记录框
                REDI_HISTORY.Clear();
                for(int i=0;i<info.Count;++i)
                {
                    REDI_HISTORY.AppendText(nameAndTime[i].ToString() + "\r\n");
                    REDI_HISTORY.AppendText(info[i] + "\r\n");

                    if (nameAndTime[i].ToString().Substring(0, 3) == user.username)
                    {
                        chatm.changeColorHistory(nameAndTime[i].ToString(), Color.Green);
                    }
                    else
                    {
                        chatm.changeColorHistory(nameAndTime[i].ToString(), Color.Blue);
                    }

                    chatm.changeColorHistory(searchInfo, Color.Red);
                }
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值