|
做个标记备忘。// Helper function to generate an Image from a BitmapSource
// 函数:从位图源生成190*120图像
private static Image ImageSourceToGDI(BitmapSource src)
{
var ms = new MemoryStream();
var encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(src));
encoder.Save(ms);
ms.Flush();
return Image.FromStream(ms);
}
1.界面
private void BlockPreviews2_Load(object sender, EventArgs e)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//打开块表
var bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
var namelist = new List();//新建图块名称表
ImageList imglist = new ImageList();//新建图块缩略图表
imglist.ImageSize = new Size(190,120);// 图片尺寸分别是宽和高
foreach (ObjectId blockRecordId in bt)//循环遍历块表中的块表记录
{
var btr = (BlockTableRecord)trans.GetObject(blockRecordId, OpenMode.ForRead);//打开块表记录对象
if (!btr.IsAnonymous && !btr.IsLayout)//在下拉列表框中只加入非匿名块和非布局块的名称
{
namelist.Add(btr.Name);//添加到图块名称表
imglist.Images.Add(ImageSourceToGDI(CMLContentSearchPreviews.GetBlockTRThumbnail(btr) as BitmapSource));//添加到图块缩略图表
}
}
listView1.View = View.LargeIcon;//设置为大图标视图
listView1.MultiSelect = false; //只能单选
listView1.LargeImageList = imglist;// 这里设置listView的SmallImageList ,用imgList将其撑大
for (int i = 0; i < imglist.Images.Count; i++)
{
var lvi = new ListViewItem();//新建ListViewItem
lvi.ImageIndex = i;//取出图片
lvi.Text = namelist[i];//取出图块名称
listView1.Items.Add(lvi);//添加到listView1
}
}
}
2.获取选中项
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.FocusedItem != null) //这个if必须的,不然会得到值但会报错
{
textBox1.Text = "当前选中的是:" + listView1.FocusedItem.SubItems[0].Text;//获得的listView的值显示在文本框里
}
} |
|