固定的样式是写死的,比如上面的,用户名,发言时间等
而发言内容可以是默认的,也可以自己修改,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();
}
}
}
}