程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#寫帶行號的richTextBox

c#寫帶行號的richTextBox

編輯:C#入門知識

加兩個控件:Panel/RichTextBox

 

view sourceprint?01 //方法 

02   

03 private void showLineNo() 

04   

05 { 

06   

07 //獲得當前坐標信息 

08   

09 Point p = this.richTextBox1.Location; 

10 int crntFirstIndex = this.richTextBox1.GetCharIndexFromPosition(p); 

11   

12 int crntFirstLine = this.richTextBox1.GetLineFromCharIndex(crntFirstIndex); 

13   

14 Point crntFirstPos = this.richTextBox1.GetPositionFromCharIndex(crntFirstIndex); 

15   

16 // 

17 p.Y += this.richTextBox1.Height; 

18   

19 // 

20   

21      int crntLastIndex = this.richTextBox1.GetCharIndexFromPosition(p); 

22   

23      int crntLastLine = this.richTextBox1.GetLineFromCharIndex(crntLastIndex); 

24      Point crntLastPos = this.richTextBox1.GetPositionFromCharIndex(crntLastIndex); 

25   

26      // 

27   

28      // 

29   

30      //准備畫圖 

31      Graphics g = this.panel1.CreateGraphics(); 

32   

33      Font font = new Font(this.richTextBox1.Font,this.richTextBox1.Font.Style); 

34   

35      SolidBrush brush = new SolidBrush(Color.Green); 

36   

37      // 

38      // 

39   

40      //畫圖開始 

41   

42      //刷新畫布 

43   

44      Rectangle rect = this.panel1.ClientRectangle; 

45      brush.Color = this.panel1.BackColor; 

46   

47 g.FillRectangle(brush, 0, 0, this.panel1.ClientRectangle.Width,this.panel1.ClientRectangle.Height); 

48   

49      brush.Color = Color.Green;//重置畫筆顏色 

50   

51      // 

52      //繪制行號 

53   

54      int lineSpace = 0; 

55   

56      if (crntFirstLine != crntLastLine) 

57   

58      { 

59           lineSpace = (crntLastPos.Y - crntFirstPos.Y) / (crntLastLine - crntFirstLine); 

60   

61      } 

62   

63      else

64   

65      { 

66           lineSpace = Convert.ToInt32(this.richTextBox1.Font.Size); 

67   

68      } 

69   

70      int brushX = this.panel1.ClientRectangle.Width - Convert.ToInt32(font.Size * 3); 

71   

72      int brushY = crntLastPos.Y+ Convert.ToInt32(font.Size*0.21f);//驚人的算法啊!! 

73      for (int i = crntLastLine; i >= crntFirstLine;i-- ) 

74   

75      { 

76   

77            g.DrawString((i + 1).ToString(), font, brush, brushX, brushY); 

78   

79            brushY -= lineSpace; 

80      } 

81   

82      g.Dispose(); 

83   

84      font.Dispose(); 

85   

86      brush.Dispose(); 

87 }


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved