如何将表情符号(笑脸)添加到WPF RichText框中 [英] how to add emoticons(smileys) into WPF richtext box

查看:470
本文介绍了如何将表情符号(笑脸)添加到WPF RichText框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有输入文本框和输出富文本框的WPF IM聊天窗口.我想在RTF文本框上呈现输入文本.当用户在文本块中输入带有:)之类的笑脸符号时,我想用预定义的笑脸图像替换该文本笑脸,并在RichText框上进行渲染.这与gtalk聊天窗口的行为非常相似.

I have a WPF IM chat window with a input textbox and a output richtext box. I want to render input text on the richtext box. When user enter a smiley symbol like :) into the text block with some texts, I want to replace that text smiley with predefined smiley image and render on the richtext box. It's very similar to gtalk chat window behavior.

我该怎么做? 在此先感谢:-)

How can i do this? thanks in advance :-)

推荐答案

您可以在下面使用此表情符号功能:

You can use this Emoticons function below:

    #region add emotion to RichTextBox function

    private Dictionary<string, string> _mappings = new Dictionary<string, string>();

    private string GetEmoticonText(string text)
    {
        string match = string.Empty;
        int lowestPosition = text.Length;

        foreach (KeyValuePair<string, string> pair in _mappings)
        {
            if (text.Contains(pair.Key))
            {
                int newPosition = text.IndexOf(pair.Key);
                if (newPosition < lowestPosition)
                {
                    match = pair.Key;
                    lowestPosition = newPosition;
                }
            }
        }

        return match;

    }
    // And also function which add smiles in richtextbox, here is it:

    private void Emoticons(string msg,Paragraph para )
    {
        //try
        //{


       // Paragraph para = new Paragraph { LineHeight = 1 };

        Run 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];

                Image image = new Image
                {
                    Source =
                        new BitmapImage(new System.Uri(Environment.CurrentDirectory+path,
                                                UriKind.RelativeOrAbsolute)),
                    Width = Height = 25,
                };

                //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);

        }
    }

// ///
   private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        _mappings.Add(@"s-]", "/Images/smiley/silly.png");
        _mappings.Add(@":-|", "/Images/smiley/angry.png");
    }

    //Call function to use
     private void SendMessage(object sender,RoutedEventArgs e)
    {

        Paragraph paragraph = new Paragraph();
        paragraph.LineHeight = 1;

        Run name = new Run();
        name.Text =rtbMessage.Text+ " : ";
        name.Foreground = new SolidColorBrush(Colors.Red);
        paragraph.Inlines.Add(new Bold(name));
        //paragraph.Inlines.Add(new Run(name.text));
        rtbConversation.Document.Blocks.Add(paragraph);
        Emoticons(name.Text, paragraph);
        rtbConversation.ScrollToEnd();

    }

这篇关于如何将表情符号(笑脸)添加到WPF RichText框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