在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(); //清空發送文本框
}
}
}
運作效果如下:
對于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.EnableDragDrop = true;
源代碼下載下傳:http://download.csdn.net/source/2842560