C#仿QQ皮肤—更新DataGridView的鼠标跟随效果

C#仿QQ皮肤-实现原理系列文章导航
                                                             
http://www.cnblogs.com/sufei/archive/2010/03/10/1682847.html    

         

 

             我们先来看看更新后的效果吧

 

其实方法很简单我们只要重写一下几个事件就行了,首先是OnCellMouseEnter事件,这个事件的做用只有一个就是保存当前的行背景色,我们都知道我们鼠标移到上方时要改变颜色,那移过后是不是还原呢,不然就成了染布了,呵呵,这个事件的做用就是记录和保存颜色一起看实现

 

代码
Color defaultcolor;
  
// 进入单元格时保存当前的颜色
         protected   override   void  OnCellMouseEnter(DataGridViewCellEventArgs e)
        {
            
base .OnCellMouseEnter(e);
            
try
            {
                defaultcolor 
=  Rows[e.RowIndex].DefaultCellStyle.BackColor;
            }
            
catch  (Exception)
            {
            }
        }

 

defaultcolor是一个颜色变量用来保存默认颜色

Rows[e.RowIndex].DefaultCellStyle.BackColor;

 

是取得当前的默认颜色

这样每次在改变颜色之前就可以保存默认颜色了那就是defaultcolor 大家一定要记着不能把这一步写在OnCellMouseMove事件里,一定要写在OnCellMouseEnter事件里,那是因为OnCellMouseEnter在OnCellMouseMove事件之前执行,否则是取不到默认颜色的就成了染布,呵呵

 

那我们再来看OnCellMouseMove事件吧,它的作用只有一个移到单元格时的改变颜色

 

代码
   // 移到单元格时的颜色
         protected   override   void  OnCellMouseMove(DataGridViewCellMouseEventArgs e)
        {
            
base .OnCellMouseMove(e);
            
try
            {
                Rows[e.RowIndex].DefaultCellStyle.BackColor 
=  Color.YellowGreen;
            }
            
catch  (Exception)
            {
            }
        }

 

 

拉下来就是用OnCellMouseLeave事件还原默认颜色了

 

代码
  // 离开时还原颜色
         protected   override   void  OnCellMouseLeave(DataGridViewCellEventArgs e)
        {
            
base .OnCellMouseLeave(e);
            
try
            {
                Rows[e.RowIndex].DefaultCellStyle.BackColor 
=  defaultcolor;
            }
            
catch  (Exception)
            {

            }
        }

 

好了到这到这里工作就完成了,所有代码如下

 

代码
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Drawing;
using  System.Windows.Forms;

namespace  CRD.WinUI.Editors
{
    
public   class  DataGridView : System.Windows.Forms.DataGridView
    {
        
public  DataGridView()
        {
            
this .SetStyle(ControlStyles.DoubleBuffer  |  ControlStyles.AllPaintingInWmPaint,  true );
        }

        
protected   override   void  OnCreateControl()
        {
            
this .EnableHeadersVisualStyles  =   false ;
            
this .ColumnHeadersDefaultCellStyle.BackColor  =  Color.FromArgb( 247 246 239 );
            
this .ColumnHeadersBorderStyle  =  DataGridViewHeaderBorderStyle.Raised;
            
this .ColumnHeadersHeight  =   26 ;
            
this .ColumnHeadersHeightSizeMode  =  DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            
this .ColumnHeadersDefaultCellStyle.Alignment  =  System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            
this .ColumnHeadersDefaultCellStyle.ForeColor  =  System.Drawing.SystemColors.WindowText;
            
this .ColumnHeadersDefaultCellStyle.SelectionBackColor  =  System.Drawing.SystemColors.Highlight;
            
this .ColumnHeadersDefaultCellStyle.SelectionForeColor  =  System.Drawing.SystemColors.HighlightText;
            
this .RowHeadersDefaultCellStyle.Alignment  =  System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            
this .RowHeadersDefaultCellStyle.BackColor  =  System.Drawing.SystemColors.Window;
            
this .RowHeadersDefaultCellStyle.ForeColor  =  System.Drawing.SystemColors.WindowText;
            
this .RowHeadersBorderStyle  =  System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
            
this .DefaultCellStyle.SelectionBackColor  =  Color.Wheat;
            
this .DefaultCellStyle.SelectionForeColor  =  Color.DarkSlateBlue;
            
this .RowHeadersWidthSizeMode  =  DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            
this .GridColor  =  System.Drawing.SystemColors.GradientActiveCaption;
            
this .BackgroundColor  =  System.Drawing.SystemColors.Window;
            
this .BorderStyle  =  System.Windows.Forms.BorderStyle.Fixed3D;
            
this .AllowUserToOrderColumns  =   true ;
            
this .AutoGenerateColumns  =   true ;

            
base .OnCreateControl();
        }

        Color defaultcolor;

        
// 移到单元格时的颜色
         protected   override   void  OnCellMouseMove(DataGridViewCellMouseEventArgs e)
        {

            
base .OnCellMouseMove(e);
            
try
            {
                Rows[e.RowIndex].DefaultCellStyle.BackColor 
=  Color.YellowGreen;

            }
            
catch  (Exception)
            {

            }
        }

        
// 进入单元格时保存当前的颜色
         protected   override   void  OnCellMouseEnter(DataGridViewCellEventArgs e)
        {
            
base .OnCellMouseEnter(e);
            
try
            {
                defaultcolor 
=  Rows[e.RowIndex].DefaultCellStyle.BackColor;
            }
            
catch  (Exception)
            {

            }
        }

        
// 离开时还原颜色
         protected   override   void  OnCellMouseLeave(DataGridViewCellEventArgs e)
        {
            
base .OnCellMouseLeave(e);
            
try
            {
                Rows[e.RowIndex].DefaultCellStyle.BackColor 
=  defaultcolor;
            }
            
catch  (Exception)
            {

            }
        }
    }
}

 

 

 

 只要我们生成一下就行了,然后项目的各个地方 就会要着变了,不需要动任何代码就OK,其实这也是自定义控件的好处,只要我们重写一下控件 ,就要吧让系统涣然一新了,最新版的源代码已经上传了,请大家到导航页面去下载最新版本 因为有点烂没有写成属性的方式,大家如果感觉颜色不好看可以自己改改,我会在下次更新时改成属性的方式 ,让大家在直接选择颜色,,,,,,,,,,,,,, 

 

                         

                              如有转载请注明出处谢谢合作!!!

签名:做一番一生引以为豪的事业;找一个一生荣辱与共的妻子;在有生之年报答帮过我的人;并有能力帮助需要帮助的人;

我的主页欢迎大家过来交流探讨:http://sufei.cnblogs.com/           

QQ:361983679 Email:sufei.1013@163.com  MSN:sufei.1013@163.com 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值