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

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

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

C#仿QQ皮肤—更新DataGridView的鼠标跟随效果_第1张图片

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

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> Colordefaultcolor;
// 进入单元格时保存当前的颜色
protected override void OnCellMouseEnter(DataGridViewCellEventArgse)
{
base .OnCellMouseEnter(e);
try
{
defaultcolor
= Rows[e.RowIndex].DefaultCellStyle.BackColor;
}
catch (Exception)
{
}
}

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

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> Rows[e.RowIndex].DefaultCellStyle.BackColor;

是取得当前的默认颜色

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

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

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> // 移到单元格时的颜色
protected override void OnCellMouseMove(DataGridViewCellMouseEventArgse)
{
base .OnCellMouseMove(e);
try
{
Rows[e.RowIndex].DefaultCellStyle.BackColor
= Color.YellowGreen;
}
catch (Exception)
{
}
}

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

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> // 离开时还原颜色
protected override void OnCellMouseLeave(DataGridViewCellEventArgse)
{
base .OnCellMouseLeave(e);
try
{
Rows[e.RowIndex].DefaultCellStyle.BackColor
= defaultcolor;
}
catch (Exception)
{

}
}

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

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> 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();
}

Colordefaultcolor;

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

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

}
catch (Exception)
{

}
}

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

}
}

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

}
}
}
}

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

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

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

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

QQ:361983679 Email:[email protected] MSN:[email protected]

你可能感兴趣的:(datagridview)