简体   繁体   English

RichTextBox行号字体大小调整-VB.NET

[英]RichTextBox Line Numbering Font Size Adjustment - VB.NET

I'm building a text editor program, and on the left-hand side of the main RichTextBox, is a line numbering scheme that consists of a PictureBox, and a routine uses the Graphics method to draw numbers.我正在构建一个文本编辑器程序,在主 RichTextBox 的左侧,是一个由 PictureBox 组成的行编号方案,以及一个使用 Graphics 方法绘制数字的例程。 As you scroll down the RichTextBox, the line numbers equally adjust.当您向下滚动 RichTextBox 时,行号会同样调整。

I found some code online and made a few adjustments, but I'm struggling with one of them.我在网上找到了一些代码并进行了一些调整,但我正在努力解决其中一个问题。

I have a Zoom In/Out feature so the user can adjust the size of the text in the RichTextBox, this is done by adding/subtracting 0.5 to/from the.ZoomFactor property of the RichTextBox.我有一个放大/缩小功能,因此用户可以调整 RichTextBox 中文本的大小,这是通过在 RichTextBox 的.ZoomFactor 属性中添加/减去 0.5 来完成的。 That part works perfectly and is a great, simple solution.这部分工作完美,是一个伟大的、简单的解决方案。 However;然而; If I adjust the zoom on the RichTextBox, the text is now larger than the line numbers, so they don't line up.如果我在 RichTextBox 上调整缩放,文本现在大于行号,因此它们不会对齐。 My idea was to just increase the font size of the line numbering scheme and make small adjustments until they line up perfectly with each other.我的想法是增加行号方案的字体大小并进行小幅调整,直到它们彼此完美对齐。

Example:例子:

This is it at normal size (the numbers are coloured because I've told the program to do that on purpose)这是正常大小的(数字是彩色的,因为我已经告诉程序故意这样做)

在此处输入图像描述

This is what happens when I adjust the RichTextBox zoom factor, without adjusting font size of line number这是当我调整 RichTextBox 缩放因子而不调整行号的字体大小时发生的情况

在此处输入图像描述

The code for drawing the line numbers is:绘制行号的代码是:

Private Sub DrawRichTextBoxLineNumbers(ByRef g As Graphics)
    With TextEditBox
        Dim font_height As Single
        font_height = .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(2)).Y _
     - .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(1)).Y
        If font_height = 0 Then Exit Sub


    'Get the first line index and location
    Dim first_index As Integer
    Dim first_line As Integer
    Dim first_line_y As Integer
    first_index = .GetCharIndexFromPosition(New _
 Point(0, g.VisibleClipBounds.Y + font_height / 3))
    first_line = .GetLineFromCharIndex(first_index)
    first_line_y = .GetPositionFromCharIndex(first_index).Y

    'Print on the PictureBox the visible line numbers of the RichTextBox
    g.Clear(Control.DefaultBackColor)
    Dim i As Integer = first_line
    Dim y As Single
    Do While y < g.VisibleClipBounds.Y + g.VisibleClipBounds.Height
        y = first_line_y + 2 + font_height * (i - first_line - 1)
        g.DrawString((i).ToString, .Font, Brushes.Gray, LineNumber.Width _
  - g.MeasureString((i).ToString, .Font).Width, y)
        i += 1
    Loop
    'Debug.WriteLine("Finished: " & firstLine + 1 & " " & i - 1)
End With
End Sub

What adjustment would I make to this section of the code to increase the size?我将对这部分代码进行哪些调整以增加大小?

Dim font_height As Single
            font_height = .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(2)).Y _
         - .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(1)).Y
            If font_height = 0 Then Exit Sub

Any help is greatly appreciated:)任何帮助是极大的赞赏:)

Turns out, the drawing routine already handles this.事实证明,绘图程序已经处理了这个。

I've made it so that when the user moves the mouse on the form, resizes the form or adjusts the zoom, it refreshes.我已经做到了,当用户在表单上移动鼠标、调整表单大小或调整缩放时,它会刷新。

I simply wrote the following code which immediately solved the problem.我简单地编写了以下代码,立即解决了这个问题。

LineNumber.Invalidate()

Also this was a great Line Numbers for RichTextBox with many Features这也是 RichTextBox 的一个很好的行号,具有许多功能

You can download the Source Code at this Link: https://drive.google.com/file/d/1aMVts_pkok_56DRvCdTkYCGywzjsRqS6/view?usp=sharing您可以在此链接下载源代码: https://drive.google.com/file/d/1aMVts_pkok_56DRvCdTkYCGywzjsRqS6/view?usp=sharing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM