How do I change the style of a disabled control?

后端 未结 2 1355
野趣味
野趣味 2021-01-19 19:18

When a WinForm element is disabled, it sort of grays out. Is it possible to disable an element, but adjust the disabled style so it still looks enabled (not grayed out)?

2条回答
  •  抹茶落季
    2021-01-19 20:12

    Preventing a focusable control from taking the focus takes a number of counter-measures. You will have to include a control that does take the focus for this class to be resist all attempts:

    using System;
    using System.Windows.Forms;
    
    class RichLabel : RichTextBox {
        public RichLabel() {
            this.ReadOnly = true;
            this.TabStop = false;
            this.SetStyle(ControlStyles.Selectable, false);
        }
        protected override void OnEnter(EventArgs e) {
            if (!DesignMode) this.Parent.SelectNextControl(this, true, true, true, true);
            base.OnEnter(e);
        }
        protected override void WndProc(ref Message m) {
            if (m.Msg < 0x201 || m.Msg > 0x20e)
                base.WndProc(ref m);
        }
    }
    

提交回复
热议问题