vb.net - RichTextBox 行号字体大小调整 - VB.NET
问题描述
我正在构建一个文本编辑器程序,在主 RichTextBox 的左侧,是一个由 PictureBox 组成的行编号方案,一个例程使用 Graphics 方法绘制数字。当您向下滚动 RichTextBox 时,行号会同样调整。
我在网上找到了一些代码并进行了一些调整,但我正在努力解决其中一个问题。
我有一个放大/缩小功能,因此用户可以调整 RichTextBox 中文本的大小,这是通过在 RichTextBox 的 .ZoomFactor 属性中添加/减去 0.5 来完成的。这部分工作完美,是一个伟大的、简单的解决方案。然而; 如果我调整 RichTextBox 的缩放,文本现在比行号大,所以它们不会对齐。我的想法是增加行号方案的字体大小并进行小幅调整,直到它们彼此完美对齐。
例子:
这是正常大小的(数字是彩色的,因为我已经告诉程序故意这样做)
这是当我调整 RichTextBox 缩放因子而不调整行号的字体大小时发生的情况
绘制行号的代码是:
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
我将对这部分代码进行哪些调整以增加大小?
Dim font_height As Single
font_height = .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(2)).Y _
- .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(1)).Y
If font_height = 0 Then Exit Sub
任何帮助是极大的赞赏 :)
解决方案
事实证明,绘图程序已经处理了这个。
我已经做到了,当用户在表单上移动鼠标、调整表单大小或调整缩放时,它会刷新。
我简单地编写了以下代码,立即解决了这个问题。
LineNumber.Invalidate()
推荐阅读
- delphi - 为什么在调整 Delphi FMX 应用程序大小时 FormFactor.Width 和 FormFactor.Height 不会改变?
- c - 在选择结构中使用 rand() 的正确方法
- javascript - getStaticProps 中的 Next.js firebase 导致开发模式出现问题
- reactjs - React 和 Vercel:错误:命令“npm run build”以 1 退出?
- python - 如何比较 JSON 响应中的每个值并根据 python 中的某些条件过滤它们?
- javascript - Javascript:如何为 JSON 数组创建可传输对象
- rust - 如何将终端存储在结构中的 tui-rs 中?
- java - java中的冒泡排序问题
- marklogic-10 - Marklogic 连接到任何应用程序
- closedxml - 使用 ClosedXML 在行为空时隐藏 DataColumn