Nazarite

导航

定义自己的dataGridView实现鼠标经过时高亮显示的效果

Winfrom继承dataGridView类实现鼠标经过时高亮显示的效果

winform C# 2010-04-11 17:44:16 阅读60 评论0 字号:

在做Winfrom编程时,为什么要建议用自己的控件呢?这样就算有什么东西要增加修改时,也可以做到统一修改和增加,现在本文就介绍一下如何实现继承dataGridView鼠标经过时高亮显示的效果,代码如下

  class GridView : System.Windows.Forms.DataGridView
    {
        #region Andy' Code

        #region 属性代码
        /// <summary>
        /// 进入Cells之前的颜色
        /// </summary>
        private static System.Drawing.Color _BeforInCellsColor;
        /// <summary>
        /// 鼠标经过时的高亮颜色
        /// </summary>
        private static System.Drawing.Color _MouseOverHighColor = System.Drawing.Color.SkyBlue;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.Description("鼠标经过时的高亮颜色")]
        [System.ComponentModel.DefaultValue(typeof(System.Drawing.Color), "SkyBlue")]
        [System.ComponentModel.Category("OtherStyle")]
        public System.Drawing.Color MouseOverHighColor
        {
            get { return GridView._MouseOverHighColor; }
            set { GridView._MouseOverHighColor = value; }
        }

        /// <summary>
        /// 是否启用鼠标经过时颜色高亮,如启用,SelectionMode也会被设为FullRowSelect
        /// </summary>
        private bool _UseMouseOverStyle = false;

        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.Category("OtherStyle")]
        [System.ComponentModel.Description("是否启用鼠标经过时颜色高亮,如启用,SelectionMode也会被设为FullRowSelect")]
        [System.ComponentModel.DefaultValue(false)]
        public bool UseMouseOverStyle
        {
            get { return _UseMouseOverStyle; }
            set
            {
                if (value)
                    this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                else
                    this.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
                _UseMouseOverStyle = value;
            }
        }

        private static Color _BeforCellSelect;

        #endregion

        #region 重写的方法

        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
            base.OnDataBindingComplete(e);
            if (this.Rows.Count > 0)
            {
                for (int i = 0; i < this.Rows.Count; i++)
                {
                    this.Rows[i].HeaderCell.Value = (i + 1).ToString();
                }
                if (this.SelectedRows.Count > 0)
                    SelectedRows[0].Selected = false;
            }
        }

        protected override void OnCellMouseLeave(DataGridViewCellEventArgs e)
        {
            base.OnCellMouseLeave(e);
            if (UseMouseOverStyle)
            {
                if (e.RowIndex > -1 && e.ColumnIndex > -1)
                {
                    if (!this.Rows[e.RowIndex].Selected)
                    {
                        this.Rows[e.RowIndex].DefaultCellStyle.BackColor = _BeforInCellsColor;
                    }
                }
            }
        }

        protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
        {
            base.OnCellMouseEnter(e);
            if (UseMouseOverStyle)
            {
                if (e.RowIndex > -1 && e.ColumnIndex > -1)
                {
                    if (!this.Rows[e.RowIndex].Selected)
                    {
                        _BeforInCellsColor = this.Rows[e.RowIndex].DefaultCellStyle.BackColor;
                        this.Rows[e.RowIndex].DefaultCellStyle.BackColor = _MouseOverHighColor;
                    }
                }
            }
        }

        protected override void OnRowLeave(DataGridViewCellEventArgs e)
        {
            base.OnRowLeave(e);
            this.Rows[e.RowIndex].DefaultCellStyle.BackColor = _BeforCellSelect;
        }

        protected override void OnCellClick(DataGridViewCellEventArgs e)
        {
            base.OnCellClick(e);
            _BeforCellSelect = _BeforInCellsColor;
        }
      

        #endregion

       
        #endregion       
    }

记得添加这些引用

