制作类似VS2008起始页中的列表框

一.引子

    在VS2008的起始页中,有一种控件类似我们常用的列表框,可以显示图片,有超链接功能,有的还有按钮,可以创建或打开项目。它的效果比较好看,在一些场合还是有比较多的使用机会。我利用这2天的闲暇时间也仿照它做了个类似的控件。

    该控件可以设计时添加子项,可以提供类似超链接的点击效果,显示不下时支持滚动条的滚动(支持鼠标滑轮滚动),在选中子项后,支持突出显示选中项边框。提供ItemClick,ItemDoubleClick,ItemChanged事件。

二.结构图

 

 

三.设计时效果

 

 

 

四.运行时效果

 

 

五.部分源代码

 

  1.  public partial class JcsTextList : UserControl
  2.     {
  3.         public delegate void ItemChangedEventHandler(Object sender, EventArgs e);
  4.         public event ItemChangedEventHandler ItemChanged;
  5.         public event EventHandler ItemClick;
  6.         public event EventHandler ItemDoubleClick;
  7.         private VScrollBar vScrollBar1; //滚动条
  8.         
  9.         private Int32 _itemSpace = 6;//item间隔
  10.         private const Int32 _itemleft = 6;//文字左侧间隔
  11.         private TextListItem _mouseHoverItem = null//鼠标移动到上面的ITEM
  12.         private TextListItem _selectedItem = null;//选择的ITEM
  13.         private TextListItemCollection _categories = new TextListItemCollection();//元素集合
  14.         private Color _borderColor = Color.Gray;//边框的颜色
  15.      
  16.         private Color _itemBackColor = Color.White;//item的背景色
  17.         private Color _itemForeColor = Color.Black;//item的前景色
  18.         private Color _itemSelectedColor = Color.LightSkyBlue ;//选定项的颜色
  19.         private Color _itemMoveOverColor = Color.LightBlue;//移动到item其上的颜色
  20.         private ImageList _imagelist;
  21.         private bool _isLink = true;
  22.         private ControlBorderStyle _borderstyle = ControlBorderStyle.None;
  23.     
  24.         /// <summary>
  25.         /// 控件边框类型
  26.         /// </summary>
  27.         public enum ControlBorderStyle
  28.         {
  29.             SingleLine,
  30.             Border3D,
  31.             None
  32.         }
  33.         
  34.         public JcsTextList()
  35.         {
  36.             InitializeComponent();
  37.             
  38.             this.BorderStyle = BorderStyle.None;
  39.             this.MinimumSize = new Size(40, 30);
  40.             vScrollBar1 = new VScrollBar();
  41.             vScrollBar1.Dock = DockStyle.Right;
  42.             this.Controls.Add(vScrollBar1);
  43.             this.vScrollBar1.Scroll +=new ScrollEventHandler(vScrollBar1_Scroll);
  44.             this.vScrollBar1.MouseEnter += new EventHandler(vScrollBar1_MouseEnter);
  45.             SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  46.             SetStyle(ControlStyles.DoubleBuffer, true);
  47.             SetStyle(ControlStyles.UserPaint, true);
  48.             SetStyle(ControlStyles.ResizeRedraw, true);
  49.             RefreshScrollBar();
  50.             this._categories.ItemChanged += new CollectionChangeEventHandler(OnCategoryCollectionChanged);
  51.             this.ItemChanged += new ItemChangedEventHandler(OnItemChanged);
  52.         }
  53.         void vScrollBar1_MouseEnter(object sender, EventArgs e)
  54.         {
  55.             if (_isLink)
  56.             {
  57.                 this.Cursor = Cursors.Default;
  58.             }
  59.         }
  60.         #region"属性"
  61.         [Category("Jcs属性"), Description("分组项的图标集合对象")]
  62.         public ImageList ImageList
  63.         {
  64.             get { return this._imagelist; }
  65.             set { this._imagelist = value; }
  66.         }
  67.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  68.         Category("Jcs属性"),Description("分组项的集合")]
  69.         public TextListItemCollection Categories
  70.         {
  71.             get
  72.             {
  73.                 return _categories;
  74.             }
  75.             set
  76.             {
  77.                 _categories = value;
  78.             }
  79.         }
  80.         //代码生成器不生成对象的代码
  81.         [Browsable(false)]
  82.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  83.         public TextListItem SelectedItem
  84.         {
  85.             get
  86.             {
  87.                 return _selectedItem;
  88.             }
  89.         }
  90.         
  91.         [Browsable(true),Category("Jcs属性"),Description("边框颜色")]
  92.         public Color BorderColor
  93.         {
  94.             get
  95.             {
  96.                 return _borderColor;
  97.             }
  98.             set
  99.             {
  100.                 if (_borderColor == value) return;
  101.                 _borderColor = value;
  102.                 Invalidate();
  103.             }
  104.         }
  105.         [Browsable(true), Category("Jcs属性"), Description("移动到Item上时显示的颜色")]
  106.         public Color ItemMoveOverColor
  107.         {
  108.             get { return _itemMoveOverColor; }
  109.             set
  110.             {
  111.                 _itemMoveOverColor = value;
  112.                 this.Invalidate();
  113.             }
  114.         }
  115.         [Browsable(true), Category("Jcs属性"), Description("Item的前景色")]
  116.         public Color ItemForeColor
  117.         {
  118.             get { return _itemForeColor; }
  119.             set 
  120.             {
  121.                 _itemForeColor = value;
  122.                 Invalidate();
  123.             }
  124.         }
  125.         [Browsable(true), Category("Jcs属性"), Description("Item的背景色")]
  126.         public Color ItemBackColor
  127.         {
  128.             get { return _itemBackColor; }
  129.             set
  130.             {
  131.                 _itemBackColor = value;
  132.                 Invalidate();
  133.             }
  134.         }
  135.         [Browsable(false )]
  136.         public VScrollBar scrollbar
  137.         {
  138.             get { return this.vScrollBar1; }
  139.         }
  140.         [Browsable(true), Category("Jcs属性"), Description("边框显示类型")]
  141.         public ControlBorderStyle ToolBoxBorderStyle
  142.         {
  143.             get { return this._borderstyle; }
  144.             set 
  145.             { 
  146.                 this._borderstyle = value ;
  147.                 this.Invalidate();
  148.             }
  149.         }
  150.         [Browsable(true), Category("Jcs属性"), Description("是否子项使用连接模式。")]
  151.         public bool IsUseLink
  152.         {
  153.             get { return this._isLink; }
  154.             set
  155.             {
  156.                 this._isLink = value;
  157.                 this.Invalidate();
  158.             }
  159.         }
  160.         #endregion
  161.         #region"重写"
  162.         protected override void OnPaint(PaintEventArgs e)
  163.         {
  164.             base.OnPaint(e);
  165.             Graphics g = e.Graphics;
  166.             Rectangle visibleRectangle = GetVisibleRect();
  167.             
  168.             //绘制边框
  169.             switch (this._borderstyle )
  170.             {
  171.                 case ControlBorderStyle.SingleLine :
  172.                     using (Pen p = new Pen(this._borderColor))
  173.                     {
  174.                         g.DrawRectangle(p, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
  175.                     }
  176.                     break;
  177.                 case ControlBorderStyle.Border3D :
  178.                     ControlPaint.DrawBorder3D(g,new Rectangle(0,0,this.Width - 1 ,this.Height -1), Border3DStyle.RaisedOuter);
  179.                     break;
  180.                 case ControlBorderStyle.None :
  181.                     break;
  182.             }
  183.             
  184.             //起始位置
  185.             Int32 top = _itemSpace;
  186.             if (vScrollBar1.Visible)
  187.             {
  188.                 top -= vScrollBar1.Value;
  189.             }
  190.             Int32 left = 0;
  191.             foreach (TextListItem item in _categories)
  192.             {
  193.                 string s = item.Caption;
  194.                 Font f = item.StringFont;
  195.                 int imageindex = item.ImageIndex;
  196.                 int maxwidth = visibleRectangle.Width - 2 - left;
  197.                 
  198.                 //绘制图标
  199.                 if (this._imagelist != null && imageindex >= 0 && this._imagelist.Images.Count > imageindex)
  200.                 {
  201.                     g.DrawImage(this._imagelist.Images[imageindex], new Rectangle(left, top, 16, 16));
  202.                     maxwidth -= 16;
  203.                     left += 16;
  204.                 }
  205.                 SizeF sizef = g.MeasureString(s, f, maxwidth);
  206.                 
  207.                 //绘制文字
  208.                 using (SolidBrush itemTextBrush = new SolidBrush(this._itemForeColor))
  209.                 {
  210.                     //如果是移动到其上的元素
  211.                     if (item == _mouseHoverItem)
  212.                     {
  213.                         itemTextBrush.Color = Color.Red;
  214.                     }
  215.                     g.DrawString(s, f, itemTextBrush, new Rectangle(left + 2 , top,(int) Math.Ceiling(sizef.Width ) , (int)Math.Floor(sizef.Height)));
  216.                 }
  217.                 //如果是选中的元素
  218.                 if (item == _selectedItem)
  219.                 {
  220.                     using(Pen p = new Pen(SystemColors.Desktop))
  221.                     {
  222.                         p.DashStyle = DashStyle.Dash;
  223.                         g.DrawRectangle(p,new Rectangle(left + 2,top -2,maxwidth - 4 ,(int)Math.Floor(sizef.Height) + 4));
  224.                     }
  225.                 }
  226.               
  227.                 top += (int)Math.Floor(sizef.Height) + _itemSpace;
  228.                 left = 0;
  229.             }
  230.         }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值