Files
CommonLib/CommonLib/Library/Windows/Forms/FormControlsFormatting.cs
2025-03-13 18:15:01 -07:00

141 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
namespace CommonLib.Windows.Forms
{
///==========================================================================
/// TextFormatting
///==========================================================================
/// <summary>
/// Primary purpose is to define the format of text such as text font and color
/// mainly for display in rich text box
/// </summary>
///==========================================================================
/// Date Programmer Proj.ID SAR REVISION HISTORY:
/// --/--/-- ------------ -------- ---- --------------------------
/// 06/06/12 D.Le
///==========================================================================
public class TextFormat
{
// this struct define the text formatting of a selected number of words in a string
// must define word index and word count, word index starts at 0
public struct TextFontAndColor
{
public int wordIndex;
public int wordCount;
public Font textFont;
public Color textColor;
public TextFontAndColor(Font fontProp, Color fontColor, int textWordIndex = -1, int textWordCount = -1)
{
wordIndex = textWordIndex;
wordCount = textWordCount;
textFont = fontProp;
textColor = fontColor;
}
public TextFontAndColor(string nothing)
{
wordIndex = -1;
wordCount = -1;
textFont = new Font("Microsoft Sans Serif", 9, FontStyle.Regular);
textColor = Color.Black;
}
}
}
///==========================================================================
/// FormatRichTextBox
///==========================================================================
/// <summary>
/// Format rich text box
/// </summary>
///==========================================================================
/// Date Programmer Proj.ID SAR REVISION HISTORY:
/// --/--/-- ------------ -------- ---- --------------------------
/// 06/06/12 D.Le
///==========================================================================
public class FormatRichTextBox
{
[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;
///==========================================================================
/// FormatRichTextBox.setLineFormat
///==========================================================================
/// <summary>
/// Allow the setting of line spacing in RichTextBox
///
/// For rule, there are only 6 possible values, 0-5
/// 0 - Single Spacing, line spacing is ignored
/// 1 - One-and-a-half spacing. line spacing is ignored
/// 2 - Double spacing. line spacing is ignored
/// 3 - Line spacing specifies spacing from one line to the next
/// 4 - Line spacing specifies spacing from one line to the next
/// 5 - Line spacing/20 is the spacing from one line to the next
/// </summary>
/// <param name="rTextbox">rich text box control</param>
/// <param name="rule">can only be 0-5 (the summary describes each rule)</param>
/// <param name="space">amount of spacing between 2 lines</param>
///==========================================================================
/// Date Programmer Proj.ID SAR REVISION HISTORY:
/// --/--/-- ------------ -------- ---- -----------------------------
/// 06/06/12 D.Le
///==========================================================================
static public void setLineFormat(RichTextBox rTextbox, byte rule, int space)
{
PARAFORMAT fmt = new PARAFORMAT();
fmt.cbSize = Marshal.SizeOf(fmt);
fmt.dwMask = PFM_LINESPACING;
fmt.dyLineSpacing = space;
fmt.bLineSpacingRule = rule;
rTextbox.SelectAll();
SendMessage(new HandleRef(rTextbox, rTextbox.Handle), EM_SETPARAFORMAT, SCF_SELECTION, ref fmt);
}
[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;
// 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;
}
}
}