在RichTextBox中实现段落居中对齐

7
我使用RichTextBox,并希望将段落中的所有行都格式化为两端对齐,只有最后一行居中对齐。
如下所示:
      sssssssssssssssssssssssss
      sssssssssssssssssssssssss
      sssssssssssssssssssssssss
           ssssssssssssss     

我使用这段代码来实现文本两端对齐。


你有没有找到解决办法?今天。 - Yisroel M. Olewski
2个回答

3
你需要的是“居中对齐”。修改枚举,它在下面的#5中:
/// <summary>
/// Specifies how text in a <see cref="AdvRichTextBox"/> is
/// horizontally aligned.
/// </summary>
public enum TextAlign
{
    /// <summary>
    /// The text is aligned to the left.
    /// </summary>
    Left = 1,

    /// <summary>
    /// The text is aligned to the right.
    /// </summary>
    Right = 2,

    /// <summary>
    /// The text is aligned in the center.
    /// </summary>
    Center = 3,

    /// <summary>
    /// The text is justified.
    /// </summary>
    Justify = 4,

    /// <summary>
    /// The text is center justified.
    /// </summary>
    CenterJustify = 5
}

在此输入图片描述

示例代码:

private void Form1_Load(object sender, EventArgs e)
{
    AdvRichTextBox tb = new AdvRichTextBox();

    tb.SelectionAlignment = TextAlign.CenterJustify;
    tb.SelectedText = "Here is a justified paragraph. It will show up justified using the new AdvRichTextBox control created by who knows.\n";

    tb.Width = 250;
    tb.Height = 450;

    this.Controls.Add(tb);
}

1
+1 这可能是正确的答案 :) (我承认我在理解 OP 的目标方面遇到了很多困难...) - digEmAll
首先,感谢您。其次,在我的电脑上,它将整个段落格式化为居中对齐。这是为什么呢?(注:困难是因为我不太懂英语) - user1543998
@user1543998 - 应该是“居中对齐”,这样整个段落既居中又对齐。如果您没有看到“对齐”部分,我不确定。确保将“CenterJustify”设置为5。FYI...我正在使用Windows 7 / Visual Studio 2010。 - Chris Gessler
@user1543998 - 同时,尝试将字体更改为 Courier New 进行测试。不同的字体字符宽度不同,如果您看到我的示例,第二行两侧有一点点白色空间。它是对齐的,但也是居中的。你将无法实现“左对齐”和“居中对齐”相结合。 - Chris Gessler
是的,我正在将“CenterJustify”设置为5。我正在使用VS 2010 Express和Windows XP进行工作。我看到了你的示例,非常好,但在我的电脑上它会居中对齐。我尝试更改字体,但没有帮助。 - user1543998
@user1543998 - 可能是Windows XP。也许在XP中,“居中对齐”被定义为其他内容。尝试使用6、7、8等数字来查看其反应如何。 - Chris Gessler

0
<RichTextBox>
            <FlowDocument>
                <Paragraph TextAlignment="Justify">
                    sssssssssssssssssssssssss
                    sssssssssssssssssssssssss
                    sssssssssssssssssssssssss
                </Paragraph>
                <Paragraph TextAlignment="Center">
                    sssssssssssssssssssssssss
                </Paragraph>
            </FlowDocument>
</RichTextBox>

主要使用段落文本对齐功能,您可以选择以下对齐方式:1. 两端对齐,2. 居中对齐,3. 左对齐,4. 右对齐;


1
感谢您的建议,Robert Columbia。 - Palash Roy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接