Relleno de cuadro de texto enriquecido entre el texto y el borde

Relleno de cuadro de texto enriquecido entre el texto y el borde

El RichTextBox no tiene propiedad de relleno.

Se puede lograr un relleno rápido y sucio colocando RichTextBox en un Panel , que tiene el mismo BackColor propiedad como RichTextBox (normalmente Color.White ).

Luego, configura el Dock propiedad de RichTextBox a Fill y juega con el Padding propiedades del control Panel.


Hay EM_GETRECT y EM_SETRECT .

Combinando esos dos juntos, puedes hacer esto:

…parece esto:

He escrito una pequeña clase de extensión de C# para terminar con todo esto.

Ejemplo de uso:

const int dist = 24;
richTextBox1.SetInnerMargins(dist, dist, dist, 0);

Esto establece los márgenes internos izquierdo, superior y derecho en 24, dejando el inferior en cero.

Tenga en cuenta que al desplazarse, el margen superior permanece como se establece, dando algo como esto:

Personalmente, esto me parece "antinatural". Preferiría que, al desplazarse, el margen superior también se convierta en cero.

Tal vez haya una solución para eso...

Código fuente completo

A partir de la solicitud:

public static class RichTextBoxExtensions
{
    public static void SetInnerMargins(this TextBoxBase textBox, int left, int top, int right, int bottom)
    {
        var rect = textBox.GetFormattingRect();

        var newRect = new Rectangle(left, top, rect.Width - left - right, rect.Height - top - bottom);
        textBox.SetFormattingRect(newRect);
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public readonly int Left;
        public readonly int Top;
        public readonly int Right;
        public readonly int Bottom;

        private RECT(int left, int top, int right, int bottom)
        {
            Left = left;
            Top = top;
            Right = right;
            Bottom = bottom;
        }

        public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom)
        {
        }
    }

    [DllImport(@"User32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)]
    private static extern int SendMessageRefRect(IntPtr hWnd, uint msg, int wParam, ref RECT rect);

    [DllImport(@"user32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)]
    private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam);

    private const int EmGetrect = 0xB2;
    private const int EmSetrect = 0xB3;

    private static void SetFormattingRect(this TextBoxBase textbox, Rectangle rect)
    {
        var rc = new RECT(rect);
        SendMessageRefRect(textbox.Handle, EmSetrect, 0, ref rc);
    }

    private static Rectangle GetFormattingRect(this TextBoxBase textbox)
    {
        var rect = new Rectangle();
        SendMessage(textbox.Handle, EmGetrect, (IntPtr) 0, ref rect);
        return rect;
    }
}

Tuve este mismo problema y la respuesta descrita no me ayudó, esto funcionó para mí, así que lo compartiré si me ayuda.

richTextBox1.SelectAll();
richTextBox1.SelectionIndent += 15;//play with this values to match yours
richTextBox1.SelectionRightIndent += 15;//this too
richTextBox1.SelectionLength = 0;
//this is a little hack because without this
//i've got the first line of my richTB selected anyway.
richTextBox1.SelectionBackColor = richTextBox1.BackColor;