C# 如何在禁用富文本框时更改其背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1946139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:07:14  来源:igfitidea点击:

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

c#.netwinformsrichtextbox

提问by Badr

Whenever I set the RichTextBox.Enabledproperty to false, its background color is automatically set to gray as it is set to the color in system color which is set in the control panel. How can I change its color to black even if I set it to disabled?

每当我将该RichTextBox.Enabled属性设置为 false 时,它​​的背景颜色都会自动设置为灰色,因为它被设置为在控制面板中设置的系统颜色中的颜色。即使将其设置为禁用,如何将其颜色更改为黑色?

采纳答案by Mikael Svenson

See: How to change the font color of a disabled TextBox?

请参阅:如何更改禁用的 TextBox 的字体颜色?

[Edit - code example added]

[编辑 - 添加了代码示例]

richTextBox.TabStop = false;
richTextBox.ReadOnly = true;
richTextBox.BackColor = Color.DimGray;
richTextBox.Cursor = Cursors.Arrow;
richTextBox.Enter += richTextBox_Enter;

private void richTextBox_Enter(object sender, EventArgs e)
{
    // you need to set the focus somewhere else. Eg a label.
    SomeOtherControl.Focus();
}

or as en extension method (I realized you don't have to put it in readonly since the Enter event catches any input):

或作为扩展方法(我意识到您不必将其置于只读状态,因为 Enter 事件会捕获任何输入):

public static class MyExtensions
{
    public static void Disable( this Control control, Control focusTarget )
    {
        control.TabStop = false;
        control.BackColor = Color.DimGray;
        control.Cursor = Cursors.Arrow;
        control.Enter += delegate { focusTarget.Focus(); };
    }
}

回答by P.K

Take a look at DrawStringDisabled Method. You will have to override OnPaintmethod and then use DrawStringDisabled method. But, if I was at your place then I will go with Mikael Svenson'sanswer.

看看DrawStringDisabled 方法。您必须覆盖OnPaint方法,然后使用 DrawStringDisabled 方法。但是,如果我在你的位置,那么我会同意 Mikael Svenson 的回答。

回答by Toby Twerl

Create a Custom Richtextbox as below This will produce a Richtextbox with a transparent Backcolor. You can then place this control on a suitably colored panel.

创建一个自定义 Richtextbox 如下 这将产生一个带有透明背景色的 Richtextbox。然后,您可以将此控件放在适当颜色的面板上。

Public Class MyRichTextBox
Inherits RichTextBox
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function
Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Dim prams As CreateParams = MyBase.CreateParams
        If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then
            prams.ExStyle = prams.ExStyle Or &H20 'Makes Transparent
            prams.ClassName = "RICHEDIT50W"
        End If
        Return prams
    End Get
End Property

回答by Mohsen Mohkami

its to late but its not a bad way,

它来晚了,但它不是一个坏方法,

    private void richTextBox1_ReadOnlyChanged(object sender, EventArgs e)
    {
        //just here instead of White select your color

        richTextBox1.BackColor = Color.White;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.ReadOnly = true;
    }

回答by shalini

Set the backcolor property to your desired color and then set the richtextbox to readonly.

将背景色属性设置为您想要的颜色,然后将富文本框设置为只读。

Ex:

前任:

richTextBox.BackColor = Color.White;
richTextBox.ReadOnly = true; 

回答by edoedoedo

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.

您可以安全地设置 Enabled = true 和 ReadOnly = false,它就像一个标签,防止焦点、用户输入、光标更改,而不会被实际禁用。

See if it works for you. Greetings

看看它是否适合你。你好