另一个实现。
这个仅在单词之间插入“Hair Spaces”。
编辑:
添加了一个实现段落块对齐的方法。
JustifyParagraph() 和 JustifyLine() 都调用了 worker 方法 Justify()。
label1.Text = JustifyParagraph(label1.Text, label1.Font, label1.ClientSize.Width);
public string JustifyParagraph(string text, Font font, int ControlWidth)
{
string result = string.Empty;
List<string> ParagraphsList = new List<string>();
ParagraphsList.AddRange(text.Split(new[] { "\r\n" }, StringSplitOptions.None).ToList());
foreach (string Paragraph in ParagraphsList) {
string line = string.Empty;
int ParagraphWidth = TextRenderer.MeasureText(Paragraph, font).Width;
if (ParagraphWidth > ControlWidth) {
//Get all paragraph words, add a normal space and calculate when their sum exceeds the constraints
string[] Words = Paragraph.Split(' ');
line = Words[0] + (char)32;
for (int x = 1; x < Words.Length; x++) {
string tmpLine = line + (Words[x] + (char)32);
if (TextRenderer.MeasureText(tmpLine, font).Width > ControlWidth)
{
//Max lenght reached. Justify the line and step back
result += Justify(line.TrimEnd(), font, ControlWidth) + "\r\n";
line = string.Empty;
--x;
} else {
//Some capacity still left
line += (Words[x] + (char)32);
}
}
//Adds the remainder if any
if (line.Length > 0)
result += line + "\r\n";
}
else {
result += Paragraph + "\r\n";
}
}
return result.TrimEnd(new[]{ '\r', '\n' });
}
JustifyLines() 只处理单行文本:(比客户区短)
textBox1.Text = JustifyLines(textBox1.Text, textBox1.Font, textBox1.ClientSize.Width);
public string JustifyLines(string text, Font font, int ControlWidth)
{
string result = string.Empty;
List<string> Paragraphs = new List<string>();
Paragraphs.AddRange(text.Split(new[] { "\r\n" }, StringSplitOptions.None).ToList());
//Justify each paragraph and re-insert a linefeed
foreach (string Paragraph in Paragraphs) {
result += Justify(Paragraph, font, ControlWidth) + "\r\n";
}
return result.TrimEnd(new[] {'\r', '\n'});
}
worker方法:
private string Justify(string text, Font font, int width)
{
char SpaceChar = (char)0x200A;
List<string> WordsList = text.Split((char)32).ToList();
if (WordsList.Capacity < 2)
return text;
int NumberOfWords = WordsList.Capacity - 1;
int WordsWidth = TextRenderer.MeasureText(text.Replace(" ", ""), font).Width;
int SpaceCharWidth = TextRenderer.MeasureText(WordsList[0] + SpaceChar, font).Width
- TextRenderer.MeasureText(WordsList[0], font).Width;
//Calculate the average spacing between each word minus the last one
int AverageSpace = ((width - WordsWidth) / NumberOfWords) / SpaceCharWidth;
float AdjustSpace = (width - (WordsWidth + (AverageSpace * NumberOfWords * SpaceCharWidth)));
//Add spaces to all words
return ((Func<string>)(() => {
string Spaces = "";
string AdjustedWords = "";
for (int h = 0; h < AverageSpace; h++)
Spaces += SpaceChar;
foreach (string Word in WordsList) {
AdjustedWords += Word + Spaces;
//Adjust the spacing if there's a reminder
if (AdjustSpace > 0) {
AdjustedWords += SpaceChar;
AdjustSpace -= SpaceCharWidth;
}
}
return AdjustedWords.TrimEnd();
}))();
}
关于 RichTextBox。
@TaW 说它不支持块对齐,但这并不完全正确。
众所周知,RichTextBox 基于 RichEdit 类,并且该类支持“Justification”。
这是在旧的 Platform SDK 中报告的(带有示例)。
RichTextBox 的AdvancedTypographicsOption 在句柄创建期间被显式截断。
(这与实现 PARAFORMAT 与 PARAFORMAT2 结构无关,这无关紧要,这是故意的)。
所以这是对可怜的 RichTextBox 的“治疗”。
派生自它并使用 SendMessage 向基类发送 EM_SETTYPOGRAPHYOPTIONS 消息的类,指定 TO_ADVANCEDTYPOGRAPHY 以重新启用 Justification。
它还会隐藏SelectionAlignment,以添加缺少的Justify 选项。
这适用于段落级别或从某个点开始。
public class JustifiedRichTextBox : RichTextBox
{
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, [In] [Out] ref PARAFORMAT2 pf);
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
public enum TextAlignment
{
Left = 1,
Right,
Center,
Justify
}
private const int EM_SETEVENTMASK = 1073;
private const int EM_GETPARAFORMAT = 1085;
private const int EM_SETPARAFORMAT = 1095;
private const int EM_SETTYPOGRAPHYOPTIONS = 1226;
private const int TO_ADVANCEDTYPOGRAPHY = 0x1;
private const int WM_SETREDRAW = 11;
private const int PFM_ALIGNMENT = 8;
private const int SCF_SELECTION = 1;
[StructLayout(LayoutKind.Sequential)]
private struct PARAFORMAT2
{
//----------------------------------------
public int cbSize; // PARAFORMAT
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; // PARAFORMAT2
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;
}
private int updating = 0;
private int oldEventMask = 0;
public new TextAlignment SelectionAlignment
{
// SelectionAlignment is not overridable
get
{
PARAFORMAT2 pf = new PARAFORMAT2();
pf.cbSize = Marshal.SizeOf(pf);
SendMessage(this.Handle, EM_GETPARAFORMAT, SCF_SELECTION, ref pf);
if ((pf.dwMask & PFM_ALIGNMENT) == 0) return TextAlignment.Left;
return (TextAlignment)pf.wAlignment;
}
set
{
PARAFORMAT2 pf = new PARAFORMAT2();
pf.cbSize = Marshal.SizeOf(pf);
pf.dwMask = PFM_ALIGNMENT;
pf.wAlignment = (short)value;
SendMessage(this.Handle, EM_SETPARAFORMAT, SCF_SELECTION, ref pf);
}
}
//Overrides OnHandleCreated to enable RTB advances options
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
// EM_SETTYPOGRAPHYOPTIONS allows to enable RTB (RichEdit) Advanced Typography
SendMessage(this.Handle, EM_SETTYPOGRAPHYOPTIONS, TO_ADVANCEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY);
}
} //JustifiedRichTextBox