作者: Miklogak
查看: 478|回复: 1
打印 上一主题 下一主题

[源码] 【RTF文本框】在 RichTextBox 中自动加载笑脸

[复制链接]
跳转到指定楼层
楼主
Miklogak 发表于 2022-12-25 11:58:36 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
查看: 478|回复: 1
说明:
在 RichTextBox 中自动加载笑脸
在您键入时自动在 RichTextBox 中加载笑脸的简单 WinForm 应用程序我还在下面附上了 .zip 源代码

效果截图:


部分代码:
[C#] 纯文本查看 复制代码
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;

using System.Collections;

namespace SmileyDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //笑脸哈希表
        Hashtable emotions;

        //在下方添加您的笑脸代码
        void CreateEmotions()
        {
            emotions= new Hashtable(6);
            emotions.Add(":-)", SmileyDemo.Properties.Resources.regular_smile);
            emotions.Add(":)", SmileyDemo.Properties.Resources.regular_smile);
            emotions.Add(":-(", SmileyDemo.Properties.Resources.sad_smile);
            emotions.Add(":(", SmileyDemo.Properties.Resources.sad_smile);
            emotions.Add(":-P", SmileyDemo.Properties.Resources.tongue_smile);
            emotions.Add(":P", SmileyDemo.Properties.Resources.tongue_smile);
        }

        //添加到 RichTextBox
        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();
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //加载表情
            CreateEmotions();            
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            //在打字时添加笑脸
            AddEmotions();
        }
    }
}


案例源码:


评分

参与人数 1金钱 +5 收起 理由
乔克斯 + 5 感谢LZ对论坛做出的贡献~

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播
回复 论坛版权

使用道具 举报

沙发
qiaoke_song 发表于 2022-12-25 19:30:41 | 只看该作者
本帖最后由 qiaoke_song 于 2022-12-25 19:35 编辑

RichTextBox这个东西么,主要是写入了图像要读取出来用的。解决的深入一点,参考下。注意下.net4.5以下
winform的,可以优化修改
链接:https://pan.baidu.com/s/1CtVLACvGVQQIktv3TWovhg
提取码:hk0z

wpf的
http://bbs.cskin.net/thread-15573-1-1.html

winform的麻烦一些,要找结构写入才能读取,
        byte[] _FileBytes = File.ReadAllBytes(p_FileFullPath);
        Image _Image = Image.FromStream(new MemoryStream(_FileBytes));
        string _Guid = BitConverter.ToString(Guid.NewGuid().ToByteArray()).Replace("-", "");
        StringBuilder _RtfText = new StringBuilder(@"{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}\uc1\pard\lang2052\f0\fs18{\object\objemb{\*\objclass Paint.Picture}");
        _RtfText.Append(@"\objw" + _Width.ToString() + @"\objh" + _Height.ToString());
        _RtfText.AppendLine(@"{\*\objdata");
        _RtfText.AppendLine(@"010500000200000007000000504272757368000000000000000000" + BitConverter.ToString(BitConverter.GetBytes(_FileBytes.Length + 20)).Replace("-", ""));
        _RtfText.Append("7A676B65" + _Guid);         
        _RtfText.AppendLine(BitConverter.ToString(_FileBytes).Replace("-", ""));
        _RtfText.AppendLine(@"0105000000000000}{\result{\pict\wmetafile0}}}}");
        base.SelectedRtf = _RtfText.ToString();

wpf的简单多了
Image image = new Image()
        {
            Width = imagesize,
            Height = imagesize,
            UseLayoutRounding = true,
            Source = new BitmapImage(new Uri(imageUrl, UriKind.RelativeOrAbsolute))
        };
        new InlineUIContainer(image, rtb.Selection.Start);
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-4-26 13:26, Processed in 0.606852 second(s), 34 queries , Gzip On.

快速回复 返回顶部 返回列表