| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6398 人关注过本帖, 1 人收藏
标题:那个关于仿QQ图片和文字一起发送的问题!
只看楼主 加入收藏
shmilylee
Rank: 2
来 自:江苏扬州蹩脚小公司
等 级:论坛游民
威 望:2
帖 子:242
专家分:22
注 册:2007-7-6
收藏(1)
 问题点数:0 回复次数:13 
那个关于仿QQ图片和文字一起发送的问题!
之前回了一篇帖子问怎么样实现UDP同时传输文字和图片
想找那篇帖子回的,找不到了就开个主题吧!

之前我回答是将图片转换成2进制传输,接收后再转换成图片
方法应该没问题,但是我发现一个比较好的方法

我现在也在做这个功能,上下分别放2个richTextBox,和QQ一样
发言的时候我需要设定字体,颜色之类的
就用了rtf格式的文本
DateTime time = System.DateTime.Now;
            string DefaultMessage = "{\\rtf1\\ansi\\ansicpg936\\deff0\\deflang1033\\deflangfe2052{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset134 \\'cb\\'ce\\'cc\\'e5;}}"
+ "{\\colortbl ;\\red47\\green54\\blue153;}\\viewkind4\\uc1\\pard\\cf1\\lang2052\\fs18     " + 用户名 + "     " + time.ToString("yy-MM-dd hh:mm:ss") + "\\cf0\\f1\\par}";
            this.richAllMessage.SelectedRtf = DefaultMessage;
            this.richAllMessage.SelectedRtf = this.richMyMessage.Rtf;
DefaultMessage表示我固定要显示的内容就是谁在什么时候发言的

然后我想看看图片行不行
        /// <summary>
        /// 选择本地图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片文件|*.jpg|所有文件|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Clipboard.SetDataObject(Image.FromFile(openFileDialog1.FileName), false);
                this.richMyMessage.Paste();//发言框插入图片
            }   
        }
发送......成功......

效果看附件图片

然后想到UDP,因为我等下才做,原理很清楚了,直接发送符合rtf规范的字符串是可以文字图片混排的!

