文章目录
思路:借鉴自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);
}