using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Reflection;

完成后生成一下项目,就会在工具栏生成一个组件,

可以直接拉来用了

http://limingloved.blog.163.com/blog/static/132297658201031154416618/

我的应用:

添加一个类MyDataGridView,继承自System.Windows.Forms.DataGridView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Reflection;

namespace Solution1Test
{
    class MyDataGridView:System.Windows.Forms.DataGridView
    {
        #region Andy' Code

        #region 属性代码
        /// <summary>
        /// 进入Cells之前的颜色
        /// </summary>
        private static System.Drawing.Color _BeforInCellsColor;
        /// <summary>
        /// 鼠标经过时的高亮颜色
        /// </summary>
        private static System.Drawing.Color _MouseOverHighColor = System.Drawing.Color.SkyBlue;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.Description("鼠标经过时的高亮颜色")]
        [System.ComponentModel.DefaultValue(typeof(System.Drawing.Color), "SkyBlue")]
        [System.ComponentModel.Category("OtherStyle")]
        public System.Drawing.Color MouseOverHighColor
        {
            get { return MyDataGridView._MouseOverHighColor; }
            set { MyDataGridView._MouseOverHighColor = value; }
        }

        /// <summary>
        /// 是否启用鼠标经过时颜色高亮,如启用,SelectionMode也会被设为FullRowSelect
        /// </summary>
        private bool _UseMouseOverStyle = false;

        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.Category("OtherStyle")]
        [System.ComponentModel.Description("是否启用鼠标经过时颜色高亮,如启用,SelectionMode也会被设为FullRowSelect")]
        [System.ComponentModel.DefaultValue(false)]
        public bool UseMouseOverStyle
        {
            get { return _UseMouseOverStyle; }
            set
            {
                if (value)
                    this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                else
                    this.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
                _UseMouseOverStyle = value;
            }
        }

        private static Color _BeforCellSelect;

        #endregion

        #region 重写的方法

        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
            base.OnDataBindingComplete(e);
            if (this.Rows.Count > 0)
            {
                for (int i = 0; i < this.Rows.Count; i++)
                {
                    this.Rows[i].HeaderCell.Value = (i + 1).ToString();
                }
                if (this.SelectedRows.Count > 0)
                    SelectedRows[0].Selected = false;
            }
        }

        protected override void OnCellMouseLeave(DataGridViewCellEventArgs e)
        {
            base.OnCellMouseLeave(e);
            if (UseMouseOverStyle)
            {
                if (e.RowIndex > -1 && e.ColumnIndex > -1)
                {
                    if (!this.Rows[e.RowIndex].Selected)
                    {
                        this.Rows[e.RowIndex].DefaultCellStyle.BackColor = _BeforInCellsColor;
                    }
                }
            }
        }

        protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
        {
            base.OnCellMouseEnter(e);
            if (UseMouseOverStyle)
            {
                if (e.RowIndex > -1 && e.ColumnIndex > -1)
                {
                    if (!this.Rows[e.RowIndex].Selected)
                    {
                        _BeforInCellsColor = this.Rows[e.RowIndex].DefaultCellStyle.BackColor;
                        this.Rows[e.RowIndex].DefaultCellStyle.BackColor = _MouseOverHighColor;
                    }
                }
            }
        }

        protected override void OnRowLeave(DataGridViewCellEventArgs e)
        {
            base.OnRowLeave(e);
            this.Rows[e.RowIndex].DefaultCellStyle.BackColor = _BeforCellSelect;
        }

        protected override void OnCellClick(DataGridViewCellEventArgs e)
        {
            base.OnCellClick(e);
            _BeforCellSelect = _BeforInCellsColor;
        }
      

        #endregion

       
        #endregion       
    }

}

 

编译后,即可在工具栏的最上方生成自己的控件。

 

posted on 2010-06-10 15:20  Nazarite  阅读(1544)  评论(0编辑  收藏  举报