(FKGJ]{AL2P_DLVEZVY4@3O.jpg (36.79 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: 文字 
2008-10-06 15:49
hellson
Rank: 2
来 自:北京
等 级:新手上路
威 望:4
帖 子:195
专家分:0
注 册:2008-9-1
收藏
得分:0 
rtf 很好用,存数据库也就一句话就能解决,如果要编辑格式的话网上还有现成的正则

春了夏了秋冬了,来了来了又来了
相信我的帖子打开都很快,看我头像就知道了
2008-10-06 15:53
guang
Rank: 4
来 自:广东深圳
等 级:贵宾
威 望:13
帖 子:1414
专家分:285
注 册:2006-4-3
收藏
得分:0 
那个字体和颜色怎么设置,难道要每次都选中它再设置?

不相信未作牺牲竟先可拥有,只相信靠双手找到我的欲求!!
我的博客:http://liao5930.blog.
2008-10-06 16:11
shmilylee
Rank: 2
来 自:江苏扬州蹩脚小公司
等 级:论坛游民
威 望:2
帖 子:242
专家分:22
注 册:2007-7-6
收藏
得分:0 
固定的样式是写死的,比如上面的,用户名,发言时间等
而发言内容可以是默认的,也可以自己修改,richTextBox可以做设置的
具体代码可以参考我写的类,其中部分控件为第三方控件,richTextBox是系统自带的!
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Drawing.Text;
using Entity;

namespace IM
{
    public partial class ChatForm : Office2007Form
    {
        private FontStyle newFont;

        public ChatForm(string user)
        {
            InitializeComponent();
            this.Text = "正在与 " + user + " 交谈";
            //加载系统字体
            InstalledFontCollection fc = new InstalledFontCollection();
            foreach (FontFamily font in fc.Families)
            {
                ButtonItem item = new ButtonItem(font.Name, font.Name);
                (item);
            }
            //加载字号
            for (int i = 7; i <= 20; i++)
            {
                ButtonItem item = new ButtonItem(i.ToString(), i.ToString());
                (item);
            }
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSendMessage_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.richMyMessage.Rtf.Length.ToString());
            DateTime time = System.DateTime.Now;
            string DefaultMessage = "{\\rtf1\\ansi\\ansicpg936\\deff0\\deflang1033\\deflangfe2052{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset134 \\'cb\\'ce\\'cc\\'e5;}}"
+ "{\\colortbl ;\\red47\\green54\\blue153;}\\viewkind4\\uc1\\pard\\cf1\\lang2052\\fs18     " + StaticInfo._userName + "     " + time.ToString("yy-MM-dd hh:mm:ss") + "\\cf0\\f1\\par}";
            this.richAllMessage.SelectedRtf = DefaultMessage;
            this.richAllMessage.SelectedRtf = this.richMyMessage.Rtf;
            this.richAllMessage.Focus();        //聊天框获得焦点
            this.richAllMessage.ScrollToCaret();//滚动到最下面
            this.richMyMessage.Clear();         //清除发言内容
            this.richMyMessage.Focus();         //发言框获得焦点
        }

        /// <summary>
        /// 选定内容加粗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBold_Click(object sender, EventArgs e)
        {
            if (this.richMyMessage.SelectionFont.Bold == false)
            {
                newFont = newFont | FontStyle.Bold;
                setFont();
            }
            else
            {
                newFont = newFont ^ FontStyle.Bold;
                setFont();
            }
        }

        /// <summary>
        /// 选定内容斜体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnItalic_Click(object sender, EventArgs e)
        {
            if (this.richMyMessage.SelectionFont.Italic == false)
            {
                newFont = newFont | FontStyle.Italic;
                setFont();
            }
            else
            {
                newFont = newFont ^ FontStyle.Italic;
                setFont();
            }
        }

        /// <summary>
        /// 选定内容带下划线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUnderline_Click(object sender, EventArgs e)
        {
            if (this.richMyMessage.SelectionFont.Underline == false)
            {
                newFont = newFont | FontStyle.Underline;
                setFont();
            }
            else
            {
                newFont = newFont ^ FontStyle.Underline;
                setFont();
            }
        }

        /// <summary>
        ///  设置选定内容字体
        /// </summary>
        private void setFont()
        {
            this.richMyMessage.SelectionFont = new Font(this.richMyMessage.SelectionFont, newFont);
        }

        /// <summary>
        /// 选定内容颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void colorMessage_SelectedColorChanged(object sender, EventArgs e)
        {
            ColorPickerButton color = sender as ColorPickerButton;
            this.richMyMessage.SelectionColor = color.SelectedColor;
        }

        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxFont_SelectedIndexChanged(object sender, EventArgs e)
        {
            ButtonItem item = as ButtonItem;
            Font font = this.richMyMessage.SelectionFont;
            this.richMyMessage.SelectionFont = new Font(item.Name, font.Size, font.Style, font.Unit, font.GdiCharSet, font.GdiVerticalFont);
        }

        /// <summary>
        /// 设置字号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            ButtonItem item = as ButtonItem;
            float size = Convert.ToSingle(item.Name.ToString());
            Font font = this.richMyMessage.SelectionFont;
            this.richMyMessage.SelectionFont = new Font(font.FontFamily, size, font.Style, font.Unit, font.GdiCharSet, font.GdiVerticalFont);
        }

        /// <summary>
        /// 选择本地图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片文件|*.jpg|所有文件|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Clipboard.SetDataObject(Image.FromFile(openFileDialog1.FileName), false);
                this.richMyMessage.Paste();
            }   
        }
    }
}
2008-10-06 16:18
guang
Rank: 4
来 自:广东深圳
等 级:贵宾
威 望:13
帖 子:1414
专家分:285
注 册:2006-4-3
收藏
得分:0 
唉,看来只能是选中字符串后才能设置字体的了。。。。。

不相信未作牺牲竟先可拥有,只相信靠双手找到我的欲求!!
我的博客:http://liao5930.blog.
2008-10-06 16:31
shmilylee
Rank: 2
来 自:江苏扬州蹩脚小公司
等 级:论坛游民
威 望:2
帖 子:242
专家分:22
注 册:2007-7-6
收藏
得分:0 
要是同时改变所有字体的话,那就失去了richTextBox的意义了,不是吗?
2008-10-06 18:06
guang
Rank: 4
来 自:广东深圳
等 级:贵宾
威 望:13
帖 子:1414
专家分:285
注 册:2006-4-3
收藏
得分:0 
当然,但QQ中的字体不可能是将它选中后改变其字体再取消选择的吧....

不相信未作牺牲竟先可拥有,只相信靠双手找到我的欲求!!
我的博客:http://liao5930.blog.
2008-10-06 20:29
trhuang
Rank: 1
等 级:新手上路
帖 子:126
专家分:0
注 册:2007-4-1
收藏
得分:0 
太好了!!!最近就在为这事烦。。。。。。。终于有点相关资料看啦!谢!
2008-10-06 20:52
shmilylee
Rank: 2
来 自:江苏扬州蹩脚小公司
等 级:论坛游民
威 望:2
帖 子:242
专家分:22
注 册:2007-7-6
收藏
得分:0 
guang,你的问题我考虑了一下,好像确实是有问题,现在将类改写,应该能满足你的需要了

