Česky
En français


WinForms RichTextBox with paragraph spacing in C#


While working on a software project for Collège technique et Lycée de Lowanatom, I have found I needed a RichTextBox in C# WinForms, that is capable of paragraph spacing. Sadly all the forums I visited were saying it is "impossible" or "too difficult". Well ... it appears, that there is a lot of hidden capabilities, that RichTextBox does not expose. After some fiddling, I have ended with a viable solution, which appears to be very simple. The complete custom control code is below. Enjoy and happy coding!

Tip: in order to make the spaces really visible, you will need to set values in orders of hundreds, e.g. 250.

The following links may be of interest:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Exerc
{
    class RichTextBoxWithParagraphSpacing : RichTextBox
    {
        private const int PFM_SPACEBEFORE = 64;
        private const int PFM_SPACEAFTER = 128;
        private const int EM_SETPARAFORMAT = 1095;
        private const int SCF_SELECTION = 1;

        public int SelectionParagraphSpacingAfter
        {
            set
            {
                PARAFORMAT fmt = new PARAFORMAT();
                fmt.cbSize = Marshal.SizeOf(fmt);
                fmt.dwMask = PFM_SPACEAFTER;
                fmt.dySpaceAfter = value;
                SendMessage(new HandleRef(this, this.Handle),
                             EM_SETPARAFORMAT,
                             SCF_SELECTION,
                             ref fmt
                           );
            }
        }

        public int SelectionParagraphSpacingBefore
        {
            set
            {
                PARAFORMAT fmt = new PARAFORMAT();
                fmt.cbSize = Marshal.SizeOf(fmt);
                fmt.dwMask = PFM_SPACEBEFORE;
                fmt.dySpaceBefore = value;
                SendMessage(new HandleRef(this, this.Handle),
                             EM_SETPARAFORMAT,
                             SCF_SELECTION,
                             ref fmt
                           );
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct PARAFORMAT
        {
            public int cbSize;
            public uint dwMask;
            public short wNumbering;
            public short wReserved;
            public int dxStartIndent;
            public int dxRightIndent;
            public int dxOffset;
            public short wAlignment;
            public short cTabCount;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public int[] rgxTabs;

            // PARAFORMAT2 from here onwards.
            public int dySpaceBefore;
            public int dySpaceAfter;
            public int dyLineSpacing;
            public short sStyle;
            public byte bLineSpacingRule;
            public byte bOutlineLevel;
            public short wShadingWeight;
            public short wShadingStyle;
            public short wNumberingStart;
            public short wNumberingStyle;
            public short wNumberingTab;
            public short wBorderSpace;
            public short wBorderWidth;
            public short wBorders;
        }

        [DllImport("user32", CharSet = CharSet.Auto)]
        private static extern int SendMessage(HandleRef hWnd,
                                               int msg,
                                               int wParam,
                                               ref PARAFORMAT lp);
    }

}

Wallis & Futuna