检索RichTextBox中所选文本的字体名称

问题描述 投票:0回答:2

我正在尝试检索 RichTextBox 中每行文本的字体名称(每行都有不同的字体)。下面是我用来获取 RTB 中第二行的字体名称的代码。

RichTextBox2.Select(RichTextBox2.Lines(0).Length + 1, 
                    RichTextBox2.Lines(1).Length)
font = RichTextBox2.SelectionFont.Name

但是我得到了文本框第一行的字体名称。如有任何帮助,我们将不胜感激。

vb.net fonts richtextbox selectedtext
2个回答
0
投票

尝试使用 GetFirstCharIndexFromLine 函数获取每行的起点:

For i As Integer = 0 To RichTextBox2.Lines.Count - 1
  RichTextBox2.Select(RichTextBox2.GetFirstCharIndexFromLine(i),
                      RichTextBox2.Lines(i).Length)
  MessageBox.Show(RichTextBox2.SelectionFont.Name)
Next

这是我用来设置 RichTextBox 控件的:

RichTextBox2.Clear()
RichTextBox2.SelectionFont = New Font("Segoe UI", 16)
RichTextBox2.AppendText("This is the First Line" & Environment.NewLine)
RichTextBox2.SelectionFont = New Font("Calibri", 12)
RichTextBox2.AppendText("This is the Second Line" & Environment.NewLine)
RichTextBox2.SelectionFont = New Font("Arial", 16)
RichTextBox2.AppendText("This is the Third Line" & Environment.NewLine)

0
投票

试试这个功能:

'Chiamare la funzione ShowInfo nel controllo su KeyUp e MouseMove
Private Const EM_GETLINE = &HC4
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Const EM_REPLACESEL = &HC2
' info testuale
Dim lCurPos As Long = 0
Dim lCurLineNum As Long = 0
dim lTotLines As Long = 0
Dim lLineLength As Long = 0
Dim lNumBChar As Long = 0
Dim sCurLine As String = ""

<DllImport("user32.dll")>
Private Shared Function SendMessage(ByVal hWnd As IntPtr,
                            ByVal msg As Int32,
                            ByVal wParam As Int32,
                            ByVal lParam As IntPtr) As Int32
End Function

Private Function ShowInfo() As String
    '********************************************************************
    ' Purpose:  Runs all the functions to get the necessary information
    '           in order to create the information text string. The text
    '           string is then displayed in the label control.
    '********************************************************************

    'Determine Cursor Position
    If RichTextBox1.SelectionLength = 0 Then
        lCurPos = RichTextBox1.SelectionStart
    Else
        lCurPos = RichTextBox1.SelectionStart + RichTextBox1.SelectionLength
    End If

    'Determine Line Number
    lCurLineNum = SendMessage(RichTextBox1.Handle, EM_LINEFROMCHAR, lCurPos, 0)

    'Determine the Line Length
    lLineLength = SendMessage(RichTextBox1.Handle, EM_LINELENGTH, lCurPos, 0)

    'Determine the number of characters in lines before current
    'cursor position. Note that the number of characters includes a
    'carriage return and line feed characters at the end of each
    'line.
    lNumBChar = SendMessage(RichTextBox1.Handle, EM_LINEINDEX, lCurLineNum, 0)

    'Determine Total number of lines
    lTotLines = SendMessage(RichTextBox1.Handle, EM_GETLINECOUNT, 0, 0)

    'Determine Current Line as string
    sCurLine = SendMessage(RichTextBox1.Handle, EM_GETLINE, lCurLineNum, 0)

    'Display the informations
    Dim return_string As String = ""
    return_string = sCurLine + ";" + CStr(lLineLength) + ";" + CStr(lCurLineNum + 1) + ";" + CStr(lTotLines) + ";" + CStr(lCurPos) + ";" + CStr(lNumBChar)
    'CurrentLine ; LineLength ; LineNumber ; TotalLines ; CursorPos ; TotalCharsInPreviousLine
    Return return_string
End Function

Private Sub RichTextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyUp
    'Capture info text
    ShowInfo()
End Sub

Private Sub RichTextBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseMove
    'Capture info text
    ShowInfo()
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.