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);
}
}
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();
}
}
}
2条答案
按热度按时间vmdwslir1#
线间距
您可以将
EM_SETPARAFORMAT
消息发送到富文本框控件,并将PARAFORMAT2
作为lparam
传递。要控制行距,应在dwMask
成员中设置PFM_LINESPACING
标志,并根据需要将PARAFORMAT2
的bLineSpacingRule
和dyLineSpacing
成员设置为合适的值。由于您需要微调行间距,因此似乎4适合
bLineSpacingRule
,然后您可以将dyLineSpacing
设置为以twip为单位的任何值。有关bLineSpacingRule
可用选项的详细信息,请阅读PARAFORMAT2
文档。字符间距
根据
CHARFORMAT2
中sSpacing
的文档,设置字符间距对富编辑控件显示的文本没有影响。编码
bjg7j2ky2#
这里的线空间小于0。您可以调整de“sl-”参数,例如“sl-440”、FontFamily、pts(14)中的“em-size”和其他文本参数。显然,您也可以完成rtf代码,但RichTextBox应该为您完成。