WinForm界面开发论(二)容器控件论
2007-08-24 14:04
351 查看
我们的生活中存在着大量的容器关系,比如吃饭需要有碗装米饭,碗放在桌上,桌在家里,家在某一栋楼内,楼在小区内...。一层一层的,控件开发也是这样。在上一篇中,我们介绍了一个简单的控件是如何编写的,本章再说说怎样实现多级的控件。
说到这里,我们需要一个例子,ACDSEE有个展现目录下所有文件夹和图片的控件,如下图。我们将一步步对该控件的开发过程进行详解。
![](http://p.blog.csdn.net/images/p_blog_csdn_net/zhxatx/temp1.jpg)
第一步,我们新建一个用户控件,假设名称为ImageBox,为类加上声明语句:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class ImageBox : UserControl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//.....
这样,ImageBox就为定义为容器控件,它可以摆放其他控件在容器控件里面。
第二部,我们再新建一个用户控件,就是每个显示图片的子项了,名称为ImageItem,并在设计器中设计它!
第三步,容器里面的项,和ListView的ListViewItem一样,里面有一个ItemList的集合,我们也需要实现这样的结合,并且希望可以使用[int]来获得Box里面的项,要实现集合的结构,我们需要使用System.Collections.ICollection接口,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class ImageItemCollection : System.Collections.ICollection
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private SortedList items = new SortedList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int Count
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return items.Count;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//------------------------------------事件----------------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public delegate void itemDelegate(ImageItem itemIsAdd,int addIndex);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public event itemDelegate AddItem;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//------------------------------------属性----------------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public ImageItem this[int index]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object tmp = items.GetByIndex(index);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return (ImageItem)tmp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public ImageItem this[string name]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object tmp = items[name];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return (ImageItem)tmp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//------------------------------------方法----------------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void RemoveAll()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.RemoveAllItem();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
items.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 添加一个新的图片项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="itemIsAdd">要添加的图片项</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <returns>包括新增后的图片项数</returns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int Add(ImageItem itemIsAdd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (itemIsAdd.Name == "ImageItem")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string autoName = "ImageItemAuto" + getAddItemNum();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itemIsAdd.Name = autoName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (this.AddItem != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.AddItem(itemIsAdd, items.Count + 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.items.Add(itemIsAdd.Name, itemIsAdd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return items.Count - 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 移除某个图片项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="itemIsRemove">要移除的项</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <returns>包括新增后的图片项数</returns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int Remove(ImageItem itemIsRemove)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(!items.Contains(itemIsRemove.Name))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("该图片项不在图片容器中");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
items.Remove(itemIsRemove.Name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (this.RemoveItem != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.RemoveItem(itemIsRemove);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return items.Count - 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int addItemNum = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string getAddItemNum()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addItemNum = addItemNum + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return addItemNum.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
第四步,我们需要把集合,容器,子项目这三个东西组装在一起,就完成了我们的图片展现控件了。我们在ImageBox类中加入下面的代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public ImageItemCollection Items = new ImageItemCollection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void ImageView_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Items.AddItem += new ImageItemCollection.itemDelegate(Items_AddItem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void Items_AddItem(ImageItem itemIsAdd, int itemIndex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itemIsAdd.Size = itemSize;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(itemIsAdd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.setImageLocation(itemIsAdd, itemIndex);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void setImageLocation(ImageItem itemIsAdd, int itemIndex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int columnNum = this.calculationsColumn();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int imageItemX = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (itemIndex % columnNum != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
imageItemX = ((itemIndex % columnNum) - 1) * (itemSize.Width + ImageLayout) + ImageLayout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
imageItemX = (columnNum - 1) * (itemSize.Width + ImageLayout) + ImageLayout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int rowNum = (itemIndex - 1) / columnNum;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int imageItemY = rowNum * (itemSize.Height + ImageLayout) + ImageLayout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//itemIsAdd.Location = new Point(imageItemX, imageItemY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itemIsAdd.Left = imageItemX;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itemIsAdd.Top = imageItemY + this.lbl_Point.Top;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private int calculationsColumn()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int thisWidth = this.Size.Width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int itemWidth = itemSize.Width + ImageLayout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int columnNum = thisWidth / itemWidth;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return columnNum;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
但调用项集合类的Add的时候,会在Box发生Add时间,把新项添加到Box中,并且根据需要,设置图片被插入的位置。如果还有其他的需要,如remove,设置item的大小,定义子项的双击事件等等,读者可以根据本章和上一章的原理,对控件进行扩展。
说到这里,我们需要一个例子,ACDSEE有个展现目录下所有文件夹和图片的控件,如下图。我们将一步步对该控件的开发过程进行详解。
![](http://p.blog.csdn.net/images/p_blog_csdn_net/zhxatx/temp1.jpg)
第一步,我们新建一个用户控件,假设名称为ImageBox,为类加上声明语句:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class ImageBox : UserControl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//.....
这样,ImageBox就为定义为容器控件,它可以摆放其他控件在容器控件里面。
第二部,我们再新建一个用户控件,就是每个显示图片的子项了,名称为ImageItem,并在设计器中设计它!
第三步,容器里面的项,和ListView的ListViewItem一样,里面有一个ItemList的集合,我们也需要实现这样的结合,并且希望可以使用[int]来获得Box里面的项,要实现集合的结构,我们需要使用System.Collections.ICollection接口,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class ImageItemCollection : System.Collections.ICollection
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private SortedList items = new SortedList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int Count
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return items.Count;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//------------------------------------事件----------------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public delegate void itemDelegate(ImageItem itemIsAdd,int addIndex);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public event itemDelegate AddItem;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//------------------------------------属性----------------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public ImageItem this[int index]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object tmp = items.GetByIndex(index);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return (ImageItem)tmp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public ImageItem this[string name]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object tmp = items[name];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return (ImageItem)tmp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//------------------------------------方法----------------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void RemoveAll()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.RemoveAllItem();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
items.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 添加一个新的图片项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="itemIsAdd">要添加的图片项</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <returns>包括新增后的图片项数</returns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int Add(ImageItem itemIsAdd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (itemIsAdd.Name == "ImageItem")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string autoName = "ImageItemAuto" + getAddItemNum();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itemIsAdd.Name = autoName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (this.AddItem != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.AddItem(itemIsAdd, items.Count + 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.items.Add(itemIsAdd.Name, itemIsAdd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return items.Count - 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 移除某个图片项
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="itemIsRemove">要移除的项</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <returns>包括新增后的图片项数</returns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int Remove(ImageItem itemIsRemove)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(!items.Contains(itemIsRemove.Name))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("该图片项不在图片容器中");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
items.Remove(itemIsRemove.Name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (this.RemoveItem != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.RemoveItem(itemIsRemove);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return items.Count - 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int addItemNum = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string getAddItemNum()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addItemNum = addItemNum + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return addItemNum.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
第四步,我们需要把集合,容器,子项目这三个东西组装在一起,就完成了我们的图片展现控件了。我们在ImageBox类中加入下面的代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public ImageItemCollection Items = new ImageItemCollection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void ImageView_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Items.AddItem += new ImageItemCollection.itemDelegate(Items_AddItem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void Items_AddItem(ImageItem itemIsAdd, int itemIndex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itemIsAdd.Size = itemSize;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Controls.Add(itemIsAdd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.setImageLocation(itemIsAdd, itemIndex);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void setImageLocation(ImageItem itemIsAdd, int itemIndex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int columnNum = this.calculationsColumn();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int imageItemX = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (itemIndex % columnNum != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
imageItemX = ((itemIndex % columnNum) - 1) * (itemSize.Width + ImageLayout) + ImageLayout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
imageItemX = (columnNum - 1) * (itemSize.Width + ImageLayout) + ImageLayout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int rowNum = (itemIndex - 1) / columnNum;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int imageItemY = rowNum * (itemSize.Height + ImageLayout) + ImageLayout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//itemIsAdd.Location = new Point(imageItemX, imageItemY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itemIsAdd.Left = imageItemX;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itemIsAdd.Top = imageItemY + this.lbl_Point.Top;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private int calculationsColumn()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int thisWidth = this.Size.Width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int itemWidth = itemSize.Width + ImageLayout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int columnNum = thisWidth / itemWidth;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return columnNum;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
但调用项集合类的Add的时候,会在Box发生Add时间,把新项添加到Box中,并且根据需要,设置图片被插入的位置。如果还有其他的需要,如remove,设置item的大小,定义子项的双击事件等等,读者可以根据本章和上一章的原理,对控件进行扩展。
相关文章推荐
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- WinForm界面开发之"SplashScreen控件"
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- [转]WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- WinForm界面开发之“分页控件”
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- WinForm界面开发之“HTML内容编辑控件”
- WinForm界面开发之"SplashScreen控件"
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- 分享-WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- WinForm界面开发论(一)控件基本论
- WinForm开发-界面控件到实体,实体到界面控件自动绑定
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- Winform界面布局-容器内的控件适应容器
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
- WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用