读书人

DataGirdView重写一部分界面源码

发布时间: 2012-09-17 12:06:51 作者: rapoo

DataGirdView重写部分界面源码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Winner.CU.CheckAccounts.DataAccess;
using Winner.CU.CheckAccounts.Facade;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace CheckAccounts
{
??? public? class testDataGirdView:System.Windows.Forms.DataGridView
??? {
??????? /// <summary>
??????? /// 重绘Column、Row
??????? /// </summary>
??????? /// <param name="e"></param>
??????? protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
??????? {

??????????? //如果是Column
??????????? if (e.RowIndex == -1)
??????????? {
??????????????? drawColumnAndRow(e);
??????????????? e.Handled = true;
??????????????? //如果是Rowheader
??????????? }
??????????? else if (e.ColumnIndex < 0 && e.RowIndex >= 0)
??????????? {
??????????????? drawColumnAndRow(e);
??????????????? e.Handled = true;
??????????? }

??????? }

??????? /// <summary>
??????? /// Column和RowHeader绘制
??????? /// </summary>
??????? /// <param name="e"></param>
??????? void drawColumnAndRow(DataGridViewCellPaintingEventArgs e)
??????? {
??????????? // 绘制背景色
??????????? using (LinearGradientBrush backbrush =
??????????????? new LinearGradientBrush(e.CellBounds,
??????????????????? ProfessionalColors.MenuItemPressedGradientBegin,
??????????????????? ProfessionalColors.MenuItemPressedGradientMiddle
??????????????????? , LinearGradientMode.Vertical))
??????????? {
??????????????? Rectangle border = e.CellBounds;
??????????????? border.Width -= 1;
??????????????? //填充绘制效果
??????????????? e.Graphics.FillRectangle(backbrush, border);
??????????????? //绘制Column、Row的Text信息
??????????????? e.PaintContent(e.CellBounds);
??????????????? //绘制边框
??????????????? ControlPaint.DrawBorder3D(e.Graphics, e.CellBounds, Border3DStyle.Etched);
??????????? }

??????? }

??????? /// <summary>
??????? /// Row重绘前处理
??????? /// </summary>
??????? /// <param name="e"></param>
??????? protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e)
??????? {
??????????? base.OnRowPrePaint(e);
??????????? //是否是选中状态
??????????? if ((e.State & DataGridViewElementStates.Selected) ==
??????????????????????? DataGridViewElementStates.Selected)
??????????? {
??????????????? // 计算选中区域Size
??????????????? int width = this.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + RowHeadersWidth; //+ _RowHeadWidth;
??????????????? Rectangle rowBounds = new Rectangle(
????????????????? 0, e.RowBounds.Top, width,
??????????????????? e.RowBounds.Height);
??????????????? // 绘制选中背景色
??????????????? using (LinearGradientBrush backbrush =
??????????????????? new LinearGradientBrush(rowBounds,
??????????????????????? Color.GreenYellow,
??????????????????????? e.InheritedRowStyle.ForeColor, 90.0f))
??????????????? {
??????????????????? e.Graphics.FillRectangle(backbrush, rowBounds);
??????????????????? e.PaintCellsContent(rowBounds);
??????????????????? e.Handled = true;
??????????????? }
??????????? }

??????? }

??????? /// <summary>
??????? /// Row重绘后处理
??????? /// </summary>
??????? /// <param name="e"></param>
??????? protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
??????? {
??????????? base.OnRowPostPaint(e);
??????????? int width = this.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + RowHeadersWidth;// + _RowHeadWidth;
??????????? Rectangle rowBounds = new Rectangle(
?????????????????? 0, e.RowBounds.Top, width, e.RowBounds.Height);
??????????? if (this.CurrentCellAddress.Y == e.RowIndex)
??????????? {
??????????????? //设置选中边框
??????????????? e.DrawFocus(rowBounds, true);
??????????? }
??????? }

???
??? }
}

读书人网 >编程

热点推荐