Winform,RichTextBox,Json文本对比工具

在工作中,有的时候需要做一些Json对比的需求,通过肉眼去找到不一样的地方

当然,现在有很多可以做对比的小工具,比如Beyond Compare

但是这个有个弊端,如果对比的时候是个Json字符串,没有格式化的时候,做对比就很尴尬了

这个时候需要以下几步

1.首先将Json字符串复制到Format工具中分别format一下

2.复制Format好的文本到Compare中做对比

3.滑动到对比不一致的地方

个人比较懒,就自己写了一个小工具,用Winfrom写了一个对比的工具

对比没啥好说的

主要用到了RichTextbox同步滚动,给RichTextBox增加行号

RichTextBox同步滚动:

以下为自定义RichTextBox

public enum WindowsMessage
    {
        /// <summary>
        /// 横向滚动
        /// </summary>
        WM_HSCROLL = 0x0114,
        /// <summary>
        /// 垂直滚动
        /// </summary>
        WM_VSCROLL = 0x0115,
        /// <summary>
        /// 鼠标滑轮
        /// </summary>
        WM_MSCROLL = 0x020a
    }
    public delegate void SendMessage(Message msg);
    public class MyRichTextBox : System.Windows.Forms.RichTextBox
    {
        public event SendMessage SendMessageEvent;
        protected override void WndProc(ref Message m)
        {
            if ((int)WindowsMessage.WM_HSCROLL == m.Msg || (int)WindowsMessage.WM_VSCROLL == m.Msg|| (int)WindowsMessage.WM_MSCROLL == m.Msg)
            {
                if (SendMessageEvent != null)
                {
                    SendMessageEvent(m);
                }
            }
            base.WndProc(ref m);
        }
        public void Scroll(Message m)
        {
            m.HWnd = this.Handle;
            WndProc(ref m);
        }
    }

调用的代码:

  public Form1()
        {
            InitializeComponent();
            label1.Hide();
            richTextBox3.Hide();
            richTextBox1.SendMessageEvent += new SendMessage(c1_SendMessageEvent);

        }

        void c1_SendMessageEvent(Message msg)
        {
            richTextBox2.Scroll(msg);
        }

加上以上代码就能同步滚动啦

添加行号的实现:需要 一个Panal和一个RichTextBox

private void showLineNo()
        {
            //获得当前坐标信息
            Point p = this.richTextBox1.Location;
            int crntFirstIndex = this.richTextBox1.GetCharIndexFromPosition(p);

            int crntFirstLine = this.richTextBox1.GetLineFromCharIndex(crntFirstIndex);

            Point crntFirstPos = this.richTextBox1.GetPositionFromCharIndex(crntFirstIndex);

            p.Y += this.richTextBox1.Height;

            int crntLastIndex = this.richTextBox1.GetCharIndexFromPosition(p);

            int crntLastLine = this.richTextBox1.GetLineFromCharIndex(crntLastIndex);
            Point crntLastPos = this.richTextBox1.GetPositionFromCharIndex(crntLastIndex);

            //准备画图
            Graphics g = this.panel1.CreateGraphics();

            Font font = new Font(this.richTextBox1.Font, this.richTextBox1.Font.Style);

            SolidBrush brush = new SolidBrush(Color.Green);

            //画图开始

            //刷新画布

            Rectangle rect = this.panel1.ClientRectangle;
            brush.Color = this.panel1.BackColor;

            g.FillRectangle(brush, 0, 0, this.panel1.ClientRectangle.Width, this.panel1.ClientRectangle.Height);

            //绘制行号

            int lineSpace = 0;

            if (crntFirstLine != crntLastLine)
            {
                lineSpace = (crntLastPos.Y - crntFirstPos.Y) / (crntLastLine - crntFirstLine);

            }

            else
            {
                lineSpace = Convert.ToInt32(this.richTextBox1.Font.Size);

            }

            int brushX = this.panel1.ClientRectangle.Width - Convert.ToInt32(font.Size * 3);

            int brushY = crntLastPos.Y + Convert.ToInt32(font.Size * 0.21f);//惊人的算法啊!!
            for (int i = crntLastLine; i >= crntFirstLine; i--)
            {
                brush.Color = Color.Blue;//重置画笔颜色
                if (errorLineNumber.Contains(i + 1))
                {
                    brush.Color = Color.Red;//不对的标红
                }

                g.DrawString((i + 1).ToString(), font, brush, brushX, brushY);

                brushY -= lineSpace;
            }

            g.Dispose();

            font.Dispose();

            brush.Dispose();
        }

使用方法:

 private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            showLineNo();
        }
        private void richTextBox1_VScroll(object sender, EventArgs e)
        {
            showLineNo();
        }

这样就能实现啦

实现效果如下

喜欢的加个收藏哟 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值