1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | Public Function ConvertToHTML(ByVal Box As RichTextBox) As String ' Takes a RichTextBox control and returns a ' simple HTML-formatted version of its contents Dim strHTML As String Dim strColour As String Dim blnBold As Boolean Dim blnItalic As Boolean Dim strFont As String Dim shtSize As Short Dim lngOriginalStart As Long Dim lngOriginalLength As Long Dim intCount As Integer ' If nothing in the box, exit If Box.Text.Length = 0 Then Exit Function ' Store original selections, then select first character lngOriginalStart = 0 lngOriginalLength = Box.TextLength Box.Select(0, 1) ' Add HTML header strHTML = "<html>" ' Setup initial parameters strColour = Box.SelectionColor.ToKnownColor.ToString blnBold = Box.SelectionFont.Bold blnItalic = Box.SelectionFont.Italic strFont = Box.SelectionFont.FontFamily.Name shtSize = Box.SelectionFont.Size ' Include first 'style' parameters in the HTML strHTML += "<span style=""font-family: " & strFont & _ "; font-size: " & shtSize & "pt; color: " & strColour & """>" ' Include bold tag, if required If blnBold = True Then strHTML += "<b>" End If ' Include italic tag, if required If blnItalic = True Then strHTML += "<i>" End If ' Finally, add our first character strHTML += Box.Text.Substring(0, 1) ' Loop around all remaining characters For intCount = 2 To Box.Text.Length ' Select current character Box.Select(intCount - 1, 1) ' If this is a line break, add HTML tag If Box.Text.Substring(intCount - 1, 1) = Convert.ToChar(10) Then strHTML += "<br>" End If ' Check/implement any changes in style If Box.SelectionColor.ToKnownColor.ToString <> strColour _ Or Box.SelectionFont.FontFamily.Name <> strFont Or _ Box.SelectionFont.Size <> shtSize Then strHTML += "</span><span style=""font-family: " _ & Box.SelectionFont.FontFamily.Name & _ "; font-size: " & Box.SelectionFont.Size & _ "pt; color: " & _ Box.SelectionColor.ToKnownColor.ToString & """>" End If ' Check for bold changes If Box.SelectionFont.Bold <> blnBold Then If Box.SelectionFont.Bold = False Then strHTML += "</b>" Else strHTML += "<b>" End If End If ' Check for italic changes If Box.SelectionFont.Italic <> blnItalic Then If Box.SelectionFont.Italic = False Then strHTML += "</i>" Else strHTML += "<i>" End If End If ' Add the actual character strHTML += Mid(Box.Text, intCount, 1) ' Update variables with current style strColour = Box.SelectionColor.ToKnownColor.ToString blnBold = Box.SelectionFont.Bold blnItalic = Box.SelectionFont.Italic strFont = Box.SelectionFont.FontFamily.Name shtSize = Box.SelectionFont.Size Next ' Close off any open bold/italic tags If blnBold = True Then strHTML += "</b>" If blnItalic = True Then strHTML += "</i>" ' Terminate outstanding HTML tags strHTML += "</span></html>" ' Restore original RichTextBox selection Box.Select(lngOriginalStart, lngOriginalLength) ' Return HTML Return strHTML End Function |
2020年5月19日 星期二
Vb.net RichTextBox轉成html格式 [RichtextBox convert to HTML]
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言