在工作中,有的时候需要做一些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();
}
这样就能实现啦
实现效果如下
喜欢的加个收藏哟