Here's some code you can experiment with. The form contains one RichTexBox control and six button plus one TextBox control. Momentarily the whole text is being selected so the spacing affects all lines, but you can change that yourself if you want. The parameters that are of interest here are:
- dyLineSpacing
- bLineSpacingRule
which live in struct PARAFORMAT. Please read the MSDN documentation for a thorough explanation on how these two play together to control linespacing:
http://msdn.microsoft.com/en-us/library/bb787942(v=vs.85).aspx[
^].
Here is the complete Form code (you need to construct the form yourself with the controls I mentioned at the beginning):
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;
}
}
The code was tested in VisualStudio 2010 in a Windows Forms project.
If you have any doubts please leave a comment to this solution.
Happy coding!
-MRB