c# - How to append text to RichTextBox without scrolling and losing selection? -
i need append text richtextbox, , need perform without making text box scroll or lose current text selection, possible?
the richtextbox in winforms quite flicker happy when play around text , select-text methods.
i have standard replacement turn off painting , scrolling following code:
class richtextboxex: richtextbox { [dllimport("user32.dll")] static extern intptr sendmessage(intptr hwnd, int32 wmsg, int32 wparam, ref point lparam); [dllimport("user32.dll")] static extern intptr sendmessage(intptr hwnd, int32 wmsg, int32 wparam, intptr lparam); const int wm_user = 0x400; const int wm_setredraw = 0x000b; const int em_geteventmask = wm_user + 59; const int em_seteventmask = wm_user + 69; const int em_getscrollpos = wm_user + 221; const int em_setscrollpos = wm_user + 222; point _scrollpoint; bool _painting = true; intptr _eventmask; int _suspendindex = 0; int _suspendlength = 0; public void suspendpainting() { 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); _painting = false; } } public void resumepainting() { 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); _painting = true; this.invalidate(); } } }
and form, can happily have flicker-free richtextbox control:
richtextboxex1.suspendpainting(); richtextboxex1.appendtext("hey!"); richtextboxex1.resumepainting();
Comments
Post a Comment