RichTextbox虽然内置的功能已经很强大了,但是还是有些功能没有提供,就比如“行间距”,不知道其内置的功能是否可以实现,这里在网上找到一个使用win32来设置的
1
public
const
int
WM_USER
=
0x0400
;
2 public const int EM_GETPARAFORMAT = WM_USER + 61 ;
3 public const int EM_SETPARAFORMAT = WM_USER + 71 ;
4 public const long MAX_TAB_STOPS = 32 ;
5 public const uint PFM_LINESPACING = 0x00000100 ;
6 [StructLayout(LayoutKind.Sequential)]
7 private struct PARAFORMAT2
8 {
9 public int cbSize;
10 public uint dwMask;
11 public short wNumbering;
12 public short wReserved;
13 public int dxStartIndent;
14 public int dxRightIndent;
15 public int dxOffset;
16 public short wAlignment;
17 public short cTabCount;
18 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32 )]
19 public int [] rgxTabs;
20 public int dySpaceBefore;
21 public int dySpaceAfter;
22 public int dyLineSpacing;
23 public short sStyle;
24 public byte bLineSpacingRule;
25 public byte bOutlineLevel;
26 public short wShadingWeight;
27 public short wShadingStyle;
28 public short wNumberingStart;
29 public short wNumberingStyle;
30 public short wNumberingTab;
31 public short wBorderSpace;
32 public short wBorderWidth;
33 public short wBorders;
34 }
35
36 [DllImport( " user32 " , CharSet = CharSet.Auto)]
37 private static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, ref PARAFORMAT2 lParam);
38
39 /// <summary>
40 /// 设置行距
41 /// </summary>
42 /// <param name="ctl"> 控件 </param>
43 /// <param name="dyLineSpacing"> 间距 </param>
44 public static void SetLineSpace(Control ctl, int dyLineSpacing)
45 {
46 PARAFORMAT2 fmt = new PARAFORMAT2();
47 fmt.cbSize = Marshal.SizeOf(fmt);
48 fmt.bLineSpacingRule = 4 ; // bLineSpacingRule;
49 fmt.dyLineSpacing = dyLineSpacing;
50 fmt.dwMask = PFM_LINESPACING;
51 try
52 {
53 SendMessage( new HandleRef(ctl, ctl.Handle), EM_SETPARAFORMAT, 0 , ref fmt);
54 }
55 catch
56 {
57
58 }
59 }
2 public const int EM_GETPARAFORMAT = WM_USER + 61 ;
3 public const int EM_SETPARAFORMAT = WM_USER + 71 ;
4 public const long MAX_TAB_STOPS = 32 ;
5 public const uint PFM_LINESPACING = 0x00000100 ;
6 [StructLayout(LayoutKind.Sequential)]
7 private struct PARAFORMAT2
8 {
9 public int cbSize;
10 public uint dwMask;
11 public short wNumbering;
12 public short wReserved;
13 public int dxStartIndent;
14 public int dxRightIndent;
15 public int dxOffset;
16 public short wAlignment;
17 public short cTabCount;
18 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32 )]
19 public int [] rgxTabs;
20 public int dySpaceBefore;
21 public int dySpaceAfter;
22 public int dyLineSpacing;
23 public short sStyle;
24 public byte bLineSpacingRule;
25 public byte bOutlineLevel;
26 public short wShadingWeight;
27 public short wShadingStyle;
28 public short wNumberingStart;
29 public short wNumberingStyle;
30 public short wNumberingTab;
31 public short wBorderSpace;
32 public short wBorderWidth;
33 public short wBorders;
34 }
35
36 [DllImport( " user32 " , CharSet = CharSet.Auto)]
37 private static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, ref PARAFORMAT2 lParam);
38
39 /// <summary>
40 /// 设置行距
41 /// </summary>
42 /// <param name="ctl"> 控件 </param>
43 /// <param name="dyLineSpacing"> 间距 </param>
44 public static void SetLineSpace(Control ctl, int dyLineSpacing)
45 {
46 PARAFORMAT2 fmt = new PARAFORMAT2();
47 fmt.cbSize = Marshal.SizeOf(fmt);
48 fmt.bLineSpacingRule = 4 ; // bLineSpacingRule;
49 fmt.dyLineSpacing = dyLineSpacing;
50 fmt.dwMask = PFM_LINESPACING;
51 try
52 {
53 SendMessage( new HandleRef(ctl, ctl.Handle), EM_SETPARAFORMAT, 0 , ref fmt);
54 }
55 catch
56 {
57
58 }
59 }
然后可以在Form_Load事件里面调用,代码如下:
SetLineSpace(txt_content,
300
);
第一个参数是一个Control类型的比如richtextbox1,第二个就是行间距了,哈哈,很简单吧。