richtextbox 中关于中文链接的乱码解决

最近在实现一个类似QQ样的东东.和richtextbox打了不少交道,目前解决了一部分问题,

在插入中文链接时 会导致整个richtextbox的中文内容出现乱码.这个问题其实还是编码引起的.一开始时我也在网上找了一段时间,却发现没有相应的方案来处理.

public void InsertLink(string text, string hyperlink, int position)
{
if (position < 0 || position > this.Text.Length)
throw new ArgumentOutOfRangeException("position");
this.SelectionStart = position;
this.SelectedRtf = @"{\rtf1\ansi " + text + @"\v #" + hyperlink + @"\v0}";
this.Select(position, text.Length + hyperlink.Length + 1);
this.SetSelectionLink(true);
this.Select(position + text.Length + hyperlink.Length + 1, 0);
}
在selectedrtf中改一下编码即可实现中文链接的处理
this.SelectedRtf = @"{\rtf1\ansicpg936 " + text + @"\v #" + hyperlink + @"\v0}";
这个我们就有了中文的链接,如果要处理链接的事件,只要注册一下richtextbox的连接事件LinkClicked
void gRichTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
这里可以获取相应的连接文本.
}
中文连接的问题解决了,下面要解决的问题就是当点了连接后,如果修改连接的内容.事件提供的参数是不可修改的.
winform中帮我分析下以下代码:会出现服务器自动断开连接时。主线程窗口会闪退或卡死。private void TCPserver_open() { string IP_str = txt_ccdIP.Text; ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //声明负责通信的套接字 try { IPAddress IP = IPAddress.Parse(IP_str); //获取设置的IP地址 int Port = int.Parse(txt_ccdPort.Text); //获取设置的端口号 IPEndPoint iPEndPoint = new IPEndPoint(IP, Port); //指定的端口号和服务器的ip建立一个IPEndPoint对象 ClientSocket.Connect(iPEndPoint); //用socket对象的Connect()方法以上面建立的IPEndPoint对象做为参数,向服务器发出连接请求 SFlag = 1; //若连接成功将标志设置为1 //开启一个线程接收数据 th1 = new Thread(TCPserver_Receive); th1.IsBackground = true; th1.Start(ClientSocket); lbl_connectState_ccd.Text = "连接"; lbl_connectState_ccd.BackColor = Color.Lime; canshu.WriteIni("参数1", "相机IP", txt_ccdIP.Text, INI_systemParameter); canshu.WriteIni("参数1", "相机IP端口", txt_ccdPort.Text, INI_systemParameter); } catch { lbl_connectState_ccd.Text = "断开"; lbl_connectState_ccd.BackColor = Color.Red; message_add(0, "相机尝试连接--失败"); } } //关闭 private void TCPserver_close() { //保证是在连接状态下退出 if (SFlag == 1) { byte[] send = new byte[1024]; send = Encoding.ASCII.GetBytes("*close*"); //关闭客户端时给服务器发送一个退出标志 ClientSocket.Send(send); th1.Abort(); //关闭线程 ClientSocket.Close(); //关闭套接字 SFlag = 0; //客户端退出后将连接成功标志程序设置为0 lbl_connectState_ccd.Text = "断开"; lbl_connectState_ccd.BackColor = Color.Red; } } //接 private void TCPserver_Receive(Object sk) { Socket socketRec = sk as Socket; while (true) { //5.接收数据 byte[] receive = new byte[1024]; int r = ClientSocket.Receive(receive); //调用Receive()接收字节数据 //6.打印接收数据 if (r == 0) { break; } if (receive.Length > 0) { for (int i = 0; i < receive.Length; i++) { if (receive[i] == 0) { receive[i] = 32; } } TCP_re_str = Encoding.ASCII.GetString(receive); //将字节数据根据ASCII码转成字符串 //VIE_re = Encoding.UTF8.GetString(receive); //使用UTF8编码接收中文不会乱码 TCP_re_str = TCP_re_str.Trim();//去除字符串的首尾空格 } } } //发送 private void TCPserver_send(string data) { TCP_re_str = ""; //只有连接成功了才能发送数据,接收部分因为是连接成功才开启线程,所以不需要使用标志 if (SFlag == 1) { byte[] send = new byte[1024]; //send = Encoding.ASCII.GetBytes(richTextBox_Send.Text); //将文本内容转换成字节发送 send = Encoding.UTF8.GetBytes(data); //解决中文乱码问题 try { ClientSocket.Send(send); //调用Send()函数发送数据 } catch { SFlag = 0; lbl_connectState_ccd.Text = "断开"; lbl_connectState_ccd.BackColor = Color.Red; message_add(1, "发送字符→相机:失败,检查后请重启软件"); return; } } }
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值