forked from PavelTorgashov/FastColoredTextBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuler.cs
More file actions
138 lines (110 loc) · 4.55 KB
/
Copy pathRuler.cs
File metadata and controls
138 lines (110 loc) · 4.55 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace FastColoredTextBoxNS
{
public partial class Ruler : UserControl
{
public EventHandler TargetChanged;
[DefaultValue(typeof(Color), "ControlLight")]
public Color BackColor2 { get; set; }
[DefaultValue(typeof(Color), "DarkGray")]
public Color TickColor { get; set; }
[DefaultValue(typeof(Color), "Black")]
public Color CaretTickColor { get; set; }
FastColoredTextBox target;
[Description("Target FastColoredTextBox")]
public FastColoredTextBox Target
{
get { return target; }
set
{
if (target != null)
UnSubscribe(target);
target = value;
Subscribe(target);
OnTargetChanged();
}
}
public Ruler()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
MinimumSize = new Size(0, 24);
MaximumSize = new Size(int.MaxValue/2, 24);
BackColor2 = SystemColors.ControlLight;
TickColor = Color.DarkGray;
CaretTickColor = Color.Black;
}
protected virtual void OnTargetChanged()
{
if (TargetChanged != null)
TargetChanged(this, EventArgs.Empty);
}
protected virtual void UnSubscribe(FastColoredTextBox target)
{
target.Scroll -= new ScrollEventHandler(target_Scroll);
target.SelectionChanged -= new EventHandler(target_SelectionChanged);
target.VisibleRangeChanged -= new EventHandler(target_VisibleRangeChanged);
}
protected virtual void Subscribe(FastColoredTextBox target)
{
target.Scroll += new ScrollEventHandler(target_Scroll);
target.SelectionChanged += new EventHandler(target_SelectionChanged);
target.VisibleRangeChanged += new EventHandler(target_VisibleRangeChanged);
}
void target_VisibleRangeChanged(object sender, EventArgs e)
{
Invalidate();
}
void target_SelectionChanged(object sender, EventArgs e)
{
Invalidate();
}
protected virtual void target_Scroll(object sender, ScrollEventArgs e)
{
Invalidate();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
if (target == null)
return;
Point car = PointToClient(target.PointToScreen(target.PlaceToPoint(target.Selection.Start)));
Size fontSize = TextRenderer.MeasureText("W", Font);
int column = 0;
e.Graphics.FillRectangle(new LinearGradientBrush(new Rectangle(0, 0, Width, Height), BackColor, BackColor2, 270), new Rectangle(0, 0, Width, Height));
float columnWidth = target.CharWidth;
var sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Near;
var zeroPoint = target.PositionToPoint(0);
zeroPoint = PointToClient(target.PointToScreen(zeroPoint));
using (var pen = new Pen(TickColor))
using (var textBrush = new SolidBrush(ForeColor))
for (float x = zeroPoint.X; x < Right; x += columnWidth, ++column)
{
if (column % 10 == 0)
e.Graphics.DrawString(column.ToString(), Font, textBrush, x, 0f, sf);
e.Graphics.DrawLine(pen, (int)x, fontSize.Height + (column % 5 == 0 ? 1 : 3), (int)x, Height - 4);
}
using (var pen = new Pen(TickColor))
e.Graphics.DrawLine(pen, new Point(car.X - 3, Height - 3), new Point(car.X + 3, Height - 3));
using (var pen = new Pen(CaretTickColor))
{
e.Graphics.DrawLine(pen, new Point(car.X - 2, fontSize.Height + 3), new Point(car.X - 2, Height - 4));
e.Graphics.DrawLine(pen, new Point(car.X, fontSize.Height + 1), new Point(car.X, Height - 4));
e.Graphics.DrawLine(pen, new Point(car.X + 2, fontSize.Height + 3), new Point(car.X + 2, Height - 4));
}
}
}
}