C#、VB.Net RichTextBox变更文件文本内容的方法,并且通过重写实现不闪屏的效果
C#、VB.Net RichTextBox变更文件文本内容的方法。
使用RichTextBox控件时,免不了需要变更文件中的内容。
本文采用的办法是使用 SelectionStart、SelectionLengthSelectedText三个方法进行变更。
同时重写RichTextBox,给出解决在变更时可能出现短暂的蓝底选择字符画面的办法。
Private Sub ReplaceTextBox(pStart As Integer, pLength As Integer, pStr As String) Try JMyBox1.SelectionStart = pStart LockPaint() '停止屏幕更新RichTextBox JMyBox1.SelectionLength = pLength RePaint() '恢复屏幕更新RichTextBox If pStr = "" Then '可能是我水平不行,哪里设置不对,或者是原生RichTextBox的问题 '导致如果pStr=""时未能成功更新,但更新为vbNullChar时却能成功 JMyBox1.SelectedText = vbNullChar Else JMyBox1.SelectedText = pStr End If Catch ex As Exception End Try End Sub Private Sub LockPaint() JMyBox1.LockPaint() End Sub Private Sub RePaint() JMyBox1.RePaint() End Sub
重写RichTextBox:此模块参考某度搜索
Public Class jMyBox Inherits RichTextBox Private Class paintHelper Inherits Control Public Sub DefaultWndProc(ByRef m As Message) DefWndProc(m) End Sub End Class Private Const WM_PAINT As Integer = &H000F Private pHelp As New paintHelper Private iLockPaint As Boolean Public Sub LockPaint() iLockPaint = True End Sub Public Sub RePaint() iLockPaint = False End Sub Protected Overrides Sub WndProc(ByRef m As Message) Select Case m.Msg Case WM_PAINT If iLockPaint Then pHelp.DefaultWndProc(m) Exit Sub End If MyBase.WndProc(m) Exit Sub End Select MyBase.WndProc(m) End Sub End Class
1、重写RichTextBox的原因,是因为在使用Selection...方法更改内容时,在运行了
JMyBox1.SelectionLength = pLength
代码时,当文本内容很长时,屏幕会有可能出现短暂的字符选择的界面。为了不显示这个画面,需要重写RichTextBox,接管WndProc方法中的 WM_PAINT 消息。
使用后的效果:
使用前的效果:

转载请注明 : 文章转载自不了阁-飞哥 blog.forbs.cn
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载请务必注明出处,尊重他人劳动成果共创和谐网络环境。





