private void DrawButton(Graphics g) { Rectangle rectangle = GetButtonAreaRectangle(); // g.FillRectangle(Brushes.Red, rectangle); int arrowX = rectangle.Left + (rectangle.Width - rectangle.Width / 2); int arrowY = rectangle.Y; Point startPoint = new Point(arrowX, arrowY); Point endPoint = new Point(arrowX, arrowY + rectangle.Height); GraphicsPath arrowPath = DrawingTool.GetArrowPath(startPoint, endPoint, rectangle.Height); using (Brush arrowBrush = new LinearGradientBrush(rectangle, _theme.ArrowColorStart, _theme.ArrowColorEnd, 45)) { g.FillPath(arrowBrush, arrowPath); } arrowPath.Dispose(); }
/// <summary> /// 绘制表头 /// </summary> /// <param name="g"></param> /// <param name="bounds"></param> /// <param name="headerText"></param> private void DrawColumnHeader(Graphics g, Rectangle bounds, int columnIndex, string headerText) { DataGridViewColumnHeaderCell headerCell = _dataGridView.Columns[columnIndex].HeaderCell; Rectangle contentBounds = bounds; int contentBoundsX = contentBounds.X; int contentBoundsY = contentBounds.Y; int contentBoundsWidth = contentBounds.Width; int contentBoundsHeight = contentBounds.Height; #region 绘制背景 //绘制背景 using (LinearGradientBrush brush = new LinearGradientBrush(bounds, _theme.ColumnHeaderBackColorStart, _theme.ColumnHeaderBackColorEnd, LinearGradientMode.Vertical)) { g.FillRectangle(brush, bounds); } #endregion #region 绘制文本 //绘制文本 if (String.IsNullOrEmpty(headerText) == false) { SizeF textSize = g.MeasureString(headerText, _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; Brush textBrush = new SolidBrush(_theme.ColumnHeaderTextColor); g.DrawString(headerText, _dataGridView.Font, textBrush, textBounds, _columnHeaderTextStringFormat); textBrush.Dispose(); } #endregion #region 在右侧绘制分隔线 Rectangle separatorBounds = new Rectangle(); separatorBounds.X = contentBoundsX + contentBoundsWidth - 1; separatorBounds.Y = contentBoundsY; separatorBounds.Width = 1; separatorBounds.Height = contentBoundsHeight; Brush separatorBrush = new LinearGradientBrush(separatorBounds, _theme.ColumnHeaderSeparatorColorStart, _theme.ColumnHeaderSeparatorColorEnd, LinearGradientMode.Vertical); g.FillRectangle(separatorBrush, separatorBounds); separatorBrush.Dispose(); #endregion #region 绘制排序箭头 //如果列排序了,在单元格中间靠顶部绘制,类似win7资源管理器中的列排序箭头绘制 if (headerCell.SortGlyphDirection != SortOrder.None) { int arrowLength = 5; int arrowX = contentBoundsX + contentBoundsWidth / 2; //箭头中间的坐标 int arrowYOffset = 0; //绘制箭头的Y坐标 int arrowYStart = 0; //箭头本身的Y轴开始坐标 int arrowYEnd = 0; //箭头本身的Y轴结束坐标 switch (headerCell.SortGlyphDirection) { case SortOrder.Ascending: //▲ arrowYStart = arrowLength + arrowYOffset; arrowYEnd = arrowYOffset; break; case SortOrder.Descending: arrowYStart = arrowYOffset; arrowYEnd = arrowLength + arrowYOffset; break; } PointF startPoint = new PointF(arrowX, arrowYStart); PointF endPoint = new PointF(arrowX, arrowYEnd); GraphicsPath arrowPath = DrawingTool.GetArrowPath(startPoint, endPoint, arrowLength); //用于填充的Rectangle必须是Graphics的实际呈现区域 Rectangle arrowBrushRectangle = new Rectangle(arrowX - arrowLength / 2, 0, arrowLength, arrowLength); Brush arrowBrush = new LinearGradientBrush(arrowBrushRectangle, _theme.ArrowColorStart, _theme.ArrowColorEnd, 45); g.FillPath(arrowBrush, arrowPath); arrowPath.Dispose(); arrowBrush.Dispose(); } #endregion }