skip to Main Content

How to edit line spacing and character spacing on richtextbox in winform? I’ve tried PARAFORMAT2 but it does not allow deep setting. I want to set spacing like photoshop. For example;

Spacing Example

In the picture is three different spacing format. How to set spacing like 1,2,3 in the picture?

2

Answers


  1. Line Spacing

    You can send EM_SETPARAFORMAT message to the rich text box control and pass PARAFORMAT2 as lparam. To control line spacing, you should set the PFM_LINESPACING flag in the dwMask member and set bLineSpacingRule and dyLineSpacing members of PARAFORMAT2 to suitable values based on your requirements.

    Since you need fine-tuning for line spacing, it seems 4 is suitable for bLineSpacingRule and then you can set dyLineSpacing to any value in twip unit. For more information about available options for bLineSpacingRule, read PARAFORMAT2 documentations.

    public void SetSelectionLineSpacing(byte bLineSpacingRule, int dyLineSpacing)
    {
        PARAFORMAT2 format = new PARAFORMAT2();
        format.cbSize = Marshal.SizeOf(format);
        format.dwMask = PFM_LINESPACING;
        format.dyLineSpacing = dyLineSpacing;
        format.bLineSpacingRule = bLineSpacingRule;
        SendMessage(this.Handle, EM_SETPARAFORMAT, SCF_SELECTION, ref format);
    }
    

    Character Spacing

    Based on documentation of sSpacing in CHARFORMAT2, setting character spacing has no effect on the text displayed by a rich edit control.

    Code

    public class ExRichText : RichTextBox
    {
        [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, Int32 msg, 
                                                 Int32 wParam, ref PARAFORMAT2 lParam);
    
        private const int SCF_SELECTION = 1;
        public const int PFM_LINESPACING = 256;
        public const int EM_SETPARAFORMAT = 1095;
    
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct PARAFORMAT2
        {
            public int cbSize;
            public uint dwMask;
            public Int16 wNumbering;
            public Int16 wReserved;
            public int dxStartIndent;
            public int dxRightIndent;
            public int dxOffset;
            public Int16 wAlignment;
            public Int16 cTabCount;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public int[] rgxTabs;
            public int dySpaceBefore;
            public int dySpaceAfter;
            public int dyLineSpacing;
            public Int16 sStyle;
            public byte bLineSpacingRule;
            public byte bOutlineLevel;
            public Int16 wShadingWeight;
            public Int16 wShadingStyle;
            public Int16 wNumberingStart;
            public Int16 wNumberingStyle;
            public Int16 wNumberingTab;
            public Int16 wBorderSpace;
            public Int16 wBorderWidth;
            public Int16 wBorders;
        }
    
        public void SetSelectionLineSpacing(byte bLineSpacingRule, int dyLineSpacing)
        {
            PARAFORMAT2 format = new PARAFORMAT2();
            format.cbSize = Marshal.SizeOf(format);
            format.dwMask = PFM_LINESPACING;
            format.dyLineSpacing = dyLineSpacing;
            format.bLineSpacingRule = bLineSpacingRule;
            SendMessage(this.Handle, EM_SETPARAFORMAT, SCF_SELECTION, ref format);
        }
    }
    
    Login or Signup to reply.
  2. Here linespaces are lower than 0. You can adjust de "sl-" parameter, by example "sl-440", the FontFamily, the "em-size" in pts (14), and other text parameters. You can also, evidently, complete the rtf code, but the RichTextBox should do it for you.

    using System;
    using System.Windows;
    using System.Windows.Forms;
    using System.Drawing;
    namespace Dartagnan{
    public class Linespace{
    public static void Main(){
    var form = new Form();
    var rtb = new RichTextBox();
    rtb.Rtf = @"{rtf1sl-180";
    rtb.Font = new System.Drawing.Font(new FontFamily("Arial"), 14,FontStyle.Regular, GraphicsUnit.Point);
    rtb.Dock = DockStyle.Fill;
    form.Controls.Add(rtb);
    form.ShowDialog();
    }
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search