c#RichTextBox边框颜色

c#RichTextBox边框颜色,c#,winforms,colors,border,richtextbox,C#,Winforms,Colors,Border,Richtextbox,我正在尝试创建一个带有边框颜色的自定义RichTextBox,但我遇到了一个问题。。。 我的边框颜色没有显示出来 这是我的密码: public partial class AlXRichTextBox : RichTextBox { private RichTextBox textBox; private Color borderColor; public AlXRichTextBox() { InitializeComponent();

我正在尝试创建一个带有边框颜色的自定义
RichTextBox
,但我遇到了一个问题。。。 我的边框颜色没有显示出来

这是我的密码:

public partial class AlXRichTextBox : RichTextBox
{
    private RichTextBox textBox;

    private Color borderColor;

    public AlXRichTextBox()
    {
        InitializeComponent();
    }
    public Color BorderColor
    {
        get { return borderColor; }
        set { borderColor = value; Invalidate(); }
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        Pen p = new Pen(borderColor);
        Graphics g = e.Graphics;
        int variance = 3;
        //g.DrawRectangle(p, new Rectangle(base.Location.X - variance, base.Location.Y - variance, base.Width + variance, base.Height + variance));
        ControlPaint.DrawBorder(e.Graphics, base.ClientRectangle, borderColor, ButtonBorderStyle.Solid);
    }

    private void InitializeComponent()
    {
            this.textBox = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.textBox.Location = new System.Drawing.Point(0, 0);
            this.textBox.Name = "richTextBox1";
            this.textBox.Size = new System.Drawing.Size(100, 96);
            this.textBox.TabIndex = 0;
            this.textBox.Text = "";
            this.textBox.Multiline = true;
            this.textBox.BorderStyle = BorderStyle.None;
            // 
            // AlXRichTextBox
            // 
            this.Size = new System.Drawing.Size(278, 123);
            this.ResumeLayout(false);
    }
}
这有什么问题吗?

指的是:

替代OnPaint将不允许您修改所有控件的外观。那些所有绘制都由Windows完成的控件(例如TextBox)从不调用其OnPaint方法,因此永远不会使用自定义代码。有关要修改的特定控件,请参阅帮助文档,以查看OnPaint方法是否可用。有关所有Windows窗体控件的列表,请参见在Windows窗体上使用的控件。如果控件没有将OnPaint作为成员方法列出,则不能通过重写此方法来更改其外观。有关自定义绘制的详细信息,请参见自定义控件绘制和渲染

但是有一个“hack”,您可以通过调用以下代码来实现调用Paint方法:

 private const int WM_PAINT = 15;
 protected override void WndProc(ref System.Windows.Forms.Message m)
 {
     base.WndProc(ref m);
     if (m.Msg == WM_PAINT && !inhibitPaint)
     {
         // raise the paint event
         using (Graphics graphic = base.CreateGraphics())
             OnPaint(new PaintEventArgs(graphic,
             base.ClientRectangle));
     }
  }

  private bool inhibitPaint = false;

  public bool InhibitPaint
  {
      set { inhibitPaint = value; }
  }
Src:

另一点是,您不能在矩形(这是RichTB组件的总大小)之外绘制。因此,您实际上希望为他提供不同的坐标(较小的内部坐标),然后您将绘制到外部

您的类将如下所示:

public partial class AlXRichTextBox : RichTextBox
{
    private Color borderColor = Color.Red;

    public Color BorderColor
    {
        get { return borderColor; }
        set { borderColor = value; Invalidate(); }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        int variance = 3;
        e = new PaintEventArgs(e.Graphics, new Rectangle(e.ClipRectangle.X + variance, e.ClipRectangle.Y + variance, e.ClipRectangle.Width - variance, e.ClipRectangle.Height - variance));
        base.OnPaint(e);

        Pen p = new Pen(borderColor, variance);
        Graphics g = e.Graphics;
        g.DrawRectangle(p, new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height));
    }


    private const int WM_PAINT = 15;
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_PAINT && !inhibitPaint)
        {
            // raise the paint event
            using (Graphics graphic = base.CreateGraphics())
                OnPaint(new PaintEventArgs(graphic,
                 base.ClientRectangle));
        }

    }

    private bool inhibitPaint = false;

    public bool InhibitPaint
    {
        set { inhibitPaint = value; }
    }
}
重要


由于预计此控件不会被Paint更改,因此您将得到“not nice”行为的变化,如边界,新的元素等。如果你想使用这样的元素,考虑使用<强> WPF - Windows演示基金会<强>。他们更美好的模板项目和修改设计。< / P > < P >有点晚的答案,但我在同一条路,你这些天,它让我到这S。解决方案,对我有效:

using System;
using System.Drawing;
using System.Windows.Forms;

public class MyRichTextBox : RichTextBox
{
    private const UInt32 WM_PAINT = 0x000F;
    private const UInt32 WM_USER = 0x0400;
    private const UInt32 EM_SETBKGNDCOLOR = (WM_USER + 67);
    private const UInt32 WM_KILLFOCUS = 0x0008;

    public MyRichTextBox()
    {
        this.BorderStyle = System.Windows.Forms.BorderStyle.None;
    }

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        base.WndProc(ref m);

        Graphics g = Graphics.FromHwnd(Handle);
        Rectangle bounds = new Rectangle(0, 0, Width - 1, Height - 1);
        Pen p = new Pen(SystemColors.Highlight, 3);

        if (m.Msg == WM_PAINT)
        {
            if (this.Enabled == true)
            {

                if (this.Focused)
                {
                    g.DrawRectangle(p, bounds);
                }

                else
                {
                    g.DrawRectangle(SystemPens.ControlDark, bounds);
                }

            }
            else
            {
                g.FillRectangle(Brushes.White, bounds);
                g.DrawRectangle(SystemPens.Control, bounds);
            }
        }

        if (m.Msg == EM_SETBKGNDCOLOR) //color disabled background
        {
            Invalidate();
        }

        if (m.Msg == WM_KILLFOCUS) //set border back to normal on lost focus
        {
            Invalidate();
        }
    }

}

此富文本框更改了3种边框颜色-启用、聚焦和禁用禁用背景。正如您所见,代码简单而简短。唯一的诀窍是覆盖KILL_FOCUS和EM_SETBKGNDCOLOR消息(此消息用于更改禁用背景),富文本框应为BorderStyle=None。干杯!

谢谢,先生,回答得很好