C#實(shí)現(xiàn)帶行數(shù)和標(biāo)尺的RichTextBox
更新時(shí)間:2022年12月16日 15:39:47 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)帶行數(shù)和標(biāo)尺的RichTextBox,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過(guò)程
效果
代碼
public partial class GuageRichTextBox : UserControl { public GuageRichTextBox() { InitializeComponent(); richTextBox1.WordWrap = false; richTextBox1.Top = Distance_X; richTextBox1.Left = Distance_Y; richTextBox1.Width = this.Width - Distance_X - 2; richTextBox1.Height = this.Height - Distance_Y - 2; } #region 變量及常量 const int Distance_X = 30; //設(shè)置RichTextBox控件的X位置 const int Distance_Y = 30; //設(shè)置RichTextBox控件的Y位置 const int SpaceBetween = 3; //設(shè)置標(biāo)尺的間距 public static float thisleft = 0; //設(shè)置控件的左邊距 public static float StartBitH = 0; //記錄橫向滾動(dòng)條的位置 public static float StartBitV = 0; //記錄縱向滾動(dòng)條的位置 const int Scale1 = 3; //設(shè)置刻度最短的線長(zhǎng) const int Scale5 = 6; //設(shè)置刻度為5時(shí)的線長(zhǎng) const int Scale10 = 9; //設(shè)置刻度為10是垢線長(zhǎng) public static float Degree = 0; //度數(shù) public static float CodeSize = 1; //代碼編號(hào)的寬度 #endregion #region 屬性 [Browsable(true), Category("設(shè)置標(biāo)尺控件"), Description("設(shè)置RichTextBox控件的相關(guān)屬性")] //在“屬性”窗口中顯示DataStyle屬性 public RichTextBox NRichTextBox { get { return richTextBox1; } } public enum Ruler { Graduation = 0, //刻度 Rule = 1, //尺子 } private bool TCodeShow = false; [Browsable(true), Category("設(shè)置標(biāo)尺控件"), Description("是否在RichTextBox控件的前面添加代碼的行號(hào)")] //在“屬性”窗口中顯示DataStyle屬性 public bool CodeShow { get { return TCodeShow; } set { TCodeShow = value; this.Invalidate(); } } private Ruler TRulerStyle = Ruler.Graduation; [Browsable(true), Category("設(shè)置標(biāo)尺控件"), Description("設(shè)置標(biāo)尺樣式:\nGraduation為刻度\nRule為尺子")] //在“屬性”窗口中顯示DataStyle屬性 public Ruler RulerStyle { get { return TRulerStyle; } set { TRulerStyle = value; this.Invalidate(); } } public enum Unit { Cm = 0, //厘米 Pels = 1, //像素 } private Unit TUnitStyle = Unit.Cm; [Browsable(true), Category("設(shè)置標(biāo)尺控件"), Description("設(shè)置標(biāo)尺的單位:\nCm為厘米\nPels為像素")] //在“屬性”窗口中顯示DataStyle屬性 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, 0, this.Width - 1, this.Height - 1); //繪制外邊框 if (CodeShow) //如查在文本框左邊添加行號(hào) { //獲取行號(hào)的寬度 float tem_code = (float) StringSize( (Convert.ToInt32(CodeSize + (float) (richTextBox1.Height / (StringSize(CodeSize.ToString(), richTextBox1.Font, false))))).ToString(), this.Font, true); richTextBox1.Top = Distance_X; //設(shè)置控件的頂端距離 richTextBox1.Left = Distance_Y + (int) Math.Ceiling(tem_code); //設(shè)置控件的左端距離 richTextBox1.Width = this.Width - Distance_X - 2 - (int) Math.Ceiling(tem_code); //設(shè)置控件的寬度 richTextBox1.Height = this.Height - Distance_Y - 2; //設(shè)置控件高度 thisleft = Distance_Y + tem_code; //設(shè)置標(biāo)尺的左端位置 } else { richTextBox1.Top = Distance_X; //設(shè)置控件的頂端距離 richTextBox1.Left = Distance_Y; //設(shè)置控件的左端距離 richTextBox1.Width = this.Width - Distance_X - 2; //設(shè)置控件的寬度 richTextBox1.Height = this.Height - Distance_Y - 2; //設(shè)置控件高度 thisleft = Distance_Y; //設(shè)置標(biāo)尺的左端位置 } //繪制文本框的邊框 e.Graphics.DrawRectangle(new Pen(Color.LightSteelBlue), richTextBox1.Location.X - 1, thisleft - 1, richTextBox1.Width + 1, richTextBox1.Height + 1); e.Graphics.FillRectangle(new SolidBrush(Color.Silver), 1, 1, this.Width - 2, Distance_Y - 2); //文本框的上邊框 e.Graphics.FillRectangle(new SolidBrush(Color.Silver), 1, 1, Distance_X - 2, this.Height - 2); //文本框的左邊框 e.Graphics.FillRectangle(new SolidBrush(Color.Gray), 3, 3, Distance_X - 7, Distance_Y - 8); //繪制左上角的方塊邊框 e.Graphics.DrawRectangle(new Pen(SystemColors.Control), 3, 3, Distance_X - 8, Distance_Y - 8); //繪制左上角的方塊 if (RulerStyle == Ruler.Rule) //標(biāo)尺 { //繪制上邊的標(biāo)尺背景 e.Graphics.FillRectangle(new SolidBrush(Color.Gray), thisleft - 3, 3, this.Width - (thisleft - 2), Distance_Y - 9); //繪制左上角的方塊邊框 e.Graphics.DrawLine(new Pen(SystemColors.Control), thisleft - 3, 3, this.Width - 2, 3); //繪制方塊的上邊線 e.Graphics.DrawLine(new Pen(SystemColors.Control), thisleft - 3, Distance_Y - 5, this.Width - 2, Distance_Y - 5); //繪制方塊的下邊線 e.Graphics.FillRectangle(new SolidBrush(Color.WhiteSmoke), thisleft - 2, 9, this.Width - (thisleft - 2) - 1, Distance_Y - 19); //繪制方塊的中間塊 //繪制左邊的標(biāo)尺背景 e.Graphics.FillRectangle(new SolidBrush(Color.Gray), 3, Distance_Y - 3, Distance_X - 7, this.Height - (Distance_Y - 3) - 2); //繪制左邊的方塊 e.Graphics.DrawLine(new Pen(SystemColors.Control), 3, Distance_Y - 3, 3, this.Height - 2); //繪制方塊的左邊線 e.Graphics.DrawLine(new Pen(SystemColors.Control), Distance_X - 5, Distance_Y - 3, Distance_X - 5, this.Height - 2); //繪制方塊的右邊線 e.Graphics.FillRectangle(new SolidBrush(Color.WhiteSmoke), 9, Distance_Y - 3, Distance_X - 19, this.Height - (Distance_Y - 3) - 2); //繪制方塊的中間塊 } int tem_temHeight = 0; string tem_value = ""; int tem_n = 0; int divide = 5; Pen tem_p = new Pen(new SolidBrush(Color.Black)); //橫向刻度的設(shè)置 if (UnitStyle == Unit.Cm) //如果刻度的單位是厘米 Degree = e.Graphics.DpiX / 25.4F; //將像素轉(zhuǎn)換成毫米 if (UnitStyle == Unit.Pels) //如果刻度的單位是像素 Degree = 10; //設(shè)置10像素為一個(gè)刻度 int tem_width = this.Width - 3; tem_n = (int) StartBitH; //記錄橫向滾動(dòng)條的位置 if (tem_n != StartBitH) //如果橫向滾動(dòng)條的位置值為小數(shù) StartBitH = (int) StartBitH; //對(duì)橫向滾動(dòng)條的位置進(jìn)行取整 for (float i = 0; i < tem_width;) //在文本框的項(xiàng)端繪制標(biāo)尺 { tem_temHeight = Scale1; //設(shè)置刻度線的最小長(zhǎng)度 float j = (i + (int) StartBitH) / Degree; //獲取刻度值 tem_value = ""; j = (int) j; //對(duì)刻度值進(jìn)行取整 if (j % (divide * 2) == 0) //如果刻度值是10進(jìn)位 { tem_temHeight = Scale10; //設(shè)置最長(zhǎng)的刻度線 if (UnitStyle == Unit.Cm) //如果刻度的單位為厘米 tem_value = Convert.ToString(j / 10); //記錄刻度值 if (UnitStyle == Unit.Pels) //如果刻度的單位為像素 tem_value = Convert.ToString((int) j * 10); //記錄刻度值 } else if (j % divide == 0) //如果刻度值的進(jìn)位為5 { tem_temHeight = Scale5; //設(shè)置刻度線為中等 } tem_p.Width = 1; if (RulerStyle == Ruler.Graduation) //如果是以刻度值進(jìn)行測(cè)量 { //繪制刻度線 e.Graphics.DrawLine(tem_p, i + 1 + thisleft, SpaceBetween, i + 1 + thisleft, SpaceBetween + tem_temHeight); if (tem_value.Length > 0) //如果有刻度值 //繪制刻度值 ProtractString(e.Graphics, tem_value.Trim(), i + 1 + thisleft, SpaceBetween, i + 1 + thisleft, SpaceBetween + tem_temHeight, 0); } if (RulerStyle == Ruler.Rule) //如果是以標(biāo)尺進(jìn)行測(cè)量 { if (tem_value.Length > 0) //如果有刻度值 { e.Graphics.DrawLine(tem_p, i + 1 + thisleft, 4, i + 1 + thisleft, 7); //繪制頂端的刻度線 e.Graphics.DrawLine(tem_p, i + 1 + thisleft, Distance_Y - 9, i + 1 + thisleft, Distance_Y - 7); //繪制底端的刻度線 float tem_space = 3 + (Distance_X - 19F - 9F - StringSize(tem_value.Trim(), this.Font, false)) / 2F; //設(shè)置文本的橫向位置 //繪制文本 ProtractString(e.Graphics, tem_value.Trim(), i + 1 + thisleft, (float) Math.Ceiling(tem_space), i + 1 + thisleft, (float) Math.Ceiling(tem_space) + tem_temHeight, 0); } } i += Degree; //累加刻度的寬度 } //縱向刻度的設(shè)置 if (UnitStyle == Unit.Cm) //如果刻度的單位是厘米 Degree = e.Graphics.DpiX / 25.4F; //將像素轉(zhuǎn)換成毫米 if (UnitStyle == Unit.Pels) //如果刻度的單位是像素 Degree = 10; //刻度值設(shè)為10像素 int tem_height = this.Height - 3; tem_n = (int) StartBitV; //記錄縱向滾動(dòng)條的位置 if (tem_n != StartBitV) //如果縱向滾動(dòng)條的位置為小數(shù) StartBitV = (int) StartBitV; //對(duì)其進(jìn)行取整 for (float i = 0; i < tem_height;) //在文本框的左端繪制標(biāo)尺 { tem_temHeight = Scale1; //設(shè)置刻度線的最小值 float j = (i + (int) StartBitV) / Degree; //獲取當(dāng)前的刻度值 tem_value = ""; j = (int) j; //對(duì)刻度值進(jìn)行取整 if (j % 10 == 0) //如果刻度值是10進(jìn)位 { tem_temHeight = Scale10; //設(shè)置刻度線的長(zhǎng)度為最長(zhǎng) if (UnitStyle == Unit.Cm) //如果刻度的單位是厘米 tem_value = Convert.ToString(j / 10); //獲取厘米的刻度值 if (UnitStyle == Unit.Pels) //如果刻度的單位是像素 tem_value = Convert.ToString((int) j * 10); //獲取像素的刻度值 } else if (j % 5 == 0) //如果刻度值是5進(jìn)位 { tem_temHeight = Scale5; //設(shè)置刻度線的長(zhǎng)度為中等 } tem_p.Width = 1; if (RulerStyle == Ruler.Graduation) //如果是以刻度值進(jìn)行測(cè)量 { //繪制刻度線 e.Graphics.DrawLine(tem_p, SpaceBetween, i + 1 + Distance_Y, SpaceBetween + tem_temHeight, i + 1 + Distance_Y); if (tem_value.Length > 0) //如果有刻度值 //繪制刻度值 ProtractString(e.Graphics, tem_value.Trim(), SpaceBetween, i + 1 + Distance_Y, SpaceBetween + tem_temHeight, i + 1 + Distance_Y, 1); } if (RulerStyle == Ruler.Rule) //如果是以標(biāo)尺進(jìn)行測(cè)量 { if (tem_value.Length > 0) //如果有刻度值 { e.Graphics.DrawLine(tem_p, 4, i + 1 + Distance_Y, 7, i + 1 + Distance_Y); //繪制左端刻度線 e.Graphics.DrawLine(tem_p, Distance_Y - 9, i + 1 + Distance_Y, Distance_Y - 7, i + 1 + Distance_Y); //繪制右端刻度線 float tem_space = 3 + (Distance_X - 19F - 9F - StringSize(tem_value.Trim(), this.Font, false)) / 2F; //設(shè)置文本的縱向位置 //繪制文本 ProtractString(e.Graphics, tem_value.Trim(), (float) Math.Floor(tem_space), i + 1 + Distance_Y, (float) Math.Floor(tem_space) + tem_temHeight, i + 1 + Distance_Y, 1); } } i += Degree; //累加刻度值 } if (CodeShow) //如果顯示行號(hào) { //設(shè)置文本的高度 float tem_FontHeight = (float) (richTextBox1.Height / (StringSize(CodeSize.ToString(), richTextBox1.Font, false))); float tem_tep = richTextBox1.Top; //獲取文本框的頂端位置 int tem_mark = 0; for (int i = 0; i < (int) tem_FontHeight; i++) //繪制行號(hào) { tem_mark = i + (int) CodeSize; //設(shè)置代碼編號(hào)的寬度 //繪制行號(hào) e.Graphics.DrawString(tem_mark.ToString(), this.Font, new SolidBrush(Color.Red), new PointF(richTextBox1.Left - StringSize(tem_mark.ToString(), this.Font, true) - 2, tem_tep)); tem_tep = tem_tep + StringSize("懂", richTextBox1.Font, false); //設(shè)置下一個(gè)行號(hào)的X坐標(biāo)值 } } } private void GuageRichTextBox_Resize(object sender, EventArgs e) { richTextBox1.Top = Distance_X; //設(shè)置控件的頂端位置 richTextBox1.Left = Distance_Y; //設(shè)置控件的左端位置 richTextBox1.Width = this.Width - Distance_X - 2; //設(shè)置控件的寬度 richTextBox1.Height = this.Height - Distance_Y - 2; //設(shè)置控件的高度 this.Invalidate(); } private void richTextBox1_HScroll(object sender, EventArgs e) { StartBitH = (int) (Math.Abs((float) richTextBox1.GetPositionFromCharIndex(0).X - 1)); //檢索控件橫向內(nèi)指定字符索引處的位置 this.Invalidate(); } private void richTextBox1_VScroll(object sender, EventArgs e) { StartBitV = (int) (Math.Abs((float) richTextBox1.GetPositionFromCharIndex(0).Y - 1)); //檢索控件縱向內(nèi)指定字符索引處的位置 if (CodeShow) //如果顯示行號(hào) CodeSize = (int) Math.Abs((richTextBox1.GetPositionFromCharIndex(0).Y / StringSize("懂", richTextBox1.Font, false))); //設(shè)置行號(hào)的高度 this.Invalidate(); } #endregion #region 方法 /// <summary> /// 在指定的位置繪制文本信息 /// </summary> /// <param e="Graphics">封裝一個(gè)繪圖的類對(duì)象</param> /// <param str="string">文本信息</param> /// <param x1="float">左上角x坐標(biāo)</param> /// <param y1="float">左上角y坐標(biāo)</param> /// <param x2="float">右下角x坐標(biāo)</param> /// <param y2="float">右下角y坐標(biāo)</param> /// <param n="float">標(biāo)識(shí),判斷是在橫向標(biāo)尺上繪制文字還是在縱向標(biāo)尺上繪制文字</param> public void ProtractString(Graphics e, string str, float x1, float y1, float x2, float y2, float n) { float TitWidth = StringSize(str, this.Font, true); //獲取字符串的寬度 if (n == 0) //在橫向標(biāo)尺上繪制文字 e.DrawString(str, this.Font, new SolidBrush(Color.Black), new PointF(x2 - TitWidth / 2, y2 + 1)); else //在縱向標(biāo)尺上繪制文字 { StringFormat drawFormat = new StringFormat(); //實(shí)例化StringFormat類 drawFormat.FormatFlags = StringFormatFlags.DirectionVertical; //設(shè)置文本為垂直對(duì)齊 //繪制指定的文本 e.DrawString(str, this.Font, new SolidBrush(Color.Black), new PointF(x2 + 1, y2 - TitWidth / 2), drawFormat); } } /// <summary> /// 獲取文本的高度或?qū)挾? /// </summary> /// <param str="string">文本信息</param> /// <param font="Font">字體樣式</param> /// <param n="bool">標(biāo)識(shí),判斷返回的是高度還是寬度</param> public float StringSize(string str, Font font, bool n) //n==true為width { Graphics TitG = this.CreateGraphics(); //創(chuàng)建Graphics類對(duì)象 SizeF TitSize = TitG.MeasureString(str, font); //將繪制的字符串進(jìn)行格式化 float TitWidth = TitSize.Width; //獲取字符串的寬度 float TitHeight = TitSize.Height; //獲取字符串的高度 if (n) return TitWidth; //返回文本信息的寬度 else return TitHeight; //返回文本信息的高度 } #endregion }
以上就是C#實(shí)現(xiàn)帶行數(shù)和標(biāo)尺的RichTextBox的詳細(xì)內(nèi)容,更多關(guān)于C# RichTextBox的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Unity?AssetPostprocessor模型函數(shù)Model實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity?AssetPostprocessor模型Model函數(shù)實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05基于WPF簡(jiǎn)單實(shí)現(xiàn)Meesage消息提醒
這篇文章主要介紹了如何利用WPF簡(jiǎn)單實(shí)現(xiàn)Meesage消息提醒,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2023-07-07C# 中const,readonly,static的使用小結(jié)
這篇文章主要介紹了C# 中使用const,readonly,static的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01C#中Byte[]和String之間轉(zhuǎn)換的方法
很多朋友不清楚如何在Byte[]和String之間進(jìn)行轉(zhuǎn)換?下面小編給大家?guī)?lái)了byte與string轉(zhuǎn)換的方法,感興趣的朋友參考下吧2016-08-08關(guān)于C#調(diào)用C++dll傳指針釋放內(nèi)存問(wèn)題
這篇文章主要介紹了關(guān)于C#調(diào)用C++dll傳指針釋放內(nèi)存問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12C#關(guān)于Task.Yeild()函數(shù)的討論
這篇文章主要介紹了C#中關(guān)于Task.Yeild()函數(shù)的相關(guān)資料,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07