最近用到richtextbox,一些操作记录在下面:
1 禁止自动换行
richtextbox1.WordWrap = false;
wordwrap 属性:指示多行文本控件是否在必要时自动换到下一行的开始处;
2 调整字体大小
richtextbox.Font = new System.Drawing.Font(FontFamily.GenericMonospace, 10);
3 调整行间距
先写一个类:
class CSetLineSpace
{
public const int WM_USER = 0x0400;
public const int EM_GETPARAFORMAT = WM_USER + 61;
public const int EM_SETPARAFORMAT = WM_USER + 71;
public const long MAX_TAB_STOPS = 32;
public const uint PFM_LINESPACING = 0x00000100;
[StructLayout(LayoutKind.Sequential)]
private struct PARAFORMAT2
{
public int cbSize;
public uint dwMask;
public short wNumbering;
public short wReserved;
public int dxStartIndent;
public int dxRightIndent;
public int dxOffset;
public short wAlignment;
public short cTabCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public int[] rgxTabs;
public int dySpaceBefore;
public int dySpaceAfter;
public int dyLineSpacing;
public short sStyle;
public byte bLineSpacingRule;
public byte bOutlineLevel;
public short wShadingWeight;
public short wShadingStyle;
public short wNumberingStart;
public short wNumberingStyle;
public short wNumberingTab;
public short wBorderSpace;
public short wBorderWidth;
public short wBorders;
}
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, ref PARAFORMAT2 lParam);
/// <summary>
/// 设置行距
/// </summary>
/// <param name="ctl">控件</param>
/// <param name="dyLineSpacing">间距</param>
public static void SetLineSpace(Control ctl, int dyLineSpacing)
{
PARAFORMAT2 fmt = new PARAFORMAT2();
fmt.cbSize = Marshal.SizeOf(fmt);
fmt.bLineSpacingRule = 4;// bLineSpacingRule;
fmt.dyLineSpacing = dyLineSpacing;
fmt.dwMask = PFM_LINESPACING;
try
{
SendMessage(new HandleRef(ctl, ctl.Handle), EM_SETPARAFORMAT, 0, ref fmt);
}
catch
{
}
}
}
SetLineSpace(richtextbox1, 300);
第三个参考自网上资源,一个博客,但现在找不到出处了,在这里谢谢他吧!
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。