带图像的 RichTextBox - 行距问题

发布于 2024-10-05 23:48:59 字数 2060 浏览 0 评论 0原文

我有这个问题。我从扩展 WPF 工具包将图像(微笑)添加到 richTextBox 控件中。

在将简单文本转换为带有图像的文本的函数中,我设置了行高 段落和本段落我添加到 richTextBox 块中。就是这样:

private void RpTextToTextWithEmoticons(string msg)
{
    //set line height
    var para = new Paragraph {LineHeight = 40};

    var r = new Run(msg);

    para.Inlines.Add(r);

    string emoticonText = GetEmoticonText(r.Text);

    //if paragraph does not contains smile only add plain text to richtextbox rtb2
    if (string.IsNullOrEmpty(emoticonText))
    {
        RtbConversation.Document.Blocks.Add(para); 
    }
    else
    {
        while (!string.IsNullOrEmpty(emoticonText))
        {

            TextPointer tp = r.ContentStart;

            // keep moving the cursor until we find the emoticon text
            while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(emoticonText))

                tp = tp.GetNextInsertionPosition(LogicalDirection.Forward);

            // select all of the emoticon text
            var tr = new TextRange(tp, tp.GetPositionAtOffset(emoticonText.Length)) { Text = string.Empty };

            //relative path to image smile file
            string path = _mappings[emoticonText];

            var image = new Image
            {
                Source = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute)),
                Width = 30,
                Height = 30,
            };

            //insert smile
            new InlineUIContainer(image, tp);

            if (para != null)
            {
                var endRun = para.Inlines.LastInline as Run;

                if (endRun == null)
                {
                    break;
                }
                else
                {
                    emoticonText = GetEmoticonText(endRun.Text);
                }

            }
        }
        RtbConversation.Document.Blocks.Add(para);
    }
}

但是如果我添加新段落到块中,所有段落都有不同的行高/间距。我需要恒定的行高/各个段落之间的间距,就像在 Skype 中聊天一样。

我的问题你可以在图片上看到: alt text

哪里有问题,我很无奈。感谢您的任何提前。

I have this problem. I add images (smiles) into richTextBox control from Extended WPF Toolkit.

In function on converting simple text to text with images I set line height
of paragraph and this paragraph I add to blocks of richTextBox. Here is it:

private void RpTextToTextWithEmoticons(string msg)
{
    //set line height
    var para = new Paragraph {LineHeight = 40};

    var r = new Run(msg);

    para.Inlines.Add(r);

    string emoticonText = GetEmoticonText(r.Text);

    //if paragraph does not contains smile only add plain text to richtextbox rtb2
    if (string.IsNullOrEmpty(emoticonText))
    {
        RtbConversation.Document.Blocks.Add(para); 
    }
    else
    {
        while (!string.IsNullOrEmpty(emoticonText))
        {

            TextPointer tp = r.ContentStart;

            // keep moving the cursor until we find the emoticon text
            while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(emoticonText))

                tp = tp.GetNextInsertionPosition(LogicalDirection.Forward);

            // select all of the emoticon text
            var tr = new TextRange(tp, tp.GetPositionAtOffset(emoticonText.Length)) { Text = string.Empty };

            //relative path to image smile file
            string path = _mappings[emoticonText];

            var image = new Image
            {
                Source = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute)),
                Width = 30,
                Height = 30,
            };

            //insert smile
            new InlineUIContainer(image, tp);

            if (para != null)
            {
                var endRun = para.Inlines.LastInline as Run;

                if (endRun == null)
                {
                    break;
                }
                else
                {
                    emoticonText = GetEmoticonText(endRun.Text);
                }

            }
        }
        RtbConversation.Document.Blocks.Add(para);
    }
}

But If I add new paragraphs to blocks all paragraphs have various line height/spacing. I need constat line height/spaccing between individual paragraph, something like chat in skype.

My problem you can see on image:
alt text

Where can be problem, I am helpless. Thank for any advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文