在NET开发平台下,做IM需要选择表情框的时候,可以利用ImageListPopup这个来快速做出自己的表情选择框,这是CodeProject上的C#类,地址(http://www.codeproject.com/KB/selection/imagelistpopup.aspx)
下面介绍下使用方法:Visual Studio 2008SP1下
1.新建一个C#窗体程序,名称为TestFaceNet
2.添加2个RichTextBox,两个按钮,一个“表情”一个“发送”
3.在工程里面添加ImageListPopup.cs文件,和一张表情图片face2.bmp,图片的属性“生成操作”改为“嵌入的资源”;
4.窗体的代码如下:
using CustomUIControls;
namespace TestFaceNet
{
public partial class Form1 : Form
{
public ImageList imageList;
public ImageListPopup ilp;
public Form1()
{
InitializeComponent();
imageList = new ImageList();
imageList.ImageSize = new Size( 32, 32);
imageList.ColorDepth = ColorDepth.Depth32Bit; //32位的带alpha通道的可以直接透明
imageList.Images.AddStrip( new Bitmap(GetType(), "face2.bmp")); //加载资源表情图片
ilp = new ImageListPopup();
ilp.Init(imageList, 8, 8, 10, 2); //水平、垂直线间距,表情显示的列和行
ilp.ItemClick += new ImageListPopupEventHandler(OnItemClicked);
}
/************************************************************************/
/* 选择了表情 */
/************************************************************************/
public void OnItemClicked( object sender, ImageListPopupEventArgs e)
{
Image img = imageList.Images[e.SelectedItem];
Clipboard.SetDataObject(img);
richMsgTextBox.ReadOnly = false;
richMsgTextBox.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
}
/************************************************************************/
/* 表情按钮点击 */
/************************************************************************/
private void faceButton_Click( object sender, EventArgs e)
{
Point pt = PointToScreen( new Point(faceButton.Left, faceButton.Top));
ilp.Show(pt.X, pt.Y- 80);
}
/************************************************************************/
/* 发送按钮 */
/************************************************************************/
private void sendButton_Click( object sender, EventArgs e)
{
richLogTextBox.SelectionStart = 0; //将文本的起始点设为0
richLogTextBox.AppendText( "海洋之心");//将当前用户名添加到文本框中
richLogTextBox.AppendText( " " + DateTime.Now.ToString()); //将当前发送的时间添加到文本框中
richLogTextBox.AppendText( "/r/n "); //换行回车
richLogTextBox.SelectedRtf = richMsgTextBox.Rtf; //将发送信息添加到接收文本框中
richLogTextBox.ScrollToCaret(); // 使文本输出框的滚动条在最下方
richLogTextBox.Focus();
richMsgTextBox.Clear(); //清空发送文本框
}
}
}
namespace TestFaceNet
{
public partial class Form1 : Form
{
public ImageList imageList;
public ImageListPopup ilp;
public Form1()
{
InitializeComponent();
imageList = new ImageList();
imageList.ImageSize = new Size( 32, 32);
imageList.ColorDepth = ColorDepth.Depth32Bit; //32位的带alpha通道的可以直接透明
imageList.Images.AddStrip( new Bitmap(GetType(), "face2.bmp")); //加载资源表情图片
ilp = new ImageListPopup();
ilp.Init(imageList, 8, 8, 10, 2); //水平、垂直线间距,表情显示的列和行
ilp.ItemClick += new ImageListPopupEventHandler(OnItemClicked);
}
/************************************************************************/
/* 选择了表情 */
/************************************************************************/
public void OnItemClicked( object sender, ImageListPopupEventArgs e)
{
Image img = imageList.Images[e.SelectedItem];
Clipboard.SetDataObject(img);
richMsgTextBox.ReadOnly = false;
richMsgTextBox.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
}
/************************************************************************/
/* 表情按钮点击 */
/************************************************************************/
private void faceButton_Click( object sender, EventArgs e)
{
Point pt = PointToScreen( new Point(faceButton.Left, faceButton.Top));
ilp.Show(pt.X, pt.Y- 80);
}
/************************************************************************/
/* 发送按钮 */
/************************************************************************/
private void sendButton_Click( object sender, EventArgs e)
{
richLogTextBox.SelectionStart = 0; //将文本的起始点设为0
richLogTextBox.AppendText( "海洋之心");//将当前用户名添加到文本框中
richLogTextBox.AppendText( " " + DateTime.Now.ToString()); //将当前发送的时间添加到文本框中
richLogTextBox.AppendText( "/r/n "); //换行回车
richLogTextBox.SelectedRtf = richMsgTextBox.Rtf; //将发送信息添加到接收文本框中
richLogTextBox.ScrollToCaret(); // 使文本输出框的滚动条在最下方
richLogTextBox.Focus();
richMsgTextBox.Clear(); //清空发送文本框
}
}
}
运行效果如下:
对于ImageList Popup可以自己设置其不同的颜色,看下表:
可以如下设置:
ilp.BackgroundColor = Color.FromArgb(
241,
241,
241);
ilp.BackgroundOverColor = Color.FromArgb( 102, 154, 204);
ilp.HLinesColor = Color.FromArgb( 182, 189, 210);
ilp.VLinesColor = Color.FromArgb( 182, 189, 210);
ilp.BorderColor = Color.FromArgb( 0, 0, 0);
ilp.BackgroundOverColor = Color.FromArgb( 102, 154, 204);
ilp.HLinesColor = Color.FromArgb( 182, 189, 210);
ilp.VLinesColor = Color.FromArgb( 182, 189, 210);
ilp.BorderColor = Color.FromArgb( 0, 0, 0);
另外还可以设置是否可以拖动:
ilp.EnableDragDrop =
true;