Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
736 views
in Technique[技术] by (71.8m points)

c# - 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;

Spacing Example

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...