How to change the background color of a rich text box when it is disabled?

后端 未结 6 837
庸人自扰
庸人自扰 2020-12-10 03:46

Whenever I set the RichTextBox.Enabled property to false, its background color is automatically set to gray as it is set to the color in system color which is s

6条回答
  •  感情败类
    2020-12-10 04:14

    I've just found a great way of doing that. It should work with any Control:

    public class DisabledRichTextBox : System.Windows.Forms.RichTextBox
    {
        // See: http://wiki.winehq.org/List_Of_Windows_Messages
    
        private const int WM_SETFOCUS   = 0x07;
        private const int WM_ENABLE     = 0x0A;
        private const int WM_SETCURSOR  = 0x20;
    
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            if (!(m.Msg == WM_SETFOCUS || m.Msg == WM_ENABLE || m.Msg == WM_SETCURSOR))
                base.WndProc(ref m);
        }
    }
    

    You can safely set Enabled = true and ReadOnly = false, and it will act like a label, preventing focus, user input, cursor change, without being actually disabled.

    See if it works for you. Greetings

提交回复
热议问题