当前位置: 首页 > 编程语言 > C#

如何在不滚动和丢失选择的情况下将文本附加到RichTextBox?Share

时间:2023-04-11 12:14:11 C#

如何在不滚动和丢失选择的情况下将文本附加到RichTextBox?我需要将文本附加到RichTextBox并且我需要在不滚动文本框或丢失当前文本选择的情况下执行此操作,这可能吗?WinForms中的RichTextBox在你使用文本和选择文本的方法时非常开心。我有一个标准的替代品,它使用以下代码关闭绘画和滚动:[DllImport("user32.dll")]staticexternIntPtrSendMessage(IntPtrhWnd,Int32wMsg,Int32wParam,IntPtrlParam);constintWM_USER=0x400;constintWM_SETREDRAW=0x000B;constintEM_GETEVENTMASK=WM_USER+constEM_SETEVENTMASK=WM_USER+69;constintEM_GETSCROLLPOS=WM_USER+221;constintEM_SETSCROLLPOS=WM_USER+222;点_ScrollPoint;布尔_绘画=真;IntPtr_EventMask;int_SuspendIndex=0;(_Painting){_SuspendIndex=this.SelectionStart;_SuspendLength=this.SelectionLength;SendMessage(this.Handle,EM_GETSCROLLPOS,0,ref_ScrollPoint);SendMessage(this.Handle,WM_SETREDRAW,0,IntPtr.Zero);_EventMask=SendMessage(this.Handle,EM_GETEVENTMASK,0,IntPtr.Zero);_绘画=错误的;}}publicvoidResumePainting(){if(!_Painting){this.Select(_SuspendIndex,_SuspendLength);SendMessage(this.Handle,EM_SETSCROLLPOS,0,ref_ScrollPoint);SendMessage(this.Handle,EM_SETEVENTMASK,0,_EventMask);SendMessage(this.Handle,WM_SETREDRAW,1,IntPtr.Zero);_绘画=真;这个。无效();然后从我的表单中,我可以愉快地拥有一个无闪烁的richtextbox控件:richTextBoxEx1。暂停绘画();richTextBoxEx1.AppendText("嘿!");richTextBoxEx1.ResumePainting();根据LarsTech文章,这里有一些不错的东西:usingSystem;使用System.Runtime.InteropServices;使用System.Windows.Forms;使用系统绘图;namespaceyournamespace{classRichTextBoxEx:RichTextBox{[DllImport("user32.dll")]staticexternIntPtrSendMessage(IntPtrhWnd,Int32wMsg,Int32wParam,refPointlParam);[DllImport("user32.dll")]staticexternIntPtrSendMessage(IntPtrhWnd,Int32wMsg,Int32wParam,IntPtrlParam);constintWM_USER=0x400;constintWM_SETREDRAW=0x000B;常数我ntEM_GETEVENTMASK=WM_USER+59;constintEM_SETEVENTMASK=WM_USER+69;constintEM_GETSCROLLPOS=WM_USER+221;constintEM_SETSCROLLPOS=WM_USER+222;点_ScrollPoint;布尔_绘画=真;IntPtr_EventMask;int_SuspendIndex=0;int_SuspendLength=0;公共布尔自动滚动=真;publicvoidSuspendPainting(){if(_Painting){_SuspendIndex=this.SelectionStart;_SuspendLength=this.SelectionLength;SendMessage(this.Handle,EM_GETSCROLLPOS,0,ref_ScrollPoint);SendMessage(this.Handle,WM_SETREDRAW,0,IntPtr.Zero);_EventMask=SendMessage(this.Handle,EM_GETEVENTMASK,0,IntPtr.Zero);_绘画=假;}}publicvoidResumePainting(){if(!_Painting){this.Select(_SuspendIndex,_SuspendLength);SendMessage(this.Handle,EM_SETSCROLLPOS,0,ref_ScrollPoint);SendMessage(this.Handle,EM_SETEVENTMASK,0,_EventMask);SendMessage(this.Handle,WM_SETREDRAW,1,IntPtr.Zero);_绘画=真;这个。无效();}}新ppublicvoidAppendText(stringtext)//覆盖RichTextBox.AppendText{if(Autoscroll)base.AppendText(text);else{暂停绘画();base.AppendText(文本);恢复绘画();你可以这样做使用这个:vartextbox=newRichTextBoxEx();textbox.Autoscroll=false;textbox.AppendText("嗨");这个解决方案几乎可以工作,除了它不能正确处理反向选择(其中插入符号位于选择的开头,而不是结尾-例如SHIFT+LEFT或向上鼠标拖动以选择文本)这是一个改进的版本以下附加功能:这意味着您可以将插入符放在文本的末尾并观察要添加的文本(想想日志文件监控)。这也意味着您可以执行CTRL+A来全选并自动在您的选择中包含任何其他文本。classRichTextBoxEx:RichTextBox{[DllImport("user32.dll")]privatestaticexternIntPtrSendMessage(IntPtrhWnd,Int32wMsg,Int32wParam,refPointlParam);[DllImport("user32.dll")]privatestaticexternIntPtrSendMessage(IntPtrhWnd,Int32wMsg,Int32wParam,IntPtrlParam);[DllImport("user32")]privatestaticexternintGetCaretPos(outPointp);constintWM_USER=0x400;constintWM_SETREDRAW=0x000B;constintEM_GETEVENTMASK=WM_USER+59;constintEM_SETEVENTMASK=WM_USER+69;constintEM_GETSCROLLPOS=WM_USER+221;constintEM_SETSCROLLPOS=WM_USER+222;私有点oScrollPoint;privateboolbPainting=true;私有IntPtroEventMask;私人诠释iSuspendCaret;私有intiSuspendIndex;私人诠释iSuspendLength;私人布尔bWasAtEnd;publicintCaretIndex{get{点oCaret;GetCaretPos(outoCaret);返回this.GetCharIndexFromPosition(oCaret);}}publicvoidAppendTextWithoutScroll(stringtext){this.Susp结束绘画();this.AppendText(文本);这个.ResumePainting();}privatevoidSuspendPainting(){if(this.bPainting){this.iSuspendCaret=this.CaretIndex;this.iSuspendIndex=this.SelectionStart;this.iSuspendLength=this.SelectionLength;this.bWasAtEnd=this.iSuspendIndex+this.iSuspendLength==this.TextLength;SendMessage(this.Handle,EM_GETSCROLLPOS,0,refthis.oScrollPoint);SendMessage(this.Handle,WM_SETREDRAW,0,IntPtr.Zero);this.oEventMask=SendMessage(this.Handle,EM_GETEVENTMASK,0,IntPtr.Zero);this.bPainting=false;}}privatevoidResumePainting(){if(!this.bPainting){if(this.iSuspendLength==0){if(!bWasAtEnd){this.Select(this.iSuspendIndex,0);}}else{//原始选择到文本结尾if(bWasAtEnd){//在新文本末尾保持选择结束}if(this.iSuspendCaret>this.iSuspendIndex){//前向选择(插入符号在末尾)this.Se选择(this.iSuspendIndex,this.iSuspendLength);}else{//反向选择(插入符号在开始处)this.Select(this.iSuspendIndex+this.iSuspendLength,-this.iSuspendLength);}}SendMessage(this.Handle,EM_SETSCROLLPOS,0,refthis.oScrollPoint);SendMessage(this.Handle,EM_SETEVENTMASK,0,this.oEventMask);SendMessage(this.Handle,WM_SETREDRAW,1,IntPtr.Zero);this.bPainting=true;这个。无效();这应该做你想做的:以上是C#学习教程:HowtoappendtexttoRichTextBoxwithoutscrollingandlosingselection?分享的所有内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注——Text+="dsfkljwerhsdlf"RichTextBox1.SelectionStart=tempStartRichTextBox1.SelectionLength=tempLength本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