ATE*_*TES 5 c# richtextbox winforms
如何在winform中的richtextbox上编辑行间距和字符间距?我试过 PARAFORMAT2 但它不允许深度设置。我想像photoshop一样设置间距。例如;

图中是三种不同的间距格式。如何设置图片中的1,2,3间距?
行间距
您可以将EM_SETPARAFORMAT消息发送到富文本框控件并作为PARAFORMAT2传递lparam。要控制行间距,您应该PFM_LINESPACING在dwMaskmember中设置标志,并根据您的要求将bLineSpacingRule和dyLineSpacingmembers设置为合适的值。PARAFORMAT2
由于需要对行距进行微调,似乎 4 比较合适bLineSpacingRule,然后您可以设置dyLineSpacing为任何以缇为单位的值。有关可用选项的更多信息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);
}
Run Code Online (Sandbox Code Playgroud)
字符间距
根据sSpacingin的文档CHARFORMAT2,设置字符间距对 Rich Edit 控件显示的文本没有影响。
代码
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);
}
}
Run Code Online (Sandbox Code Playgroud)