zoukankan      html  css  js  c++  java
  • 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(247246239);
                
    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,其实这也是自定义控件的好处,只要我们重写一下控件 ,就要吧让系统涣然一新了,最新版的源代码已经上传了,请大家到导航页面去下载最新版本 因为有点烂没有写成属性的方式,大家如果感觉颜色不好看可以自己改改,我会在下次更新时改成属性的方式 ,让大家在直接选择颜色,,,,,,,,,,,,,, 

  • 相关阅读:
    正经学C#_循环[do while,while,for]:[c#入门经典]
    Vs 控件错位 右侧资源管理器文件夹点击也不管用,显示异常
    asp.net core 获取当前请求的url
    在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be
    用orchard core和asp.net core 3.0 快速搭建博客,解决iis 部署https无法登录后台问题
    System.Data.Entity.Core.EntityCommandExecution The data reader is incompatible with the specified
    初探Java设计模式3:行为型模式(策略,观察者等)
    MySQL教程77-CROSS JOIN 交叉连接
    MySQL教程76-HAVING 过滤分组
    MySQL教程75-使用GROUP BY分组查询
  • 原文地址:https://www.cnblogs.com/sufei/p/1734028.html
Copyright © 2011-2022 走看看