none
C#自定义控件 RRS feed

答案

  • 你好!
         文档里已经有很详细的讲述了,希望对你有帮助:
    http://msdn.microsoft.com/zh-cn/library/ms180794(VS.80).aspx
    周雪峰
    2009年5月13日 13:18
    版主
  • // 控件 01 无功能-
    public partial class ControlWithToolboxIcon : Control
    {
        public ControlWithToolboxIcon()
        {
            InitializeComponent();
        }
    
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }
    //控件 02 DataGridView 扩展
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.ComponentModel;
    using X.Library;
    
    namespace X.WinFormsControl
    {
        /// <summary>
        /// 标题:DataGridView 控件
        /// 日期:2009-04-22
        /// 描述:
        /// </summary>
        public class XGrid : DataGridView
        {
            private XGridOptionsGradient fOptionsGradient;
            private XGridOptionsView fOptionsView;
    
            public XGridOptionsGradient OptionsGradient
            {
                get { return fOptionsGradient; }
                set { fOptionsGradient = value; }
            }
    
            public XGridOptionsView OptionsView
            {
                get { return fOptionsView; }
                set
                {
                    if (this.fOptionsView.ShowRowNumber != value.ShowRowNumber)
                    {
                        this.fOptionsView.ShowRowNumber = value.ShowRowNumber;
                    }
    
                    if (this.fOptionsView.ShowGradient != value.ShowGradient)
                    {
                        this.fOptionsView.ShowGradient = value.ShowGradient;
                    }
    
                    if (this.fOptionsView.ColumnAutoWith != value.ColumnAutoWith)
                    {
                        this.fOptionsView.ColumnAutoWith = value.ColumnAutoWith;
                        if (this.fOptionsView.ColumnAutoWith == DefaultBoolean.Default||
                            this.fOptionsView.ColumnAutoWith == DefaultBoolean.True)
                            this.UpdateAutoSizeColumnMode(DataGridViewAutoSizeColumnMode.Fill);
                        else
                            this.UpdateAutoSizeColumnMode(DataGridViewAutoSizeColumnMode.None);
                    }
                }
            }
    
            public XGrid()
            {
                this.InitClass();
            }
    
            protected virtual void InitClass()
            {
                this.fOptionsView = new XGridOptionsView();
                this.fOptionsGradient = new XGridOptionsGradient();
    
                Padding fPadding = new Padding(0, 1, 0, this.fOptionsGradient.RowHeight);
                this.RowTemplate.DefaultCellStyle.Padding = fPadding;
                //把整行背景色设为透明(若不这样就不能渐变)
                this.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.Transparent;
                this.RowTemplate.Height += this.fOptionsGradient.RowHeight;
                this.AllowUserToAddRows = false;
                this.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                this.CellBorderStyle = DataGridViewCellBorderStyle.None;
                this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                this.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders);
    
                #region " Events "
                //列宽时重画
                base.ColumnWidthChanged += new DataGridViewColumnEventHandler(XDataGridView_ColumnWidthChanged);
                base.RowPrePaint += new DataGridViewRowPrePaintEventHandler(XDataGridView_RowPrePaint); // Generate Row Gradient
                base.RowPostPaint += new DataGridViewRowPostPaintEventHandler(XDataGridView_RowPostPaint); // Generate Row Number
                #endregion
    
                //禁止修改
                this.ReadOnly = true;
                this.Dock = DockStyle.Fill;
            }
    
            #region " Generate Row Gradient "
    
            private void XDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
            {
                // Generate Row Gradient
                // 如果按下面那样写的话,就没有数据直奇怪
                // e.PaintParts &= DataGridViewPaintParts.Focus;
                e.PaintParts &= ~DataGridViewPaintParts.Focus;
    
                if ((e.State & DataGridViewElementStates.Selected) ==
                    DataGridViewElementStates.Selected)
                {
                    Rectangle rowBounds = new Rectangle(
                        this.RowHeadersWidth, e.RowBounds.Top,
                        this.Columns.GetColumnsWidth(
                        DataGridViewElementStates.Visible) -
                        this.HorizontalScrollingOffset + 1,
                        e.RowBounds.Height);
    
                    using (Brush backbrush =
                        new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
                        this.fOptionsGradient.Color1, 
                        this.fOptionsGradient.Color2, 
                        System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
                    {
                        e.Graphics.FillRectangle(backbrush, rowBounds);
                    }
                }
            }
    
            #endregion
    
            #region " Generate Row Number "
    
            private void XDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                if (this.OptionsView.ShowRowNumber == DefaultBoolean.Default||
                    this.OptionsView.ShowRowNumber == DefaultBoolean.True)
                {
                    // Generate Row Number
                    using (SolidBrush b = new SolidBrush(RowHeadersDefaultCellStyle.ForeColor))
                    {
                        /*
                        e.Graphics.DrawString(e.RowIndex.ToString(
                            System.Globalization.CultureInfo.CurrentUICulture), 
                            e.InheritedRowStyle.Font, b,
                            e.RowBounds.Location.X + 20, 
                            e.RowBounds.Location.Y + 4);
                         */
    
                        e.Graphics.DrawString((e.RowIndex + 1).ToString(),
                            e.InheritedRowStyle.Font, b,
                            e.RowBounds.Location.X + 16,
                            e.RowBounds.Location.Y + 4);
                    }
                }
            }
    
            #endregion
    
            private void XDataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
            {
                if (this.OptionsView.ShowGradient == DefaultBoolean.Default ||
                    this.OptionsView.ShowGradient == DefaultBoolean.True)
                    this.Invalidate();
            }
    
            #region " ColumnAutoWith "
    
            protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
            {
                base.OnDataBindingComplete(e);
    
                if (this.fOptionsView.ColumnAutoWith == DefaultBoolean.Default ||
                    this.fOptionsView.ColumnAutoWith == DefaultBoolean.True)
                    this.UpdateAutoSizeColumnMode(DataGridViewAutoSizeColumnMode.Fill);
            }
    
            #endregion
    
            #region " Best Fit Columns "
            /// <summary>
            /// 设置所有列为最佳列宽
            /// </summary>
            public virtual void BestFitColumns()
            {
                this.UpdateAutoSizeColumnMode(DataGridViewAutoSizeColumnMode.DisplayedCells);
            }
    
            #endregion
    
            public virtual void UpdateAutoSizeColumnMode(DataGridViewAutoSizeColumnMode fAutoSizeColumnMode)
            {
                foreach (DataGridViewColumn fColumn in this.Columns)
                    fColumn.AutoSizeMode = fAutoSizeColumnMode;
            }
        }
    }
    
    using System.Drawing;
    
    namespace X.WinFormsControl
    {
        /// <summary>
        /// 标题:DataGridView 行渐变颜色
        /// 日期:2009-04-22
        /// </summary>
        public class XGridOptionsGradient
        {
            private Color fColor1;
            private Color fColor2;
            public int fRowHeight = 0;
    
            /// <summary>
            /// 获取或设置一个值,它表示渐变起始色的 Color 结构。
            /// </summary>
            public Color Color1
            {
                get { return fColor1; }
                set { fColor1 = value; }
            }
    
            /// <summary>
            /// 获取或设置一个值,它表示渐变起结束的 Color 结构。
            /// </summary>
            public Color Color2
            {
                get { return fColor2; }
                set { fColor2 = value; }
            }
    
            public int RowHeight
            {
                get { return fRowHeight; }
                set { fRowHeight = value; }
            }
    
            /// <summary>
            /// 初始化 XGridOptionsGradient
            /// </summary>
            public XGridOptionsGradient() : this(ColorTranslator.FromHtml("#06B4F3"), ColorTranslator.FromHtml("#0044C1")) { }
            public XGridOptionsGradient(Color fColor1, Color fColor2) : this(fColor1, fColor2, 0) { }
            /// <summary>
            /// 初始化 XGridOptionsGradient
            /// </summary>
            /// <param name="fColor1">渐变起始色的 Color 结构</param>
            /// <param name="fColor2">渐变起结束的 Color 结构</param>
            public XGridOptionsGradient(Color fColor1, Color fColor2, int fRowHeight)
            {
                this.fColor1 = fColor1;
                this.fColor2 = fColor2;
                this.fRowHeight = fRowHeight;
            }
        }
    }
    
    using X.Library;
    
    namespace X.WinFormsControl
    {
        /// <summary>
        /// 标题:XGrid 控制默认参数类
        /// 日期:2009-04-22
        /// 描述:
        /// </summary>
        public class XGridOptionsView
        {
            private DefaultBoolean fShowRowNumber;
            private DefaultBoolean fShowGradient;
            private DefaultBoolean fColumnAutoWith;
    
            /// <summary>
            /// 获取或设置一个值,它表示是否为每一行数据显示行号
            /// </summary>
            public DefaultBoolean ShowRowNumber
            {
                get { return fShowRowNumber; }
                set { fShowRowNumber = value; }
            }
    
            /// <summary>
            /// 获取或设置一个值,它表未是否在行上显示渐变色彩
            /// </summary>
            public DefaultBoolean ShowGradient
            {
                get { return fShowGradient; }
                set { fShowGradient = value; }
            }
    
            /// <summary>
            /// 获取或设置一个值,它表未所有列是否自动调整最佳宽度
            /// </summary>
            public DefaultBoolean ColumnAutoWith
            {
                get { return fColumnAutoWith; }
                set { fColumnAutoWith = value; }
            }
    
            public XGridOptionsView() : this(DefaultBoolean.Default, DefaultBoolean.Default, DefaultBoolean.Default) { }
            public XGridOptionsView(DefaultBoolean fShowRowNumber, DefaultBoolean fShowGradient, DefaultBoolean fColumnAutoWith)
            {
                this.fShowRowNumber = fShowRowNumber;
                this.fShowGradient = fShowGradient;
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace X.Library
    {
        public enum DefaultBoolean
        {
            Default,
            True,
            False
        }
    }
    
    


    知识改变命运,奋斗成就人生!
    2009年5月13日 13:19
    版主

全部回复

  • 你好!
         文档里已经有很详细的讲述了,希望对你有帮助:
    http://msdn.microsoft.com/zh-cn/library/ms180794(VS.80).aspx
    周雪峰
    2009年5月13日 13:18
    版主
  • // 控件 01 无功能-
    public partial class ControlWithToolboxIcon : Control
    {
        public ControlWithToolboxIcon()
        {
            InitializeComponent();
        }
    
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }
    //控件 02 DataGridView 扩展
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.ComponentModel;
    using X.Library;
    
    namespace X.WinFormsControl
    {
        /// <summary>
        /// 标题:DataGridView 控件
        /// 日期:2009-04-22
        /// 描述:
        /// </summary>
        public class XGrid : DataGridView
        {
            private XGridOptionsGradient fOptionsGradient;
            private XGridOptionsView fOptionsView;
    
            public XGridOptionsGradient OptionsGradient
            {
                get { return fOptionsGradient; }
                set { fOptionsGradient = value; }
            }
    
            public XGridOptionsView OptionsView
            {
                get { return fOptionsView; }
                set
                {
                    if (this.fOptionsView.ShowRowNumber != value.ShowRowNumber)
                    {
                        this.fOptionsView.ShowRowNumber = value.ShowRowNumber;
                    }
    
                    if (this.fOptionsView.ShowGradient != value.ShowGradient)
                    {
                        this.fOptionsView.ShowGradient = value.ShowGradient;
                    }
    
                    if (this.fOptionsView.ColumnAutoWith != value.ColumnAutoWith)
                    {
                        this.fOptionsView.ColumnAutoWith = value.ColumnAutoWith;
                        if (this.fOptionsView.ColumnAutoWith == DefaultBoolean.Default||
                            this.fOptionsView.ColumnAutoWith == DefaultBoolean.True)
                            this.UpdateAutoSizeColumnMode(DataGridViewAutoSizeColumnMode.Fill);
                        else
                            this.UpdateAutoSizeColumnMode(DataGridViewAutoSizeColumnMode.None);
                    }
                }
            }
    
            public XGrid()
            {
                this.InitClass();
            }
    
            protected virtual void InitClass()
            {
                this.fOptionsView = new XGridOptionsView();
                this.fOptionsGradient = new XGridOptionsGradient();
    
                Padding fPadding = new Padding(0, 1, 0, this.fOptionsGradient.RowHeight);
                this.RowTemplate.DefaultCellStyle.Padding = fPadding;
                //把整行背景色设为透明(若不这样就不能渐变)
                this.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.Transparent;
                this.RowTemplate.Height += this.fOptionsGradient.RowHeight;
                this.AllowUserToAddRows = false;
                this.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                this.CellBorderStyle = DataGridViewCellBorderStyle.None;
                this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                this.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders);
    
                #region " Events "
                //列宽时重画
                base.ColumnWidthChanged += new DataGridViewColumnEventHandler(XDataGridView_ColumnWidthChanged);
                base.RowPrePaint += new DataGridViewRowPrePaintEventHandler(XDataGridView_RowPrePaint); // Generate Row Gradient
                base.RowPostPaint += new DataGridViewRowPostPaintEventHandler(XDataGridView_RowPostPaint); // Generate Row Number
                #endregion
    
                //禁止修改
                this.ReadOnly = true;
                this.Dock = DockStyle.Fill;
            }
    
            #region " Generate Row Gradient "
    
            private void XDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
            {
                // Generate Row Gradient
                // 如果按下面那样写的话,就没有数据直奇怪
                // e.PaintParts &= DataGridViewPaintParts.Focus;
                e.PaintParts &= ~DataGridViewPaintParts.Focus;
    
                if ((e.State & DataGridViewElementStates.Selected) ==
                    DataGridViewElementStates.Selected)
                {
                    Rectangle rowBounds = new Rectangle(
                        this.RowHeadersWidth, e.RowBounds.Top,
                        this.Columns.GetColumnsWidth(
                        DataGridViewElementStates.Visible) -
                        this.HorizontalScrollingOffset + 1,
                        e.RowBounds.Height);
    
                    using (Brush backbrush =
                        new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
                        this.fOptionsGradient.Color1, 
                        this.fOptionsGradient.Color2, 
                        System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
                    {
                        e.Graphics.FillRectangle(backbrush, rowBounds);
                    }
                }
            }
    
            #endregion
    
            #region " Generate Row Number "
    
            private void XDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                if (this.OptionsView.ShowRowNumber == DefaultBoolean.Default||
                    this.OptionsView.ShowRowNumber == DefaultBoolean.True)
                {
                    // Generate Row Number
                    using (SolidBrush b = new SolidBrush(RowHeadersDefaultCellStyle.ForeColor))
                    {
                        /*
                        e.Graphics.DrawString(e.RowIndex.ToString(
                            System.Globalization.CultureInfo.CurrentUICulture), 
                            e.InheritedRowStyle.Font, b,
                            e.RowBounds.Location.X + 20, 
                            e.RowBounds.Location.Y + 4);
                         */
    
                        e.Graphics.DrawString((e.RowIndex + 1).ToString(),
                            e.InheritedRowStyle.Font, b,
                            e.RowBounds.Location.X + 16,
                            e.RowBounds.Location.Y + 4);
                    }
                }
            }
    
            #endregion
    
            private void XDataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
            {
                if (this.OptionsView.ShowGradient == DefaultBoolean.Default ||
                    this.OptionsView.ShowGradient == DefaultBoolean.True)
                    this.Invalidate();
            }
    
            #region " ColumnAutoWith "
    
            protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
            {
                base.OnDataBindingComplete(e);
    
                if (this.fOptionsView.ColumnAutoWith == DefaultBoolean.Default ||
                    this.fOptionsView.ColumnAutoWith == DefaultBoolean.True)
                    this.UpdateAutoSizeColumnMode(DataGridViewAutoSizeColumnMode.Fill);
            }
    
            #endregion
    
            #region " Best Fit Columns "
            /// <summary>
            /// 设置所有列为最佳列宽
            /// </summary>
            public virtual void BestFitColumns()
            {
                this.UpdateAutoSizeColumnMode(DataGridViewAutoSizeColumnMode.DisplayedCells);
            }
    
            #endregion
    
            public virtual void UpdateAutoSizeColumnMode(DataGridViewAutoSizeColumnMode fAutoSizeColumnMode)
            {
                foreach (DataGridViewColumn fColumn in this.Columns)
                    fColumn.AutoSizeMode = fAutoSizeColumnMode;
            }
        }
    }
    
    using System.Drawing;
    
    namespace X.WinFormsControl
    {
        /// <summary>
        /// 标题:DataGridView 行渐变颜色
        /// 日期:2009-04-22
        /// </summary>
        public class XGridOptionsGradient
        {
            private Color fColor1;
            private Color fColor2;
            public int fRowHeight = 0;
    
            /// <summary>
            /// 获取或设置一个值,它表示渐变起始色的 Color 结构。
            /// </summary>
            public Color Color1
            {
                get { return fColor1; }
                set { fColor1 = value; }
            }
    
            /// <summary>
            /// 获取或设置一个值,它表示渐变起结束的 Color 结构。
            /// </summary>
            public Color Color2
            {
                get { return fColor2; }
                set { fColor2 = value; }
            }
    
            public int RowHeight
            {
                get { return fRowHeight; }
                set { fRowHeight = value; }
            }
    
            /// <summary>
            /// 初始化 XGridOptionsGradient
            /// </summary>
            public XGridOptionsGradient() : this(ColorTranslator.FromHtml("#06B4F3"), ColorTranslator.FromHtml("#0044C1")) { }
            public XGridOptionsGradient(Color fColor1, Color fColor2) : this(fColor1, fColor2, 0) { }
            /// <summary>
            /// 初始化 XGridOptionsGradient
            /// </summary>
            /// <param name="fColor1">渐变起始色的 Color 结构</param>
            /// <param name="fColor2">渐变起结束的 Color 结构</param>
            public XGridOptionsGradient(Color fColor1, Color fColor2, int fRowHeight)
            {
                this.fColor1 = fColor1;
                this.fColor2 = fColor2;
                this.fRowHeight = fRowHeight;
            }
        }
    }
    
    using X.Library;
    
    namespace X.WinFormsControl
    {
        /// <summary>
        /// 标题:XGrid 控制默认参数类
        /// 日期:2009-04-22
        /// 描述:
        /// </summary>
        public class XGridOptionsView
        {
            private DefaultBoolean fShowRowNumber;
            private DefaultBoolean fShowGradient;
            private DefaultBoolean fColumnAutoWith;
    
            /// <summary>
            /// 获取或设置一个值,它表示是否为每一行数据显示行号
            /// </summary>
            public DefaultBoolean ShowRowNumber
            {
                get { return fShowRowNumber; }
                set { fShowRowNumber = value; }
            }
    
            /// <summary>
            /// 获取或设置一个值,它表未是否在行上显示渐变色彩
            /// </summary>
            public DefaultBoolean ShowGradient
            {
                get { return fShowGradient; }
                set { fShowGradient = value; }
            }
    
            /// <summary>
            /// 获取或设置一个值,它表未所有列是否自动调整最佳宽度
            /// </summary>
            public DefaultBoolean ColumnAutoWith
            {
                get { return fColumnAutoWith; }
                set { fColumnAutoWith = value; }
            }
    
            public XGridOptionsView() : this(DefaultBoolean.Default, DefaultBoolean.Default, DefaultBoolean.Default) { }
            public XGridOptionsView(DefaultBoolean fShowRowNumber, DefaultBoolean fShowGradient, DefaultBoolean fColumnAutoWith)
            {
                this.fShowRowNumber = fShowRowNumber;
                this.fShowGradient = fShowGradient;
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace X.Library
    {
        public enum DefaultBoolean
        {
            Default,
            True,
            False
        }
    }
    
    


    知识改变命运,奋斗成就人生!
    2009年5月13日 13:19
    版主