目录
实践过程
效果
代码
public partial class GuageRichTextBox : UserControl
{
public GuageRichTextBox()
{
InitializeComponent();
richTextBox.WordWrap = false;
richTextBox.Top = Distance_X;
richTextBox.Left = Distance_Y;
richTextBox.Width = this.Width - Distance_X - 2;
richTextBox.Height = this.Height - Distance_Y - 2;
}
#region 变量及常量
const int Distance_X =;
const int Distance_Y =;
const int SpaceBetween =;
public static float thisleft =;
public static float StartBitH =;
public static float StartBitV =;
const int Scale = 3;
const int Scale = 6;
const int Scale = 9;
public static float Degree =;
public static float CodeSize =;
#endregion
#region 属性
[Browsable(true), Category("设置标尺控件"), Description("设置RichTextBox控件的相关属性")]
public RichTextBox NRichTextBox
{
get { return richTextBox; }
}
public enum Ruler
{
Graduation =,
Rule =,
}
private bool TCodeShow = false;
[Browsable(true), Category("设置标尺控件"), Description("是否在RichTextBox控件的前面添加代码的行号")]
public bool CodeShow
{
get { return TCodeShow; }
set
{
TCodeShow = value;
this.Invalidate();
}
}
private Ruler TRulerStyle = Ruler.Graduation;
[Browsable(true), Category("设置标尺控件"), Description("设置标尺样式:\nGraduation为刻度\nRule为尺子")]
public Ruler RulerStyle
{
get { return TRulerStyle; }
set
{
TRulerStyle = value;
this.Invalidate();
}
}
public enum Unit
{
Cm =,
Pels =,
}
private Unit TUnitStyle = Unit.Cm;
[Browsable(true), Category("设置标尺控件"), Description("设置标尺的单位:\nCm为厘米\nPels为像素")]
public Unit UnitStyle
{
get { return TUnitStyle; }
set
{
TUnitStyle = value;
this.Invalidate();
}
}
#endregion
#region 事件
private void GuageRichTextBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(new Pen(Color.DarkGray),, 0, this.Width - 1, this.Height - 1);
if (CodeShow)
{
float tem_code =
(float) StringSize(
(Convert.ToInt(CodeSize + (float) (richTextBox1.Height /
(StringSize(CodeSize.ToString(), richTextBox.Font,
false))))).ToString(), this.Font, true);
richTextBox.Top = Distance_X;
richTextBox.Left = Distance_Y + (int) Math.Ceiling(tem_code);
richTextBox.Width = this.Width - Distance_X - 2 - (int) Math.Ceiling(tem_code);
richTextBox.Height = this.Height - Distance_Y - 2;
thisleft = Distance_Y + tem_code;
}
else
{
richTextBox.Top = Distance_X;
richTextBox.Left = Distance_Y;
richTextBox.Width = this.Width - Distance_X - 2;
richTextBox.Height = this.Height - Distance_Y - 2;
thisleft = Distance_Y;
}
e.Graphics.DrawRectangle(new Pen(Color.LightSteelBlue), richTextBox.Location.X - 1, thisleft - 1,
richTextBox.Width + 1, richTextBox1.Height + 1);
e.Graphics.FillRectangle(new SolidBrush(Color.Silver),, 1, this.Width - 2, Distance_Y - 2);
e.Graphics.FillRectangle(new SolidBrush(Color.Silver),, 1, Distance_X - 2, this.Height - 2);
e.Graphics.FillRectangle(new SolidBrush(Color.Gray),, 3, Distance_X - 7, Distance_Y - 8);
e.Graphics.DrawRectangle(new Pen(SystemColors.Control),, 3, Distance_X - 8, Distance_Y - 8);
if (RulerStyle == Ruler.Rule)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Gray), thisleft -, 3, this.Width - (thisleft - 2),
Distance_Y -);
e.Graphics.DrawLine(new Pen(SystemColors.Control), thisleft -, 3, this.Width - 2, 3);
e.Graphics.DrawLine(new Pen(SystemColors.Control), thisleft -, Distance_Y - 5, this.Width - 2,
Distance_Y -);
e.Graphics.FillRectangle(new SolidBrush(Color.WhiteSmoke), thisleft -, 9,
this.Width - (thisleft -) - 1, Distance_Y - 19);
e.Graphics.FillRectangle(new SolidBrush(Color.Gray),, Distance_Y - 3, Distance_X - 7,
this.Height - (Distance_Y -) - 2);
e.Graphics.DrawLine(new Pen(SystemColors.Control),, Distance_Y - 3, 3, this.Height - 2);
e.Graphics.DrawLine(new Pen(SystemColors.Control), Distance_X -, Distance_Y - 3, Distance_X - 5,
this.Height -);
e.Graphics.FillRectangle(new SolidBrush(Color.WhiteSmoke),, Distance_Y - 3, Distance_X - 19,
this.Height - (Distance_Y -) - 2);
}
int tem_temHeight =;
string tem_value = "";
int tem_n =;
int divide =;
Pen tem_p = new Pen(new SolidBrush(Color.Black));
if (UnitStyle == Unit.Cm)
Degree = e.Graphics.DpiX /.4F;
if (UnitStyle == Unit.Pels)
Degree =;
int tem_width = this.Width -;
tem_n = (int) StartBitH;
if (tem_n != StartBitH)
StartBitH = (int) StartBitH;
for (float i =; i < tem_width;)
{
tem_temHeight = Scale;
float j = (i + (int) StartBitH) / Degree;
tem_value = "";
j = (int) j;
if (j % (divide *) == 0)
{
tem_temHeight = Scale;
if (UnitStyle == Unit.Cm)
tem_value = Convert.ToString(j /);
if (UnitStyle == Unit.Pels)
tem_value = Convert.ToString((int) j *);
}
else if (j % divide ==)
{
tem_temHeight = Scale;
}
tem_p.Width =;
if (RulerStyle == Ruler.Graduation)
{
e.Graphics.DrawLine(tem_p, i + + thisleft, SpaceBetween, i + 1 + thisleft,
SpaceBetween + tem_temHeight);
if (tem_value.Length >)
ProtractString(e.Graphics, tem_value.Trim(), i + + thisleft, SpaceBetween, i + 1 + thisleft,
SpaceBetween + tem_temHeight,);
}
if (RulerStyle == Ruler.Rule)
{
if (tem_value.Length >)
{
e.Graphics.DrawLine(tem_p, i + + thisleft, 4, i + 1 + thisleft, 7);
e.Graphics.DrawLine(tem_p, i + + thisleft, Distance_Y - 9, i + 1 + thisleft,
Distance_Y -);
float tem_space = + (Distance_X - 19F - 9F - StringSize(tem_value.Trim(), this.Font, false)) /
F;
ProtractString(e.Graphics, tem_value.Trim(), i + + thisleft, (float) Math.Ceiling(tem_space),
i + + thisleft, (float) Math.Ceiling(tem_space) + tem_temHeight, 0);
}
}
i += Degree;
}
if (UnitStyle == Unit.Cm)
Degree = e.Graphics.DpiX /.4F;
if (UnitStyle == Unit.Pels)
Degree =;
int tem_height = this.Height -;
tem_n = (int) StartBitV;
if (tem_n != StartBitV)
StartBitV = (int) StartBitV;
for (float i =; i < tem_height;)
{
tem_temHeight = Scale;
float j = (i + (int) StartBitV) / Degree;
tem_value = "";
j = (int) j;
if (j % == 0)
{
tem_temHeight = Scale;
if (UnitStyle == Unit.Cm)
tem_value = Convert.ToString(j /);
if (UnitStyle == Unit.Pels)
tem_value = Convert.ToString((int) j *);
}
else if (j % == 0)
{
tem_temHeight = Scale;
}
tem_p.Width =;
if (RulerStyle == Ruler.Graduation)
{
e.Graphics.DrawLine(tem_p, SpaceBetween, i + + Distance_Y, SpaceBetween + tem_temHeight,
i + + Distance_Y);
if (tem_value.Length >)
ProtractString(e.Graphics, tem_value.Trim(), SpaceBetween, i + + Distance_Y,
SpaceBetween + tem_temHeight, i + + Distance_Y, 1);
}
if (RulerStyle == Ruler.Rule)
{
if (tem_value.Length >)
{
e.Graphics.DrawLine(tem_p,, i + 1 + Distance_Y, 7, i + 1 + Distance_Y);
e.Graphics.DrawLine(tem_p, Distance_Y -, i + 1 + Distance_Y, Distance_Y - 7,
i + + Distance_Y);
float tem_space = + (Distance_X - 19F - 9F - StringSize(tem_value.Trim(), this.Font, false)) /
F;
ProtractString(e.Graphics, tem_value.Trim(), (float) Math.Floor(tem_space), i + + Distance_Y,
(float) Math.Floor(tem_space) + tem_temHeight, i + + Distance_Y, 1);
}
}
i += Degree;
}
if (CodeShow)
{
float tem_FontHeight =
(float) (richTextBox.Height / (StringSize(CodeSize.ToString(), richTextBox1.Font, false)));
float tem_tep = richTextBox.Top;
int tem_mark =;
for (int i =; i < (int) tem_FontHeight; i++)
{
tem_mark = i + (int) CodeSize;
e.Graphics.DrawString(tem_mark.ToString(), this.Font, new SolidBrush(Color.Red),
new PointF(richTextBox.Left - StringSize(tem_mark.ToString(), this.Font, true) - 2, tem_tep));
tem_tep = tem_tep + StringSize("懂", richTextBox.Font, false);
}
}
}
private void GuageRichTextBox_Resize(object sender, EventArgs e)
{
richTextBox.Top = Distance_X;
richTextBox.Left = Distance_Y;
richTextBox.Width = this.Width - Distance_X - 2;
richTextBox.Height = this.Height - Distance_Y - 2;
this.Invalidate();
}
private void richTextBox_HScroll(object sender, EventArgs e)
{
StartBitH = (int) (Math.Abs((float) richTextBox.GetPositionFromCharIndex(0).X - 1));
this.Invalidate();
}
private void richTextBox_VScroll(object sender, EventArgs e)
{
StartBitV = (int) (Math.Abs((float) richTextBox.GetPositionFromCharIndex(0).Y - 1));
if (CodeShow)
CodeSize = (int) Math.Abs((richTextBox.GetPositionFromCharIndex(0).Y /
StringSize("懂", richTextBox.Font, false)));
this.Invalidate();
}
#endregion
#region 方法
public void ProtractString(Graphics e, string str, float x, float y1, float x2, float y2, float n)
{
float TitWidth = StringSize(str, this.Font, true);
if (n ==)
e.DrawString(str, this.Font, new SolidBrush(Color.Black), new PointF(x - TitWidth / 2, y2 + 1));
else
{
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
e.DrawString(str, this.Font, new SolidBrush(Color.Black), new PointF(x + 1, y2 - TitWidth / 2),
drawFormat);
}
}
public float StringSize(string str, Font font, bool n)
{
Graphics TitG = this.CreateGraphics();
SizeF TitSize = TitG.MeasureString(str, font);
float TitWidth = TitSize.Width;
float TitHeight = TitSize.Height;
if (n)
return TitWidth;
else
return TitHeight;
}
#endregion
}