using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Drawing.Text;
using Entity;

namespace IM
{
    public partial class ChatForm : Office2007Form
    {
        private FontStyle newFont;
        private FontFamily newFamily = new FontFamily("宋体");
        private Single newSize = 9;
        private Color color;

        public ChatForm(string user)
        {
            InitializeComponent();
            this.Text = "正在与 " + user + " 交谈";
            //加载系统字体
            InstalledFontCollection fc = new InstalledFontCollection();
            foreach (FontFamily font in fc.Families)
            {
                ButtonItem item = new ButtonItem(font.Name, font.Name);
                (item);
            }
            //加载字号
            for (int i = 7; i <= 20; i++)
            {
                ButtonItem item = new ButtonItem(i.ToString(), i.ToString());
                (item);
            }
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSendMessage_Click(object sender, EventArgs e)
        {
            //string temp = (this.richMyMessage.Rtf);
            DateTime time = System.DateTime.Now;
            string DefaultMessage = "{\\rtf1\\ansi\\ansicpg936\\deff0\\deflang1033\\deflangfe2052{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset134 \\'cb\\'ce\\'cc\\'e5;}}"
+ "{\\colortbl ;\\red47\\green54\\blue153;}\\viewkind4\\uc1\\pard\\cf1\\lang2052\\fs18     " + StaticInfo._userName + "     " + time.ToString("yy-MM-dd hh:mm:ss") + "\\cf0\\f1\\par}";
            this.richAllMessage.SelectedRtf = DefaultMessage;
            this.richAllMessage.SelectedRtf = this.richMyMessage.Rtf;
            this.richAllMessage.Focus();        //聊天框获得焦点
            this.richAllMessage.ScrollToCaret();//滚动到最下面
            this.richMyMessage.Clear();         //清除发言内容
            this.richMyMessage.Focus();         //发言框获得焦点
            setFont();
            this.richMyMessage.SelectionColor = color;
        }

        /// <summary>
        /// 选定内容加粗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBold_Click(object sender, EventArgs e)
        {
            if (this.btnBold.Checked == false)
                this.btnBold.Checked = true;
            else
                this.btnBold.Checked = false;
            newFont = newFont ^ FontStyle.Bold;
            setFont();
        }

        /// <summary>
        /// 选定内容斜体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnItalic_Click(object sender, EventArgs e)
        {
            if (this.btnItalic.Checked == false)
                this.btnItalic.Checked = true;
            else
                this.btnItalic.Checked = false;
            newFont = newFont ^ FontStyle.Italic;
            setFont();
        }

        /// <summary>
        /// 选定内容带下划线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUnderline_Click(object sender, EventArgs e)
        {
            if (this.btnUnderline.Checked == false)
                this.btnUnderline.Checked = true;
            else
                this.btnUnderline.Checked = false;
            newFont = newFont ^ FontStyle.Underline;
            setFont();
        }

        /// <summary>
        ///  设置选定内容字体
        /// </summary>
        private void setFont()
        {
            this.richMyMessage.SelectionFont = new Font(newFamily, newSize, newFont);
            this.richMyMessage.Focus();         //发言框获得焦点
        }

        /// <summary>
        /// 选定内容颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void colorMessage_SelectedColorChanged(object sender, EventArgs e)
        {
            ColorPickerButton colorButton = sender as ColorPickerButton;
            color = colorButton.SelectedColor;
            this.richMyMessage.SelectionColor = color;
        }

        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxFont_SelectedIndexChanged(object sender, EventArgs e)
        {
            ButtonItem item = as ButtonItem;
            newFamily = new FontFamily(item.Name);
            setFont();
        }

        /// <summary>
        /// 设置字号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            ButtonItem item = as ButtonItem;
            newSize = Convert.ToSingle(item.Name.ToString());
            setFont();
        }

        /// <summary>
        /// 选择本地图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片文件|*.jpg|所有文件|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Clipboard.SetDataObject(Image.FromFile(openFileDialog1.FileName), false);
                this.richMyMessage.Paste();
            }   
        }
    }
}
2008-10-06 23:13
yzmiaomiao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-12-5
收藏
得分:0 
RichTextBox RTF
数据库中存放了 RichTextBox RTF 读取出此数据,给RichTextBox 赋值时字体颜色不是我设置的颜色
2008-12-05 11:16
快速回复:那个关于仿QQ图片和文字一起发送的问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032161 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved