Při práci na jednom softwarovém projektu jsem zjistil, že potřebuji RichTextBox v C# WinForms, který umí mezery mezi odstavci. Bohužel všechna fóra, která jsem navštívil, říkala, že je to “nemožné” nebo “příliš obtížné”. No … zdá se, že existuje mnoho skrytých schopností, které RichTextBox neodhaluje. Po chvilce kutilské práce a pokusů jsem skončil s řešením, které není ani moc složité. Kompletní ovládací prvek je ve formě zdrojového kódu tady dole. Happy coding!
Ovládací prvek
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ExtendedRTF
{
class RichTextBoxWithParagraphSpacing : RichTextBox
{
private const int PFM_SPACEBEFORE = 64;
private const int PFM_SPACEAFTER = 128;
private const int EM_SETPARAFORMAT = 1095;
private const int SCF_SELECTION = 1;
public int SelectionParagraphSpacingAfter
{
set
{
PARAFORMAT fmt = new PARAFORMAT();
fmt.cbSize = Marshal.SizeOf(fmt);
fmt.dwMask = PFM_SPACEAFTER;
fmt.dySpaceAfter = value;
SendMessage(new HandleRef(this, this.Handle),
EM_SETPARAFORMAT,
SCF_SELECTION,
ref fmt
);
}
}
public int SelectionParagraphSpacingBefore
{
set
{
PARAFORMAT fmt = new PARAFORMAT();
fmt.cbSize = Marshal.SizeOf(fmt);
fmt.dwMask = PFM_SPACEBEFORE;
fmt.dySpaceBefore = value;
SendMessage(new HandleRef(this, this.Handle),
EM_SETPARAFORMAT,
SCF_SELECTION,
ref fmt
);
}
}
[StructLayout(LayoutKind.Sequential)]
private struct PARAFORMAT
{
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;
// PARAFORMAT2 from here onwards.
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 int SendMessage(HandleRef hWnd,
int msg,
int wParam,
ref PARAFORMAT lp);
}
}
|
Jak to použít ve vašem projektu
Tip: aby byly mezery opravdu vidět, bude potřeba nastavit hodnoty v řádu stovek, např. 250.
1
2
3
4
5
6
7
8
9
|
RichTextBoxWithParagraphSpacing rtb = new RichTextBoxWithParagraphSpacing();
rtb.Location = new Point(0, 0);
rtb.Text = "First paragraph\r\nSecond paragraph";
rtb.SelectionParagraphSpacingBefore = 100;
rtb.SelectionParagraphSpacingAfter = 300;
this.Controls.Add(rtb);
|
Pár odkazů k tématu: