forked from sumy7/DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighLightTextbox.cs
More file actions
104 lines (97 loc) · 3.63 KB
/
HighLightTextbox.cs
File metadata and controls
104 lines (97 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace DesignPattern
{
public partial class HighLightTextbox : UserControl
{
public 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);
brush.Color = Color.Green;//重置画笔颜色
//
//绘制行号
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--)
{
g.DrawString((i + 1).ToString(), font, brush, brushX, brushY);
brushY -= lineSpace;
}
g.Dispose();
font.Dispose();
brush.Dispose();
}
public HighLightTextbox()
{
InitializeComponent();
panel1.Left = 0;
panel1.Top = 0;
panel1.Height = this.Height;
richTextBox1.Left = panel1.Width;
richTextBox1.Top = 0;
richTextBox1.Height = this.Height;
richTextBox1.Width = this.Width - panel1.Width;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
showLineNo();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
panel1.Invalidate();
//HighLightText();
}
private void HighLightTextbox_Load(object sender, EventArgs e)
{
richTextBox1.BackColor = Color.White;
richTextBox1.WordWrap = false;
}
private void richTextBox1_HScorll(object sender, EventArgs e)
{
}
private void richTextBox1_VScroll(object sender, EventArgs e)
{
showLineNo();
}
}
}