Esempio n. 1
0
        public void AddCellRenderer(IDataGridViewCellRenderer cellRenderer)
        {
            if (cellRenderer == null)
            {
                Debug.Assert(false, "cellRenderer 为 null");
                return;
            }

            if (_cellRenderers.Contains(cellRenderer))
            {
                Debug.Assert(false, "cellRenderer 已存在");
                return;
            }

            _cellRenderers.Add(cellRenderer);
        }
Esempio n. 2
0
        /// <summary>
        /// 绘制行中的单元格
        /// </summary>
        /// <param name="g"></param>
        /// <param name="bounds"></param>
        /// <param name="value"></param>
        /// <param name="state"></param>
        private void DrawRowCell(Graphics g, Rectangle bounds, int rowIndex, int columnIndex, object value, DataGridViewElementStates state)
        {
            if (bounds == Rectangle.Empty)
            {
                return;
            }

            g.Clear(_theme.RowBackColor);

            Rectangle contentBounds       = bounds;
            int       contentBoundsX      = contentBounds.X;
            int       contentBoundsY      = contentBounds.Y;
            int       contentBoundsWidth  = contentBounds.Width;
            int       contentBoundsHeight = contentBounds.Height;

            bool selected = (state & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected;

            #region 绘制选定或未选定情况下的单元格

            if (selected)
            {
                Brush backBrush;
                Pen   borderPen;

                if (_dataGridView.Focused)
                {
                    backBrush = new LinearGradientBrush(contentBounds, _theme.RowSelectedBackColorStart, _theme.RowSelectedBackColorEnd,
                                                        LinearGradientMode.Vertical);
                    borderPen = new Pen(_theme.RowSelectedBorderColor);
                }
                else
                {
                    backBrush = new LinearGradientBrush(contentBounds, _theme.RowUnFocusedSelectedColorStart, _theme.RowUnFocusedSelectedColorEnd,
                                                        LinearGradientMode.Vertical);
                    borderPen = new Pen(_theme.RowUnFocusedSelectedBorderColor);
                }

                DrawRowCell(g, columnIndex, bounds, backBrush, borderPen);

                backBrush.Dispose();
                borderPen.Dispose();
            }
            else
            {
                using (Brush backBrush = new SolidBrush(_theme.RowBackColor))
                {
                    g.FillRectangle(backBrush, bounds);
                }
            }

            #endregion

            #region 绘制鼠标经过时的背景

            if (_hoveredRow != null && rowIndex == _hoveredRow.Index)
            {
                Brush backBrush = new LinearGradientBrush(bounds, _theme.RowHoveredBackColorStart, _theme.RowHoveredBackColorEnd,
                                                          LinearGradientMode.Vertical);
                Pen borderPen = new Pen(_theme.RowHoveredBorderColor);

                DrawRowCell(g, columnIndex, bounds, backBrush, borderPen);

                backBrush.Dispose();
                borderPen.Dispose();
            }

            #endregion

            #region 绘制单元格的内容部分,如文本,checkbox,或图像

            if (value != null)
            {
                DataGridViewCell          cell         = _dataGridView[columnIndex, rowIndex];
                IDataGridViewCellRenderer cellRenderer = GetCellRenderer(cell);
                if (cellRenderer != null)
                {
                    cellRenderer.Paint(g, _dataGridView.ClientRectangle, bounds, rowIndex, state, value,
                                       cell.FormattedValue, cell.ErrorText, cell.Style);
                }
                //如果没有找到匹配的单元格渲染器,绘制内容的文本形式
                else
                {
                    string     text       = value.ToString();
                    SizeF      textSize   = g.MeasureString(text, _font);
                    RectangleF textBounds = new RectangleF();
                    textBounds.X      = contentBoundsX + 2;
                    textBounds.Y      = bounds.Y + (bounds.Height - textSize.Height) / 2;
                    textBounds.Width  = bounds.Width - 4;
                    textBounds.Height = textSize.Height;

                    using (SolidBrush fontBrush = new SolidBrush(_theme.RowTextColor))
                    {
                        g.DrawString(value.ToString(), _dataGridView.Font, fontBrush, textBounds, _cellTextStringFormat);
                    }
                }
            }

            #endregion
        }