如何将表情符号(笑脸)添加到WPF富文本框中

发布于 2024-09-18 12:42:15 字数 158 浏览 5 评论 0原文

我有一个带有输入文本框和输出富文本框的 WPF IM 聊天窗口。我想在富文本框中呈现输入文本。当用户在包含某些文本的文本块中输入像 :) 这样的笑脸符号时,我想用预定义的笑脸图像替换该文本笑脸并在富文本框中呈现。它与 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 :-)

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

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

发布评论

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

评论(3

月亮是我掰弯的 2024-09-25 12:42:15

这会是这样的

显示带有内嵌图像的文本

并使用文件后面的代码你可以做同样的事情

    //Create a new RichTextBox.
    RichTextBox MyRTB = new RichTextBox();

    // Create a Run of plain text and image.
    Run myRun = new Run();
    myRun.Text = "Displaying text with inline image";
    Image MyImage = new Image();
    MyImage.Source = new BitmapImage(new Uri("flower.jpg", UriKind.RelativeOrAbsolute));
    MyImage.Height = 50;
    MyImage.Width = 50;
    InlineUIContainer MyUI = new InlineUIContainer();
    MyUI.Child = MyImage;

    // Create a paragraph and add the paragraph to the RichTextBox.
    Paragraph myParagraph = new Paragraph();
    MyRTB.Blocks.Add(myParagraph);

    // Add the Run and image to it.
    myParagraph.Inlines.Add(myRun);
    myParagraph.Inlines.Add(MyUI);

    //Add the RichTextBox to the StackPanel.
    MySP.Children.Add(MyRTB);

This would be like this

Displaying text with inline image

and using code behind file you can do the same thing

    //Create a new RichTextBox.
    RichTextBox MyRTB = new RichTextBox();

    // Create a Run of plain text and image.
    Run myRun = new Run();
    myRun.Text = "Displaying text with inline image";
    Image MyImage = new Image();
    MyImage.Source = new BitmapImage(new Uri("flower.jpg", UriKind.RelativeOrAbsolute));
    MyImage.Height = 50;
    MyImage.Width = 50;
    InlineUIContainer MyUI = new InlineUIContainer();
    MyUI.Child = MyImage;

    // Create a paragraph and add the paragraph to the RichTextBox.
    Paragraph myParagraph = new Paragraph();
    MyRTB.Blocks.Add(myParagraph);

    // Add the Run and image to it.
    myParagraph.Inlines.Add(myRun);
    myParagraph.Inlines.Add(MyUI);

    //Add the RichTextBox to the StackPanel.
    MySP.Children.Add(MyRTB);
一个人练习一个人 2024-09-25 12:42:15

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

    #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();

    }

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

    }
葵雨 2024-09-25 12:42:15

这并不容易。一般方法是监视富文本框输入,查找所有表情符号并将其替换为图像:在 Runs 中发现一个微笑,然后用 Runsbreak Run 进入 Span代码>和<代码>图像。例如

  <RichTextBox>
    <FlowDocument>
     <Paragraph>
      <!-- Before -->
      <Run>Hello :) world!</Run>
      <!-- After -->
      <Span>
         <Run Text="Hello"/>
         <Image Width="16" Source="http://kolobok.us/smiles/light_skin/smile.gif"/>
         <Run Text=" world"/>
       </Span>
     </Paragraph>
    </FlowDocument>
 </RichTextBox>

希望这有帮助。

That's not easy. General approach would be to monitor rich text box input, find all emoticons and replace them with images: break Run where you found a smile into Span with Runs and Images. E.g.

  <RichTextBox>
    <FlowDocument>
     <Paragraph>
      <!-- Before -->
      <Run>Hello :) world!</Run>
      <!-- After -->
      <Span>
         <Run Text="Hello"/>
         <Image Width="16" Source="http://kolobok.us/smiles/light_skin/smile.gif"/>
         <Run Text=" world"/>
       </Span>
     </Paragraph>
    </FlowDocument>
 </RichTextBox>

Hope this helps.

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