I cooked up a small code sample that demonstrates how to easily change the line spacing of text in a
RichTextBox
control. This example was written by me as a solution to a
question in Q&A[
^]. To use the below code, you have to make a WinForm project and add six buttons, a
RichTextBox
control and normal
TextBox
to it.
Inside the structure
PARAFORMAT
, there are two relevant fields that play closely together:
bLineSpacingRule
defines how
dyLineSpacing
is supposed to be interpreted.
- 0
- Single spacing. The
dyLineSpacing
member is ignored. - 1
- One-and-a-half spacing. The
dyLineSpacing
member is ignored. - 2
- Double spacing. The
dyLineSpacing
member is ignored. - 3
- The
dyLineSpacing
member specifies the spacing from one line to the next, in twips. However, if dyLineSpacing
specifies a value that is less than single spacing, the control displays single-spaced text. - 4
- The
dyLineSpacing
member specifies the spacing from one line to the next, in twips. The control uses the exact spacing specified, even if dyLineSpacing
specifies a value that is less than single spacing. - 5
- The value of
dyLineSpacing
/ 20 is the spacing, in lines, from one line to the next. Thus, setting dyLineSpacing
to 20 produces single-spaced text, 40 is double spaced, 60 is triple spaced, and so on.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace RichTextBoxLineSpacing
{
public partial class Form1 : Form
{
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, ref PARAFORMAT lParam);
const int PFM_SPACEBEFORE = 0x00000040;
const int PFM_SPACEAFTER = 0x00000080;
const int PFM_LINESPACING = 0x00000100;
const int SCF_SELECTION = 1;
const int EM_SETPARAFORMAT = 1095;
public Form1()
{
InitializeComponent();
}
private void setLineFormat(byte rule, int space)
{
PARAFORMAT fmt = new PARAFORMAT();
fmt.cbSize = Marshal.SizeOf(fmt);
fmt.dwMask = PFM_LINESPACING;
fmt.dyLineSpacing = space;
fmt.bLineSpacingRule = rule;
richTextBox1.SelectAll();
SendMessage( new HandleRef( richTextBox1, richTextBox1.Handle ),
EM_SETPARAFORMAT,
SCF_SELECTION,
ref fmt
);
}
private void button1_Click(object sender, EventArgs e)
{
setLineFormat(0, space);
}
private void button2_Click(object sender, EventArgs e)
{
setLineFormat(1, space);
}
private void button3_Click(object sender, EventArgs e)
{
setLineFormat(2, space);
}
private void button4_Click(object sender, EventArgs e)
{
setLineFormat(3, space);
}
private void button5_Click(object sender, EventArgs e)
{
setLineFormat(4, space);
}
private void button6_Click(object sender, EventArgs e)
{
setLineFormat(5, space);
}
int space = 0;
private void textBox1_TextChanged(object sender, EventArgs e)
{
String val = textBox1.Text;
bool success = int.TryParse(val, out space);
if (success)
{
textBox1.BackColor = Color.White;
}
else
{
textBox1.BackColor = Color.Red;
}
}
}
[StructLayout( LayoutKind.Sequential )]
public 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;
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;
}
}