2013-04-30 60 views
6
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace abc 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    EmoticonRender ab = new EmoticonRender(); 
    private void button1_Click(object sender, EventArgs e) 
    { 
     string textie = ab.Parse(textBox1.Text); 
     richTextBox1.Text += textie+"\n"; 
    } 
} 
public class EmoticonRender 
{ 

    private List<KeyValuePair<string, string>> _dictionary = new List<KeyValuePair<string, string>>() 
    { 
    new KeyValuePair<string, string>(":-)", "a.png"), 
    new KeyValuePair<string, string>(";-(", "a.png"), 
    }; 

    public string Parse(string text) 
    { 
    foreach(KeyValuePair<string, string> kvp in _dictionary) 
    { 
    text = text.Replace(kvp.Key, @"C:\Users\Buddiez\Documents\Visual Studio 2010\Projects\abc\abc\a.png"); 
    } 
    return text; 
    } 

} 

}如何添加表情符號使用這些線路的代碼插入到smilyes RichTextBox的到的RichTextBox

IM但不是表示smileye它顯示的PNG imgae即路徑。 C:\ Users \ Buddiez \ Documents \ Visual Studio 2010 \ Projects \ abc \ abc \ a.png

+0

可能的重複[如何將圖像插入RichTextBox?](http://stackoverflow.com/questions/542850/how-can-i-insert-an-image-into-a-richtextbox) – 2013-05-13 08:57:28

+0

我喜歡那個鍵:) – nawfal 2013-06-18 10:18:38

+0

任何運氣兄弟? – zzlalani 2013-08-01 07:55:32

回答

1

複製您擁有的所有圖像並導航到>> visual studio選擇Project >> Properties.There選擇Resources將所有複製的圖像粘貼到右側窗格上。

Hashtable emotions; 
    void CreateEmotions() 
    { 
     emotions= new Hashtable(6); 
     emotions.Add(":-)", Project.Properties.Resources.regular_smile); 
     emotions.Add(":)", Project.Properties.Resources.regular_smile); 

    } 

    void AddEmotions() 
    { 
     foreach (string emote in emotions.Keys) 
     { 
      while(richTextBox1.Text.Contains(emote)) 
      { 
       int ind = richTextBox1.Text.IndexOf(emote); 
       richTextBox1.Select(ind, emote.Length); 
       Clipboard.SetImage((Image)emotions[emote]); 
       richTextBox1.Paste(); 
      } 
     } 
    } 
+0

後搜索堆棧溢出和其他網站這是最好的解決方案。效果很好。謝謝。 – jonathana 2017-05-31 05:08:32

1

一個好的(也是相對較新的)解決方案可以使用開源的EmojiBox項目。

沒有太多的代碼有那麼這是很容易做到,只要注意,爲了插入表情符號到自定義的RichTextBox那裏,你鍵入的文本必須遵循的模板:emoji_name:

當然,如果你不想使用整個unicode emojis列表,你也可以在json文件中替換圖像文件或它們的名稱/描述。