WinForm richtextbox深行间距和字符间距

时间:2016-02-20 17:56:16

标签: c# winforms richtextbox

如何在winform中编辑richtextbox上的行间距和字符间距?我尝试过PARAFORMAT2,但它不允许深度设置。我想像photoshop一样设置间距。例如;

Spacing Example

图中是三种不同的间距格式。如何在图片中设置1,2,3之间的间距?

1 个答案:

答案 0 :(得分:2)

行间距

您可以将EM_SETPARAFORMAT邮件发送到富文本框控件,并将PARAFORMAT2作为lparam传递。要控制行间距,您应在PFM_LINESPACING成员中设置dwMask标记,并根据您的要求将bLineSpacingRuledyLineSpacing PARAFORMAT2成员设置为合适的值

由于您需要对行间距进行微调,因此似乎4适用于bLineSpacingRule,然后您可以将dyLineSpacing设置为twip单位中的任何值。有关bLineSpacingRule的可用选项的详细信息,请阅读PARAFORMAT2文档。

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

字符间距

根据CHARFORMAT2sSpacing的文档,设置字符间距对丰富的编辑控件显示的文本没有影响。

<强>代码

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