简体   繁体   中英

WinForm richtextbox deep line spacing and character spacing

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;

间距示例

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

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM