我经常看小说,所以自己写了个阅读器,里面涉及了一些知识,我放出来,一是记录方便我以后忘了可以查看,二是供需要的朋友参考。
如果代码有不足之处或有更好的方法,可以的话请留言交流,感激不尽!
iLabel.class(用户控件)
Public Class iLabel Private iText As String = "" Private iFontSize As Single = 12 Private iFontBold As Boolean = True Private iFontname As String = "微软雅黑" Private iFontcolor As color = color.White Private iFontBackcolor As color = color.Black Public Sub SetText(ByVal T As String) iText = T : ReDraw() End Sub Public Function GetText() As String Return iText End Function Public Sub SetFont(Optional ByVal FontSize As Single = 0,_ Optional ByVal FontBold As Boolean = True,_ Optional ByVal Fontname As String = "") If FontSize > 0 Then iFontSize = FontSize If FontBold <> iFontBold Then iFontBold = FontBold If Fontname.Length > 0 Then Try Dim tFont As New Font(Fontname,12) iFontname = Fontname Catch ex As Exception End Try End If ReDraw() End Sub Public Function GetFontname() As String Return iFontname End Function Public Function GetFontSize() As Single Return iFontSize End Function Public Function IsFontBold() As Boolean Return iFontBold End Function Public Function GetFontcolor() As color Return iFontcolor End Function Public Function GetBackcolor() As color Return iFontBackcolor End Function Public Sub Setcolor(ByVal Fontcolor As color,ByVal Backcolor As color) iFontBackcolor = Backcolor Me.Backcolor = Backcolor iFontcolor = Fontcolor ReDraw() End Sub Public Sub SetSize(Optional ByVal WIDth As Integer = 0,_ Optional ByVal Height As Integer = 0,_ Optional ByVal Text As String = "",_ Optional ByVal left As Integer = 0,_ Optional ByVal top As Integer = 0) With Me If left > 0 Then .left = left If top > 0 Then .top = top If WIDth > 0 Then .WIDth = WIDth If Height > 0 Then .Height = Height End With If Text.Length > 0 Then iText = Text ReDraw() End Sub Public Function GetSize() As Size Return Me.Size End Function Public Sub ReDraw() Dim nBrush As New SolIDBrush(iFontcolor) Dim Font As New Font(iFontname,iFontSize,CType(IIf(iFontBold,FontStyle.Bold,FontStyle.Regular),FontStyle)) Me.Font = Font Dim bmp As New Bitmap(Me.WIDth,Me.Height) Dim g = Graphics.FromImage(bmp) Dim sizef As Sizef = g.MeasureString(iText,Font) Dim top As Single = (Me.Height - sizef.Height) / 2 g.Clear(iFontBackcolor) TextRenderer.DrawText(g,iText,Font,New Point(0,top),iFontcolor) 'g.DrawString(iText,nBrush,top) Me.BackgroundImage = bmp g = nothing : bmp = nothing End Sub Private Sub iLabel_InvalIDated(sender As Object,e As InvalIDateEventArgs) Handles Me.InvalIDated Dim Font As New Font(iFontname,FontStyle)) Me.Font = Font End SubEnd Class
TXTClass.vb(类)
Public Class TXTClass Private TxtParagraghCount As Integer '文本总段落数 Private TxtParagraghIndex As Integer '文本当前段落引索 Private iTxt() As String '以段落为元素组成集合 Private iTxtPath As String '文本文件位置 Private TxtWordCount As Long '文本总文字数 Public Enum TXTClass_Enum_TState '当前文本状态枚举 NoTxt = 0 '未打开任何文本文件 Inhead = 1 '已打开文件,且当前段落引索在文件头 InMIDdle = 2 '已打开文件,且当前段落引索在文件中部 InTail = 3 '已打开文件,且当前段落引索在文件尾 End Enum Private iTxtState As TXTClass_Enum_TState '当前文本状态 Public Sub New() TxtParagraghCount = 0 : TxtParagraghIndex = 0 : iTxtPath = "" ReDim iTxt(0) : TxtWordCount = 0 iTxtState = TXTClass_Enum_TState.NoTxt End Sub Public Function OpenTxt(ByVal filePath As String,_ Optional ByVal fileCheck As Boolean = False,_ Optional ByVal HasRecord As Boolean = False) As Boolean Try If Not (fileCheck OrElse (filePath.Length > 0 AndAlso IO.file.Exists(filePath))) Then Return False Dim Tmp As String Dim TmpTxt() As String Dim TmpTxtParagraghCount As Integer Dim TmpWordCount As Long Dim iReader As IO.StreamReader '尝试打开文件并尝试获取相关信息 Try iReader = New IO.StreamReader(filePath,System.Text.EnCoding.Default) Tmp = iReader.ReadToEnd : iReader.dispose() TmpTxt = Split(Tmp,vbNewline) TmpTxtParagraghCount = TmpTxt.Length If Not HasRecord Then TmpWordCount = Tmp.LongCount Catch ex As Exception MsgBox("方法:TXTClass.OpenTxt() '尝试打开文件并获取信息' 执行出错!","错误提示") Return False End Try '成功打开文件和获取信息,下面清除原来的信息并写入新文件信息 If Not CloseTxt() Then Return False iTxt = TmpTxt If Not HasRecord Then '如果没有记录文件,则使用默认信息 TxtParagraghCount = TmpTxtParagraghCount : TxtParagraghIndex = -1 TxtWordCount = TmpWordCount iTxtState = TXTClass_Enum_TState.Inhead Else '如果有记录文件则使用记录的信息 End If iTxtPath = filePath Return True Catch ex As Exception MsgBox("方法:TXTClass.OpenTxt() 执行出错!","错误提示") Return False End Try End Function Public Function CloseTxt() As Boolean Try ReDim iTxt(0) : iTxtState = TXTClass_Enum_TState.NoTxt TxtParagraghCount = 0 : TxtParagraghIndex = 0 TxtWordCount = 0 Return True Catch ex As Exception MsgBox("方法:TXTClass.CloseTxt() 执行出错!","错误提示") Return False End Try End Function Public Function GetTxtState() As TXTClass_Enum_TState Return iTxtState End Function Public Function GetParagraghCount() As Integer Return IIf(iTxtState = TXTClass_Enum_TState.NoTxt,-1,TxtParagraghCount) End Function Public Function GetParagraghIndex() As Integer Return IIf(iTxtState = TXTClass_Enum_TState.NoTxt,TxtParagraghIndex) End Function Public Function SetParagraghIndex(ByVal Index As Integer) As Boolean Try If Index >= 0 AndAlso Index < TxtParagraghCount Then TxtParagraghIndex = Index Return True End If Catch ex As Exception MsgBox(ex.ToString,"错误提示") Return False End Try End Function Public Function GetWordCount() As Long Return IIf(iTxtState = TXTClass_Enum_TState.NoTxt,TxtWordCount) End Function Public Function IsHOF() As Boolean 'headOffile '等同 return iif(iTxtState=TXTClass_Enum_TState.Inhead,true,false) Return IIf(TxtParagraghIndex <= 0,True,False) End Function Public Function ISEOF(Optional ByVal Index As Integer = -1) As Boolean 'EndOffile If Index = -1 Then '等同 return iif(iTxtState=TXTClass_Enum_TState.InTail,false) Return IIf(TxtParagraghIndex >= TxtParagraghCount - 1,False) Else Return IIf(Index >= TxtParagraghCount - 1,False) End If End Function Public Function NextParagragh(Optional ByVal WithoutChange As Boolean = True) As String Try '判断是否已打开文件 If iTxtState = TXTClass_Enum_TState.NoTxt Then Return "" If WithoutChange Then '如果只是读取而不改变段落引索 Return IIf(iTxtState = TXTClass_Enum_TState.NoTxt,"",_ IIf(TxtParagraghIndex + 1 >= TxtParagraghCount,"已到尾段!",_ iTxt(TxtParagraghIndex + 1))) Else '读取并自动改变段落引索 TxtParagraghIndex += 1 '判断并修改文件状态 If TxtParagraghIndex >= TxtParagraghCount - 1 Then iTxtState = TXTClass_Enum_TState.InTail Else iTxtState = TXTClass_Enum_TState.InMIDdle End If If TxtParagraghIndex >= TxtParagraghCount Then TxtParagraghIndex = TxtParagraghCount - 1 : Return "已到尾段!" End If Return iTxt(TxtParagraghIndex) End If Catch ex As Exception MsgBox("方法:TXTClass.NextParagragh() 执行出错!","错误提示") Return "" End Try End Function Public Function ThisParagragh(Optional ByVal Index As Integer = -1,Optional ByVal WithoutChange As Boolean = True) As String Try '判断是否已打开文件 If iTxtState = TXTClass_Enum_TState.NoTxt Then Return "" Dim TmplineIndex As Integer = TxtParagraghIndex '临时创建段落引索副本 If TxtParagraghIndex = -1 Then '当段落引索为初始值时自动调整引索为有效值 TmplineIndex = 0 If Not WithoutChange Then TxtParagraghCount = 0 End If If Index <> -1 Then '判断返回默认行还是指定行,Index=-1 默认行 If Index >= 0 AndAlso Index < TxtParagraghCount Then '检查新段落引索有效性 TmplineIndex = Index If Not WithoutChange Then '如果【不是】 只读取而不改变段落引索 TxtParagraghIndex = Index '修改段落引索 '判断并修改文本状态 If TxtParagraghIndex > 0 AndAlso TxtParagraghIndex < TxtParagraghCount - 1 Then iTxtState = TXTClass_Enum_TState.InMIDdle ElseIf TxtParagraghIndex = 0 Then iTxtState = TXTClass_Enum_TState.Inhead ElseIf TxtParagraghIndex = TxtParagraghCount - 1 Then iTxtState = TXTClass_Enum_TState.InTail End If End If Else Return "" End If Else Return iTxt(TmplineIndex) End If Catch ex As Exception MsgBox("方法:TXTClass.LastParagragh() 执行出错!","错误提示") Return "" End Try End Function Public Function LastParagragh(Optional ByVal WithoutChange As Boolean = True) As String Try '判断是否已打开文件 If iTxtState = TXTClass_Enum_TState.NoTxt Then Return "" If Not WithoutChange Then '如果只是读取而不改变段落引索 Return IIf(iTxtState = TXTClass_Enum_TState.NoTxt,_ IIf(TxtParagraghIndex - 1 < 0,"已到首段!",iTxt(TxtParagraghIndex - 1))) Else '读取并自动改变段落引索 TxtParagraghIndex -= 1 '判断并修改文件状态 iTxtState = IIf(TxtParagraghIndex <= 0,_ TXTClass_Enum_TState.Inhead,_ TXTClass_Enum_TState.InMIDdle) If TxtParagraghIndex < 0 Then TxtParagraghIndex = 0 : Return "已到首段!" End If Return iTxt(TxtParagraghIndex) End If Catch ex As Exception MsgBox("方法:TXTClass.LastParagragh() 执行出错!","错误提示") Return "" End Try End FunctionEnd Class
ReadlineClass.vb(类)
Public Class ReadlineClass inherits TXTClass Private ilineCount As Integer Private ilineIndex As Integer Private iline() As String Private TXTWordindex As Long Private Enum LState Godown = 0 GoUp = 1 End Enum Private ilinestate As LState Private iLab As iLabel Public Sub Init(ByRef tLab As iLabel) iLab = tLab End Sub Public Function ISEOL(Optional ByVal Index As Integer = -1) As Boolean 'EndOfline If Index = -1 Then If ilineIndex >= ilineCount - 1 Then Return True Else Return False Else If Index >= ilineCount - 1 Then Return True Else Return False End If End Function Public Function IsHol() As Boolean 'headOfline '判断是否处于iline的首元素 If ilineIndex <= 0 Then Return True Else Return False End Function Public Function GetlineCount() As Integer Return ilineCount End Function Public Function GetlineIndex() As Integer Return ilineIndex End Function Public Function SetlineIndex(ByVal Index As Integer) As Boolean Try If Index >= 0 AndAlso Index < ilineCount Then ilineIndex = Index Return True End If Catch ex As Exception MsgBox(ex.ToString,"错误提示") Return False End Try End Function Public Function GetWordindex() As Long Return TXTWordindex End Function Public Function SetWordindex(ByVal Index As Long) As Boolean If GetTxtState() = TXTClass_Enum_TState.NoTxt Then Return False If Index < 0 OrElse Index >= GetWordCount() Then Return False Try Dim tIndex As Long = TXTWordindex Dim tParagraghIndex As Integer = GetParagraghIndex(),tlineIndex As Integer = GetlineIndex() Dim bak_ParagraghIndex As Integer = tParagraghIndex,bak_lineIndex As Integer = tlineIndex If tIndex < Index Then '向后跳转 '步骤:计入当前行;计入剩余行;计入N段、再计入M行 '判断成功:是否在当前行范围内;是否在剩余行范围内;先是否在段范围内,再判断在哪行范围内 '失败:恢复之前的ParagraghIndex和lineIndex '第一步:计入当前行,判断是否在当前行范围内 tIndex += Thisline.Length If tIndex >= Index Then Return True 'Index在当前段-当前行 '第二步:在第一步的基础上,计入剩余行,判断是否在剩余行范围内 While Not ISEOL(tlineIndex) tlineIndex += 1 : tIndex += Thisline(tlineIndex,True).Length If tIndex >= Index Then If SetlineIndex(tlineIndex) Then Return True 'Index在当前段-第tlineIndex行 Else SetlineIndex(bak_lineIndex) Return False End If End If End While '第三步:在第一、二步基础上,先计入N段判断在哪段范围内,再计入M行判断在哪行范围内 Do tParagraghIndex += 1 : tIndex += ThisParagragh(tParagraghIndex,True).Length Loop Until tIndex > Index '判断在哪段 If Not (SetParagraghIndex(tParagraghIndex) AndAlso _ CutParagragh(ThisParagragh(-1,True),iline) > 0) Then '段落引索设置失败或新段落的分行失败,恢复原来的信息 SetParagraghIndex(bak_ParagraghIndex) CutParagragh(ThisParagragh(-1,iline) SetlineIndex(bak_lineIndex) Return False End If tlineIndex = 0 : tIndex -= ThisParagragh(tParagraghIndex,True).Length Do Until tIndex + Thisline(tlineIndex,True).Length > Index tIndex += Thisline(tlineIndex,True).Length : tlineIndex += 1 Loop If SetlineIndex(tlineIndex) Then Return True Else SetParagraghIndex(bak_ParagraghIndex) CutParagragh(ThisParagragh(-1,iline) SetlineIndex(bak_lineIndex) Return False End If Else '向前跳转 '步骤:计入多余行(向上);计入N段、再计入M行 '判断成功:是否在多余行范围内;先是否在段范围内,再判断在哪行范围内 '失败:恢复之前的ParagraghIndex和lineIndex '第一步:计入多余行(向上),判断是否在多余行范围内 While Not IsHol() tlineIndex -= 1 : tIndex -= Thisline(tlineIndex,True).Length If tIndex <= Index Then If SetlineIndex(tlineIndex) Then Return True Else SetlineIndex(bak_lineIndex) Return False End If End If End While '第二步:在第一步基础上,先向上计入N段判断在哪段范围内,再向上计入M行判断在哪行范围内 Do tParagraghIndex -= 1 : tIndex -= ThisParagragh(tParagraghIndex,True).Length Loop Until tIndex <= Index OrElse IsHOF() If Not (SetParagraghIndex(tParagraghIndex) AndAlso _ CutParagragh(ThisParagragh(-1,iline) SetlineIndex(bak_lineIndex) Return False End If tlineIndex = iline.Length - 1 : tIndex += ThisParagragh(tParagraghIndex,True).Length Do Until tIndex - Thisline(tlineIndex,True).Length <= Index tIndex -= Thisline(tlineIndex,True).Length : tlineIndex -= 1 Loop If SetlineIndex(tlineIndex) Then Return True Else SetParagraghIndex(bak_ParagraghIndex) CutParagragh(ThisParagragh(-1,iline) SetlineIndex(bak_lineIndex) Return False End If End If Catch ex As Exception MsgBox(ex.ToString,"错误提示") Return False End Try End Function Public Function GetReadratio() As Double Dim wCount As Long = GetWordCount() If wCount <= 0 Then Return 0 Return TXTWordindex / wCount End Function Public Function SetReadratio(ByVal Ratio As Double) As Boolean If GetTxtState() = TXTClass_Enum_TState.NoTxt Then Return False Dim DWordindex As Long = CLng(GetWordCount() * Ratio) '定位的wordindex Return SetWordindex(DWordindex) End Function Public Function Nextline(Optional ByVal WithoutChange As Boolean = False) As String Try If Not ISEOL() Then '如果还有下一行 If WithoutChange Then Return iline(ilineIndex + 1) Else TXTWordindex += iline(ilineIndex).Length '减去当前行字数 ilineIndex += 1 : ilinestate = LState.Godown Return iline(ilineIndex) End If Else '无下一行,需要对下一段进行分行 If ISEOF() Then Return "" '如果无下一段,则返回空字符串 Dim tline() As String : ReDim tline(0) If WithoutChange Then If CutParagragh(NextParagragh(True),tline) > 0 Then Return tline(0) Else MsgBox("段落分行出错!" + vbCrLf + "函数:Nextline(),引索:" + GetParagraghIndex() + 1 + vbCrLf + "该段内容在下个提示框显示。","错误提示") MsgBox(NextParagragh(True),"段落分行出错") Return "段落分行出错!" End If Else Dim tlineCount As Integer = CutParagragh(NextParagragh(False),tline) If tlineCount > 0 Then iline = tline : ilineCount = tlineCount ilineIndex = 0 : ilinestate = LState.Godown Return iline(ilineIndex) Else MsgBox("段落分行出错!" + vbCrLf + "函数:Nextline(),引索:" + GetParagraghIndex() + vbCrLf + "该段内容在下个提示框显示。","错误提示") MsgBox(ThisParagragh(),"段落分行出错") Return "段落分行出错!" End If End If End If Catch ex As Exception MsgBox(ex.ToString,"错误提示") Return "" End Try End Function Public Function Thisline(Optional ByVal Index As Integer = -1,Optional ByVal WithoutChange As Boolean = False) As String Try If Index >= 0 Then Return iline(Index) If Not WithoutChange Then ilineIndex = Index Else Return iline(ilineIndex) End If Catch ex As Exception MsgBox(ex.ToString,"错误提示") Return "" End Try End Function Public Function Lastline(Optional ByVal WithoutChange As Boolean = False) As String Try If Not IsHol() Then '如果还有上一行 If WithoutChange Then Return iline(ilineIndex - 1) Else ilineIndex -= 1 : ilinestate = LState.GoUp TXTWordindex -= iline(ilineIndex).Length '减去上一行的字数 Return iline(ilineIndex) End If Else '无下一行,需要对上一段进行分行 If IsHOF() Then Return "" '如果没有上一段,则返回空字段 Dim tline() As String,tlineCount As Integer : ReDim tline(0) If WithoutChange Then tlineCount = CutParagragh(LastParagragh(True),tline) If tlineCount > 0 Then Return tline(tlineCount - 1) Else MsgBox("段落分行出错!" + vbCrLf + "函数:Lastline(),引索:" + GetParagraghIndex() - 1 + vbCrLf + "该段内容在下个提示框显示。","错误提示") MsgBox(LastParagragh(True),"段落分行出错") Return "段落分行出错!" End If Else tlineCount = CutParagragh(LastParagragh(False),tline) If tlineCount > 0 Then iline = tline : ilineCount = tlineCount ilineIndex = ilineCount - 1 : ilinestate = LState.GoUp Return iline(ilineIndex) Else MsgBox("段落分行出错!" + vbCrLf + "函数:Lastline(),引索:" + GetParagraghIndex() + vbCrLf + "该段内容在下个提示框显示。","错误提示") Return "" End Try End Function Private Function CutParagragh(ByVal tText As String,ByRef tmpline() As String) As Integer '根据tLab的信息对tText进行分行,结果以地址的形式储存到tmpline中,返回tmplineCount Try ReDim tmpline(0) If tText.Length = 0 Then Return 1 Dim tLab As iLabel = iLab Dim tFont As Font = tLab.Font Dim g As Graphics = tLab.CreateGraphics Dim tmplineCount As Integer = 0 Dim iLabWIDth As Integer = tLab.WIDth '经测试得出的比例 Dim EachWorDWIDth As Integer = TextRenderer.MeasureText("测",tFont).WIDth Dim EachlineWordCount As Integer = iLabWIDth / EachWorDWIDth EachlineWordCount += CInt(EachlineWordCount * 0.5) Dim iStr As String = tText Dim tStrWIDth As Integer = 0,tStr As String = "",tStrCount As Integer = 0 Dim iStrWIDth As Integer = TextRenderer.MeasureText(g,iStr,tFont).WIDth While iStrWIDth > iLabWIDth '如果初始(剩余)字串的长度比限定的长则进行(继续)分割 '判断字串的个数是否比默认的小,若比默认的小则以字串的个数作为默认值。该值用于初次截取字串 tStrCount = iStr.Length tStrCount = IIf(EachlineWordCount >= tStrCount,tStrCount - 1,EachlineWordCount) '-1的必要性:由于已知tStrCount个字的长度比限定的长,所以按照这个值截取出来的长度肯定不符合,因此尝试截取tStrCount-1个字 '判断初次截取的长度是否符合要求 tStr = MID(iStr,1,tStrCount) tStrWIDth = TextRenderer.MeasureText(tStr,tFont).WIDth If tStrWIDth > iLabWIDth Then '初次截取的字串长度较长,尝试减少字符个数 Do '根据超出的长度判断减少多少个字符 tStrCount -= CIntA(tStrWIDth - iLabWIDth,EachWorDWIDth) tStr = MID(iStr,tStrCount) tStrWIDth = TextRenderer.MeasureText(tStr,tFont).WIDth Loop While tStrWIDth > iLabWIDth ReDim Preserve tmpline(tmplineCount) tmpline(tmplineCount) = tStr tmplineCount += 1 Else '初次截取的字串长度较短,尝试增加字符个数 Do '根据剩余的长度判断增加多少个字符 tStrCount += CIntA(iLabWIDth - tStrWIDth,EachWorDWIDth) 'tStrCount += 1 '防止增加太多导致超出范围 tStr = MID(iStr,tFont).WIDth Loop While tStrWIDth < iLabWIDth ReDim Preserve tmpline(tmplineCount) tStrCount -= 1 : tmpline(tmplineCount) = MID(tStr,tStrCount) tmplineCount += 1 End If iStr = MID(iStr,tStrCount + 1) iStrWIDth = TextRenderer.MeasureText(iStr,tFont).WIDth End While '如果(剩余)字符串的长度小于限定的值,则直接储存。 If iStr.Length > 0 Then ReDim Preserve tmpline(tmplineCount) tmpline(tmplineCount) = iStr tmplineCount += 1 End If Return tmplineCount Catch ex As Exception MsgBox(ex.ToString,"错误提示") Return 0 End Try End Function Private Function CIntA(ByVal V1 As Integer,ByVal V2 As Integer) As Integer '作用:返回不小于V1除以V2的商的值 Dim v As Integer = V1 \ V2 Return IIf(V1 Mod V2 = 0,v,v + 1) End FunctionEnd Class总结
以上是内存溢出为你收集整理的【VB.NET】自己写的阅读器类源码全部内容,希望文章能够帮你解决【VB.NET】自己写的阅读器类源码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)