winforms WinForm richtextbox深行间距和字符间距

jxct1oxe  于 2023-05-01  发布在  其他
关注(0)|答案(2)|浏览(291)

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

在图片中有三种不同的间距格式。如何在图片中设置像1,2,3这样的间距?

vmdwslir

vmdwslir1#

线间距

您可以将EM_SETPARAFORMAT消息发送到富文本框控件,并将PARAFORMAT2作为lparam传递。要控制行距,应在dwMask成员中设置PFM_LINESPACING标志,并根据需要将PARAFORMAT2bLineSpacingRuledyLineSpacing成员设置为合适的值。
由于您需要微调行间距,因此似乎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);
    }
}
bjg7j2ky

bjg7j2ky2#

这里的线空间小于0。您可以调整de“sl-”参数,例如“sl-440”、FontFamily、pts(14)中的“em-size”和其他文本参数。显然,您也可以完成rtf代码,但RichTextBox应该为您完成。

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 = @"{\rtf1\sl-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();
}
}
}

相关问题