Disable Painting of the VScrollbar in a System.Windows.Forms.RichTextBox

后端 未结 1 1661
星月不相逢
星月不相逢 2021-01-17 04:31

I have a custom control inherited from RichTextBox. This control has the ability to \"disable\" rich text editing. I achive this by just setting the Rtf property to the text

相关标签:
1条回答
  • 2021-01-17 05:12

    SuspendLayout() isn't going to have an effect, there are no child controls inside an RTB that need to be arranged. RTB is missing the Begin/EndUpdate() methods that most controls have, although it supports it. It suspends painting, although I'm not so sure it suspends updates to the scrollbar. Add them as follows:

    public void BeginUpdate() {
      SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
    }
    public void EndUpdate() {
      SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero); 
    }
    
    // P/invoke declarations
    private const int WM_SETREDRAW = 0xb;
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private extern static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); 
    

    The better way to prevent the user from editing text is to set the ReadOnly property to True. Removing the scrollbar entirely is possible too by overriding CreateParams.

    0 讨论(0)
提交回复
热议问题