一.引子
在VS2008的起始页中,有一种控件类似我们常用的列表框,可以显示图片,有超链接功能,有的还有按钮,可以创建或打开项目。它的效果比较好看,在一些场合还是有比较多的使用机会。我利用这2天的闲暇时间也仿照它做了个类似的控件。
该控件可以设计时添加子项,可以提供类似超链接的点击效果,显示不下时支持滚动条的滚动(支持鼠标滑轮滚动),在选中子项后,支持突出显示选中项边框。提供ItemClick,ItemDoubleClick,ItemChanged事件。
二.结构图
三.设计时效果
四.运行时效果
五.部分源代码
- public partial class JcsTextList : UserControl
- {
- public delegate void ItemChangedEventHandler(Object sender, EventArgs e);
- public event ItemChangedEventHandler ItemChanged;
- public event EventHandler ItemClick;
- public event EventHandler ItemDoubleClick;
- private VScrollBar vScrollBar1; //滚动条
- private Int32 _itemSpace = 6;//item间隔
- private const Int32 _itemleft = 6;//文字左侧间隔
- private TextListItem _mouseHoverItem = null; //鼠标移动到上面的ITEM
- private TextListItem _selectedItem = null;//选择的ITEM
- private TextListItemCollection _categories = new TextListItemCollection();//元素集合
- private Color _borderColor = Color.Gray;//边框的颜色
- private Color _itemBackColor = Color.White;//item的背景色
- private Color _itemForeColor = Color.Black;//item的前景色
- private Color _itemSelectedColor = Color.LightSkyBlue ;//选定项的颜色
- private Color _itemMoveOverColor = Color.LightBlue;//移动到item其上的颜色
- private ImageList _imagelist;
- private bool _isLink = true;
- private ControlBorderStyle _borderstyle = ControlBorderStyle.None;
- /// <summary>
- /// 控件边框类型
- /// </summary>
- public enum ControlBorderStyle
- {
- SingleLine,
- Border3D,
- None
- }
- public JcsTextList()
- {
- InitializeComponent();
- this.BorderStyle = BorderStyle.None;
- this.MinimumSize = new Size(40, 30);
- vScrollBar1 = new VScrollBar();
- vScrollBar1.Dock = DockStyle.Right;
- this.Controls.Add(vScrollBar1);
- this.vScrollBar1.Scroll +=new ScrollEventHandler(vScrollBar1_Scroll);
- this.vScrollBar1.MouseEnter += new EventHandler(vScrollBar1_MouseEnter);
- SetStyle(ControlStyles.AllPaintingInWmPaint, true);
- SetStyle(ControlStyles.DoubleBuffer, true);
- SetStyle(ControlStyles.UserPaint, true);
- SetStyle(ControlStyles.ResizeRedraw, true);
- RefreshScrollBar();
- this._categories.ItemChanged += new CollectionChangeEventHandler(OnCategoryCollectionChanged);
- this.ItemChanged += new ItemChangedEventHandler(OnItemChanged);
- }
- void vScrollBar1_MouseEnter(object sender, EventArgs e)
- {
- if (_isLink)
- {
- this.Cursor = Cursors.Default;
- }
- }
- #region"属性"
- [Category("Jcs属性"), Description("分组项的图标集合对象")]
- public ImageList ImageList
- {
- get { return this._imagelist; }
- set { this._imagelist = value; }
- }
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
- Category("Jcs属性"),Description("分组项的集合")]
- public TextListItemCollection Categories
- {
- get
- {
- return _categories;
- }
- set
- {
- _categories = value;
- }
- }
- //代码生成器不生成对象的代码
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public TextListItem SelectedItem
- {
- get
- {
- return _selectedItem;
- }
- }
- [Browsable(true),Category("Jcs属性"),Description("边框颜色")]
- public Color BorderColor
- {
- get
- {
- return _borderColor;
- }
- set
- {
- if (_borderColor == value) return;
- _borderColor = value;
- Invalidate();
- }
- }
- [Browsable(true), Category("Jcs属性"), Description("移动到Item上时显示的颜色")]
- public Color ItemMoveOverColor
- {
- get { return _itemMoveOverColor; }
- set
- {
- _itemMoveOverColor = value;
- this.Invalidate();
- }
- }
- [Browsable(true), Category("Jcs属性"), Description("Item的前景色")]
- public Color ItemForeColor
- {
- get { return _itemForeColor; }
- set
- {
- _itemForeColor = value;
- Invalidate();
- }
- }
- [Browsable(true), Category("Jcs属性"), Description("Item的背景色")]
- public Color ItemBackColor
- {
- get { return _itemBackColor; }
- set
- {
- _itemBackColor = value;
- Invalidate();
- }
- }
- [Browsable(false )]
- public VScrollBar scrollbar
- {
- get { return this.vScrollBar1; }
- }
- [Browsable(true), Category("Jcs属性"), Description("边框显示类型")]
- public ControlBorderStyle ToolBoxBorderStyle
- {
- get { return this._borderstyle; }
- set
- {
- this._borderstyle = value ;
- this.Invalidate();
- }
- }
- [Browsable(true), Category("Jcs属性"), Description("是否子项使用连接模式。")]
- public bool IsUseLink
- {
- get { return this._isLink; }
- set
- {
- this._isLink = value;
- this.Invalidate();
- }
- }
- #endregion
- #region"重写"
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- Graphics g = e.Graphics;
- Rectangle visibleRectangle = GetVisibleRect();
- //绘制边框
- switch (this._borderstyle )
- {
- case ControlBorderStyle.SingleLine :
- using (Pen p = new Pen(this._borderColor))
- {
- g.DrawRectangle(p, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
- }
- break;
- case ControlBorderStyle.Border3D :
- ControlPaint.DrawBorder3D(g,new Rectangle(0,0,this.Width - 1 ,this.Height -1), Border3DStyle.RaisedOuter);
- break;
- case ControlBorderStyle.None :
- break;
- }
- //起始位置
- Int32 top = _itemSpace;
- if (vScrollBar1.Visible)
- {
- top -= vScrollBar1.Value;
- }
- Int32 left = 0;
- foreach (TextListItem item in _categories)
- {
- string s = item.Caption;
- Font f = item.StringFont;
- int imageindex = item.ImageIndex;
- int maxwidth = visibleRectangle.Width - 2 - left;
- //绘制图标
- if (this._imagelist != null && imageindex >= 0 && this._imagelist.Images.Count > imageindex)
- {
- g.DrawImage(this._imagelist.Images[imageindex], new Rectangle(left, top, 16, 16));
- maxwidth -= 16;
- left += 16;
- }
- SizeF sizef = g.MeasureString(s, f, maxwidth);
- //绘制文字
- using (SolidBrush itemTextBrush = new SolidBrush(this._itemForeColor))
- {
- //如果是移动到其上的元素
- if (item == _mouseHoverItem)
- {
- itemTextBrush.Color = Color.Red;
- }
- g.DrawString(s, f, itemTextBrush, new Rectangle(left + 2 , top,(int) Math.Ceiling(sizef.Width ) , (int)Math.Floor(sizef.Height)));
- }
- //如果是选中的元素
- if (item == _selectedItem)
- {
- using(Pen p = new Pen(SystemColors.Desktop))
- {
- p.DashStyle = DashStyle.Dash;
- g.DrawRectangle(p,new Rectangle(left + 2,top -2,maxwidth - 4 ,(int)Math.Floor(sizef.Height) + 4));
- }
- }
- top += (int)Math.Floor(sizef.Height) + _itemSpace;
- left = 0;
- }
- }