|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace PatchManager |
| 9 | +{ |
| 10 | + public class RichTextBoxRedrawHandler |
| 11 | + { |
| 12 | + RichTextBox rtb; |
| 13 | + |
| 14 | + public RichTextBoxRedrawHandler(RichTextBox _rtb) |
| 15 | + { |
| 16 | + rtb = _rtb; |
| 17 | + } |
| 18 | + [DllImport("user32.dll")] |
| 19 | + private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, ref Point lParam); |
| 20 | + |
| 21 | + [DllImport("user32.dll")] |
| 22 | + private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, IntPtr lParam); |
| 23 | + |
| 24 | + const int WM_USER = 1024; |
| 25 | + const int WM_SETREDRAW = 11; |
| 26 | + const int EM_GETEVENTMASK = WM_USER + 59; |
| 27 | + const int EM_SETEVENTMASK = WM_USER + 69; |
| 28 | + const int EM_GETSCROLLPOS = WM_USER + 221; |
| 29 | + const int EM_SETSCROLLPOS = WM_USER + 222; |
| 30 | + |
| 31 | + private Point _ScrollPoint; |
| 32 | + private bool _Painting = true; |
| 33 | + private IntPtr _EventMask; |
| 34 | + private int _SuspendIndex = 0; |
| 35 | + private int _SuspendLength = 0; |
| 36 | + |
| 37 | + public void SuspendPainting() |
| 38 | + { |
| 39 | + if (_Painting) |
| 40 | + { |
| 41 | + _SuspendIndex = rtb.SelectionStart; |
| 42 | + _SuspendLength = rtb.SelectionLength; |
| 43 | + SendMessage(rtb.Handle, EM_GETSCROLLPOS, 0, ref _ScrollPoint); |
| 44 | + SendMessage(rtb.Handle, WM_SETREDRAW, 0, IntPtr.Zero); |
| 45 | + _EventMask = SendMessage(rtb.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero); |
| 46 | + _Painting = false; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public void ResumePainting() |
| 51 | + { |
| 52 | + if (!_Painting) |
| 53 | + { |
| 54 | + rtb.Select(_SuspendIndex, _SuspendLength); |
| 55 | + SendMessage(rtb.Handle, EM_SETSCROLLPOS, 0, ref _ScrollPoint); |
| 56 | + SendMessage(rtb.Handle, EM_SETEVENTMASK, 0, _EventMask); |
| 57 | + SendMessage(rtb.Handle, WM_SETREDRAW, 1, IntPtr.Zero); |
| 58 | + _Painting = true; |
| 59 | + rtb.Invalidate(); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments