如何更改禁用控件的样式?

当一个WinForm元素被禁用时,它会变灰。 是否可以禁用元素,但调整禁用的样式以使其仍然显示为启用(不显示为灰色)?

防止可聚焦控制成为焦点需要采取一些措施。 您必须包含一个控件,该控件确实将此类的重点放在所有尝试上:

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); } } 

禁用的样式是标准Windows行为的一部分。 如果要更改样式,则必须自己绘制控件,这意味着您必须处理Paint方法,并且可能必须重写OnPaint

请参阅覆盖OnPaint方法和自定义控件绘画和渲染 。