标题:那个关于仿QQ图片和文字一起发送的问题!
取消只看楼主
shmilylee
Rank: 2
来 自:江苏扬州蹩脚小公司
等 级:论坛游民
威 望:2
帖 子:242
专家分:22
注 册:2007-7-6
 问题点数:0 回复次数:3 
那个关于仿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
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
shmilylee
Rank: 2
来 自:江苏扬州蹩脚小公司
等 级:论坛游民
威 望:2
帖 子:242
专家分:22
注 册:2007-7-6
得分:0 
要是同时改变所有字体的话,那就失去了richTextBox的意义了,不是吗?
2008-10-06 18:06
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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-236639-1-1.html




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

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