官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

来都来了,点个【推荐】再走吧,谢谢

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

准备工作

依然使用GDI+画图,不懂的先百度了解下

开始

添加一些枚举

  public enum FunelChartAlignment
{
/// <summary>
/// The left
/// </summary>
Left,
/// <summary>
/// The center
/// </summary>
Center,
/// <summary>
/// The right
/// </summary>
Right
} public enum FunelChartDirection
{
/// <summary>
/// Up
/// </summary>
UP,
/// <summary>
/// Down
/// </summary>
Down
}

添加一个项实体

   public class FunelChartItem
{
/// <summary>
/// Gets or sets the text.
/// </summary>
/// <value>The text.</value>
public string Text { get; set; }
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
public float Value { get; set; }
/// <summary>
/// Gets or sets the color of the value.
/// </summary>
/// <value>The color of the value.</value>
public System.Drawing.Color? ValueColor { get; set; }
/// <summary>
/// Gets or sets the color of the text fore.
/// </summary>
/// <value>The color of the text fore.</value>
public System.Drawing.Color? TextForeColor { get; set; }
}

添加一个类UCFunnelChart ,继承UserControl

添加一些控制属性

 /// <summary>
/// The title
/// </summary>
private string title;
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置标题")]
public string Title
{
get { return title; }
set
{
title = value;
ResetTitleSize();
Invalidate();
}
} /// <summary>
/// The title font
/// </summary>
private Font titleFont = new Font("微软雅黑", );
/// <summary>
/// Gets or sets the title font.
/// </summary>
/// <value>The title font.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置标题字体")]
public Font TitleFont
{
get { return titleFont; }
set
{
titleFont = value;
ResetTitleSize();
Invalidate();
}
} /// <summary>
/// The title fore color
/// </summary>
private Color titleForeColor = Color.Black;
/// <summary>
/// Gets or sets the color of the title fore.
/// </summary>
/// <value>The color of the title fore.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置标题文字颜色")]
public Color TitleForeColor
{
get { return titleForeColor; }
set
{
titleForeColor = value;
Invalidate();
}
}
/// <summary>
/// The items
/// </summary>
private FunelChartItem[] items;
/// <summary>
/// Gets or sets the items.
/// </summary>
/// <value>The items.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置项目")]
public FunelChartItem[] Items
{
get { return items; }
set
{
items = value;
Invalidate();
}
} /// <summary>
/// The direction
/// </summary>
private FunelChartDirection direction = FunelChartDirection.UP;
/// <summary>
/// Gets or sets the direction.
/// </summary>
/// <value>The direction.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置方向")]
public FunelChartDirection Direction
{
get { return direction; }
set
{
direction = value;
Invalidate();
}
} /// <summary>
/// The alignment
/// </summary>
private FunelChartAlignment alignment = FunelChartAlignment.Center;
/// <summary>
/// Gets or sets the alignment.
/// </summary>
/// <value>The alignment.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置对齐方式")]
public FunelChartAlignment Alignment
{
get { return alignment; }
set
{
alignment = value;
Invalidate();
}
} /// <summary>
/// The item text align
/// </summary>
private FunelChartAlignment itemTextAlign = FunelChartAlignment.Center;
/// <summary>
/// Gets or sets the item text align.
/// </summary>
/// <value>The item text align.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置文字位置")]
public FunelChartAlignment ItemTextAlign
{
get { return itemTextAlign; }
set
{
itemTextAlign = value;
ResetWorkingRect();
Invalidate();
}
}
/// <summary>
/// The show value
/// </summary>
private bool showValue = false;
/// <summary>
/// Gets or sets a value indicating whether [show value].
/// </summary>
/// <value><c>true</c> if [show value]; otherwise, <c>false</c>.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置是否显示值")]
public bool ShowValue
{
get { return showValue; }
set
{
showValue = value;
Invalidate();
}
} /// <summary>
/// The value format
/// </summary>
private string valueFormat = "0.##";
/// <summary>
/// Gets or sets the value format.
/// </summary>
/// <value>The value format.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置值格式化")]
public string ValueFormat
{
get { return valueFormat; }
set
{
valueFormat = value;
Invalidate();
}
} /// <summary>
/// The m rect working
/// </summary>
RectangleF m_rectWorking;
/// <summary>
/// The m title size
/// </summary>
SizeF m_titleSize = SizeF.Empty;
/// <summary>
/// The int split width
/// </summary>
int intSplitWidth = ;

构造函数初始化

  public UCFunnelChart()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.FontChanged += UCFunnelChart_FontChanged;
Font = new Font("微软雅黑", ); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.SizeChanged += UCFunnelChart_SizeChanged;
Size = new System.Drawing.Size(, );
items = new FunelChartItem[];
if (ControlHelper.IsDesignMode())
{
items = new FunelChartItem[];
for (int i = ; i < ; i++)
{
items[i] = new FunelChartItem()
{
Text = "item" + i,
Value = * (i + )
};
}
}
}

当大小及状态改变时 重新计算工作区域

   void UCFunnelChart_FontChanged(object sender, EventArgs e)
{
ResetWorkingRect();
} /// <summary>
/// Handles the SizeChanged event of the UCFunnelChart control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void UCFunnelChart_SizeChanged(object sender, EventArgs e)
{
ResetWorkingRect();
} /// <summary>
/// Resets the working rect.
/// </summary>
private void ResetWorkingRect()
{
if (itemTextAlign == FunelChartAlignment.Center)
{
m_rectWorking = new RectangleF(, m_titleSize.Height == ? : (m_titleSize.Height + ), this.Width, this.Height - (m_titleSize.Height == ? : (m_titleSize.Height + )));
}
else if (itemTextAlign == FunelChartAlignment.Left)
{
float fltMax = ;
if (items != null && items.Length > )
{
using (Graphics g = this.CreateGraphics())
{
fltMax = items.Max(p => g.MeasureString(p.Text, Font).Width);
}
}
m_rectWorking = new RectangleF(fltMax, m_titleSize.Height == ? : (m_titleSize.Height + ), this.Width - fltMax, this.Height - (m_titleSize.Height == ? : (m_titleSize.Height + )));
}
else
{
float fltMax = ;
if (items != null && items.Length > )
{
using (Graphics g = this.CreateGraphics())
{
fltMax = items.Max(p => g.MeasureString(p.Text, Font).Width);
}
}
m_rectWorking = new RectangleF(, m_titleSize.Height == ? : (m_titleSize.Height + ), this.Width - fltMax, this.Height - (m_titleSize.Height == ? : (m_titleSize.Height + )));
}
} /// <summary>
/// Resets the size of the title.
/// </summary>
private void ResetTitleSize()
{
if (string.IsNullOrEmpty(title))
{
m_titleSize = SizeF.Empty;
}
else
{
using (Graphics g = this.CreateGraphics())
{
m_titleSize = g.MeasureString(title, titleFont);
m_titleSize.Height += ;
}
}
ResetWorkingRect();
}

重绘

 protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh(); if (!string.IsNullOrEmpty(title))
{
g.DrawString(title, titleFont, new SolidBrush(titleForeColor), new RectangleF(, , this.Width, m_titleSize.Height), new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
} if (items == null || items.Length <= )
{
g.DrawString("没有数据", Font, new SolidBrush(Color.Black), this.m_rectWorking, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
return;
} List<FunelChartItem> lstItems;
if (direction == FunelChartDirection.UP)
{
lstItems = items.OrderBy(p => p.Value).ToList();
}
else
{
lstItems = items.OrderByDescending(p => p.Value).ToList();
} List<RectangleF> lstRects = new List<RectangleF>();
List<GraphicsPath> lstPaths = new List<GraphicsPath>();
float maxValue = lstItems.Max(p => p.Value);
float dblSplitHeight = m_rectWorking.Height / lstItems.Count;
for (int i = ; i < lstItems.Count; i++)
{
FunelChartItem item = lstItems[i];
if (item.ValueColor == null || item.ValueColor == Color.Empty || item.ValueColor == Color.Transparent)
item.ValueColor = ControlHelper.Colors[i]; switch (alignment)
{
case FunelChartAlignment.Left:
lstRects.Add(new RectangleF(m_rectWorking.Left, m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
break;
case FunelChartAlignment.Center:
lstRects.Add(new RectangleF(m_rectWorking.Left + (m_rectWorking.Width - (item.Value / maxValue * m_rectWorking.Width)) / , m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
break;
case FunelChartAlignment.Right:
lstRects.Add(new RectangleF(m_rectWorking.Right - (item.Value / maxValue * m_rectWorking.Width), m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
break;
}
} for (int i = ; i < lstRects.Count; i++)
{
var rect = lstRects[i];
GraphicsPath path = new GraphicsPath();
List<PointF> lstPoints = new List<PointF>();
if (direction == FunelChartDirection.UP)
{
switch (alignment)
{
case FunelChartAlignment.Left:
lstPoints.Add(new PointF(rect.Left, rect.Top));
if (i != )
{
lstPoints.Add(new PointF(lstRects[i - ].Right, rect.Top));
}
break;
case FunelChartAlignment.Center:
if (i == )
{
lstPoints.Add(new PointF(rect.Left + rect.Width / , rect.Top));
}
else
{
lstPoints.Add(new PointF(lstRects[i - ].Left, rect.Top));
lstPoints.Add(new PointF(lstRects[i - ].Right, rect.Top));
}
break;
case FunelChartAlignment.Right:
if (i == )
{
lstPoints.Add(new PointF(rect.Right, rect.Top));
}
else
{
lstPoints.Add(new PointF(rect.Right - lstRects[i - ].Width, rect.Top));
lstPoints.Add(new PointF(rect.Right, rect.Top));
}
break;
}
lstPoints.Add(new PointF(rect.Right, rect.Bottom - intSplitWidth));
lstPoints.Add(new PointF(rect.Left, rect.Bottom - intSplitWidth));
}
else
{
lstPoints.Add(new PointF(rect.Left, rect.Top + intSplitWidth));
lstPoints.Add(new PointF(rect.Right, rect.Top + intSplitWidth));
switch (alignment)
{
case FunelChartAlignment.Left:
if (i == lstRects.Count - )
{
lstPoints.Add(new PointF(rect.Left, rect.Bottom));
}
else
{
lstPoints.Add(new PointF(lstRects[i + ].Right, rect.Bottom));
lstPoints.Add(new PointF(rect.Left, rect.Bottom));
}
break;
case FunelChartAlignment.Center:
if (i == lstRects.Count - )
{
lstPoints.Add(new PointF(rect.Left + rect.Width / , rect.Bottom));
}
else
{
lstPoints.Add(new PointF(lstRects[i + ].Right, rect.Bottom));
lstPoints.Add(new PointF(lstRects[i + ].Left, rect.Bottom));
}
break;
case FunelChartAlignment.Right:
if (i == lstRects.Count - )
{
lstPoints.Add(new PointF(rect.Right, rect.Bottom));
}
else
{
lstPoints.Add(new PointF(rect.Right, rect.Bottom));
lstPoints.Add(new PointF(lstRects[i + ].Left, rect.Bottom));
}
break;
}
}
path.AddLines(lstPoints.ToArray());
path.CloseAllFigures();
// g.DrawPath(new Pen(new SolidBrush(lstItems[i].ValueColor.Value)), path);
g.FillPath(new SolidBrush(lstItems[i].ValueColor.Value), path); //写字
if (itemTextAlign == FunelChartAlignment.Center)
{
g.DrawString(lstItems[i].Text + (ShowValue ? lstItems[i].Value.ToString("\n" + valueFormat) : ""), Font, new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? Color.White : lstItems[i].TextForeColor.Value), rect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
}
else if (itemTextAlign == FunelChartAlignment.Left)
{
g.DrawString(lstItems[i].Text + (ShowValue ? lstItems[i].Value.ToString("\n" + valueFormat) : ""), Font, new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value), new RectangleF(, rect.Top, rect.Left, rect.Height), new StringFormat() { Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Center });
g.DrawLine(new Pen(new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value)), rect.Left, rect.Top + rect.Height / , rect.Left + rect.Width / , rect.Top + rect.Height / );
}
else
{
g.DrawString(lstItems[i].Text + (ShowValue ? lstItems[i].Value.ToString("\n" + valueFormat) : ""), Font, new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value), new RectangleF(rect.Right, rect.Top, this.Width - rect.Right, rect.Height), new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center });
g.DrawLine(new Pen(new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value)), rect.Left + rect.Width / , rect.Top + rect.Height / , rect.Right, rect.Top + rect.Height / );
}
}
}

完整代码

 // ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-09-26
//
// ***********************************************************************
// <copyright file="UCFunnelChart.cs">
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel; namespace HZH_Controls.Controls
{
/// <summary>
/// Class UCFunnelChart.
/// Implements the <see cref="System.Windows.Forms.UserControl" />
/// </summary>
/// <seealso cref="System.Windows.Forms.UserControl" />
public class UCFunnelChart : UserControl
{
/// <summary>
/// The title
/// </summary>
private string title;
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置标题")]
public string Title
{
get { return title; }
set
{
title = value;
ResetTitleSize();
Invalidate();
}
} /// <summary>
/// The title font
/// </summary>
private Font titleFont = new Font("微软雅黑", );
/// <summary>
/// Gets or sets the title font.
/// </summary>
/// <value>The title font.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置标题字体")]
public Font TitleFont
{
get { return titleFont; }
set
{
titleFont = value;
ResetTitleSize();
Invalidate();
}
} /// <summary>
/// The title fore color
/// </summary>
private Color titleForeColor = Color.Black;
/// <summary>
/// Gets or sets the color of the title fore.
/// </summary>
/// <value>The color of the title fore.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置标题文字颜色")]
public Color TitleForeColor
{
get { return titleForeColor; }
set
{
titleForeColor = value;
Invalidate();
}
}
/// <summary>
/// The items
/// </summary>
private FunelChartItem[] items;
/// <summary>
/// Gets or sets the items.
/// </summary>
/// <value>The items.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置项目")]
public FunelChartItem[] Items
{
get { return items; }
set
{
items = value;
Invalidate();
}
} /// <summary>
/// The direction
/// </summary>
private FunelChartDirection direction = FunelChartDirection.UP;
/// <summary>
/// Gets or sets the direction.
/// </summary>
/// <value>The direction.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置方向")]
public FunelChartDirection Direction
{
get { return direction; }
set
{
direction = value;
Invalidate();
}
} /// <summary>
/// The alignment
/// </summary>
private FunelChartAlignment alignment = FunelChartAlignment.Center;
/// <summary>
/// Gets or sets the alignment.
/// </summary>
/// <value>The alignment.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置对齐方式")]
public FunelChartAlignment Alignment
{
get { return alignment; }
set
{
alignment = value;
Invalidate();
}
} /// <summary>
/// The item text align
/// </summary>
private FunelChartAlignment itemTextAlign = FunelChartAlignment.Center;
/// <summary>
/// Gets or sets the item text align.
/// </summary>
/// <value>The item text align.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置文字位置")]
public FunelChartAlignment ItemTextAlign
{
get { return itemTextAlign; }
set
{
itemTextAlign = value;
ResetWorkingRect();
Invalidate();
}
}
/// <summary>
/// The show value
/// </summary>
private bool showValue = false;
/// <summary>
/// Gets or sets a value indicating whether [show value].
/// </summary>
/// <value><c>true</c> if [show value]; otherwise, <c>false</c>.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置是否显示值")]
public bool ShowValue
{
get { return showValue; }
set
{
showValue = value;
Invalidate();
}
} /// <summary>
/// The value format
/// </summary>
private string valueFormat = "0.##";
/// <summary>
/// Gets or sets the value format.
/// </summary>
/// <value>The value format.</value>
[Browsable(true)]
[Category("自定义")]
[Description("获取或设置值格式化")]
public string ValueFormat
{
get { return valueFormat; }
set
{
valueFormat = value;
Invalidate();
}
} /// <summary>
/// The m rect working
/// </summary>
RectangleF m_rectWorking;
/// <summary>
/// The m title size
/// </summary>
SizeF m_titleSize = SizeF.Empty;
/// <summary>
/// The int split width
/// </summary>
int intSplitWidth = ; /// <summary>
/// Initializes a new instance of the <see cref="UCFunnelChart"/> class.
/// </summary>
public UCFunnelChart()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.FontChanged += UCFunnelChart_FontChanged;
Font = new Font("微软雅黑", ); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.SizeChanged += UCFunnelChart_SizeChanged;
Size = new System.Drawing.Size(, );
items = new FunelChartItem[];
if (ControlHelper.IsDesignMode())
{
items = new FunelChartItem[];
for (int i = ; i < ; i++)
{
items[i] = new FunelChartItem()
{
Text = "item" + i,
Value = * (i + )
};
}
}
} /// <summary>
/// Handles the FontChanged event of the UCFunnelChart control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void UCFunnelChart_FontChanged(object sender, EventArgs e)
{
ResetWorkingRect();
} /// <summary>
/// Handles the SizeChanged event of the UCFunnelChart control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void UCFunnelChart_SizeChanged(object sender, EventArgs e)
{
ResetWorkingRect();
} /// <summary>
/// Resets the working rect.
/// </summary>
private void ResetWorkingRect()
{
if (itemTextAlign == FunelChartAlignment.Center)
{
m_rectWorking = new RectangleF(, m_titleSize.Height == ? : (m_titleSize.Height + ), this.Width, this.Height - (m_titleSize.Height == ? : (m_titleSize.Height + )));
}
else if (itemTextAlign == FunelChartAlignment.Left)
{
float fltMax = ;
if (items != null && items.Length > )
{
using (Graphics g = this.CreateGraphics())
{
fltMax = items.Max(p => g.MeasureString(p.Text, Font).Width);
}
}
m_rectWorking = new RectangleF(fltMax, m_titleSize.Height == ? : (m_titleSize.Height + ), this.Width - fltMax, this.Height - (m_titleSize.Height == ? : (m_titleSize.Height + )));
}
else
{
float fltMax = ;
if (items != null && items.Length > )
{
using (Graphics g = this.CreateGraphics())
{
fltMax = items.Max(p => g.MeasureString(p.Text, Font).Width);
}
}
m_rectWorking = new RectangleF(, m_titleSize.Height == ? : (m_titleSize.Height + ), this.Width - fltMax, this.Height - (m_titleSize.Height == ? : (m_titleSize.Height + )));
}
} /// <summary>
/// Resets the size of the title.
/// </summary>
private void ResetTitleSize()
{
if (string.IsNullOrEmpty(title))
{
m_titleSize = SizeF.Empty;
}
else
{
using (Graphics g = this.CreateGraphics())
{
m_titleSize = g.MeasureString(title, titleFont);
m_titleSize.Height += ;
}
}
ResetWorkingRect();
} /// <summary>
/// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
/// </summary>
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh(); if (!string.IsNullOrEmpty(title))
{
g.DrawString(title, titleFont, new SolidBrush(titleForeColor), new RectangleF(, , this.Width, m_titleSize.Height), new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
} if (items == null || items.Length <= )
{
g.DrawString("没有数据", Font, new SolidBrush(Color.Black), this.m_rectWorking, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
return;
} List<FunelChartItem> lstItems;
if (direction == FunelChartDirection.UP)
{
lstItems = items.OrderBy(p => p.Value).ToList();
}
else
{
lstItems = items.OrderByDescending(p => p.Value).ToList();
} List<RectangleF> lstRects = new List<RectangleF>();
List<GraphicsPath> lstPaths = new List<GraphicsPath>();
float maxValue = lstItems.Max(p => p.Value);
float dblSplitHeight = m_rectWorking.Height / lstItems.Count;
for (int i = ; i < lstItems.Count; i++)
{
FunelChartItem item = lstItems[i];
if (item.ValueColor == null || item.ValueColor == Color.Empty || item.ValueColor == Color.Transparent)
item.ValueColor = ControlHelper.Colors[i]; switch (alignment)
{
case FunelChartAlignment.Left:
lstRects.Add(new RectangleF(m_rectWorking.Left, m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
break;
case FunelChartAlignment.Center:
lstRects.Add(new RectangleF(m_rectWorking.Left + (m_rectWorking.Width - (item.Value / maxValue * m_rectWorking.Width)) / , m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
break;
case FunelChartAlignment.Right:
lstRects.Add(new RectangleF(m_rectWorking.Right - (item.Value / maxValue * m_rectWorking.Width), m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
break;
}
} for (int i = ; i < lstRects.Count; i++)
{
var rect = lstRects[i];
GraphicsPath path = new GraphicsPath();
List<PointF> lstPoints = new List<PointF>();
if (direction == FunelChartDirection.UP)
{
switch (alignment)
{
case FunelChartAlignment.Left:
lstPoints.Add(new PointF(rect.Left, rect.Top));
if (i != )
{
lstPoints.Add(new PointF(lstRects[i - ].Right, rect.Top));
}
break;
case FunelChartAlignment.Center:
if (i == )
{
lstPoints.Add(new PointF(rect.Left + rect.Width / , rect.Top));
}
else
{
lstPoints.Add(new PointF(lstRects[i - ].Left, rect.Top));
lstPoints.Add(new PointF(lstRects[i - ].Right, rect.Top));
}
break;
case FunelChartAlignment.Right:
if (i == )
{
lstPoints.Add(new PointF(rect.Right, rect.Top));
}
else
{
lstPoints.Add(new PointF(rect.Right - lstRects[i - ].Width, rect.Top));
lstPoints.Add(new PointF(rect.Right, rect.Top));
}
break;
}
lstPoints.Add(new PointF(rect.Right, rect.Bottom - intSplitWidth));
lstPoints.Add(new PointF(rect.Left, rect.Bottom - intSplitWidth));
}
else
{
lstPoints.Add(new PointF(rect.Left, rect.Top + intSplitWidth));
lstPoints.Add(new PointF(rect.Right, rect.Top + intSplitWidth));
switch (alignment)
{
case FunelChartAlignment.Left:
if (i == lstRects.Count - )
{
lstPoints.Add(new PointF(rect.Left, rect.Bottom));
}
else
{
lstPoints.Add(new PointF(lstRects[i + ].Right, rect.Bottom));
lstPoints.Add(new PointF(rect.Left, rect.Bottom));
}
break;
case FunelChartAlignment.Center:
if (i == lstRects.Count - )
{
lstPoints.Add(new PointF(rect.Left + rect.Width / , rect.Bottom));
}
else
{
lstPoints.Add(new PointF(lstRects[i + ].Right, rect.Bottom));
lstPoints.Add(new PointF(lstRects[i + ].Left, rect.Bottom));
}
break;
case FunelChartAlignment.Right:
if (i == lstRects.Count - )
{
lstPoints.Add(new PointF(rect.Right, rect.Bottom));
}
else
{
lstPoints.Add(new PointF(rect.Right, rect.Bottom));
lstPoints.Add(new PointF(lstRects[i + ].Left, rect.Bottom));
}
break;
}
}
path.AddLines(lstPoints.ToArray());
path.CloseAllFigures();
// g.DrawPath(new Pen(new SolidBrush(lstItems[i].ValueColor.Value)), path);
g.FillPath(new SolidBrush(lstItems[i].ValueColor.Value), path); //写字
if (itemTextAlign == FunelChartAlignment.Center)
{
g.DrawString(lstItems[i].Text + (ShowValue ? lstItems[i].Value.ToString("\n" + valueFormat) : ""), Font, new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? Color.White : lstItems[i].TextForeColor.Value), rect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
}
else if (itemTextAlign == FunelChartAlignment.Left)
{
g.DrawString(lstItems[i].Text + (ShowValue ? lstItems[i].Value.ToString("\n" + valueFormat) : ""), Font, new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value), new RectangleF(, rect.Top, rect.Left, rect.Height), new StringFormat() { Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Center });
g.DrawLine(new Pen(new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value)), rect.Left, rect.Top + rect.Height / , rect.Left + rect.Width / , rect.Top + rect.Height / );
}
else
{
g.DrawString(lstItems[i].Text + (ShowValue ? lstItems[i].Value.ToString("\n" + valueFormat) : ""), Font, new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value), new RectangleF(rect.Right, rect.Top, this.Width - rect.Right, rect.Height), new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center });
g.DrawLine(new Pen(new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value)), rect.Left + rect.Width / , rect.Top + rect.Height / , rect.Right, rect.Top + rect.Height / );
}
}
}
}
}

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

(七十四)c#Winform自定义控件-金字塔图表的更多相关文章

  1. (七十)c#Winform自定义控件-饼状图

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  2. 第三百七十四节,Django+Xadmin打造上线标准的在线教育平台—创建课程app,在models.py文件生成4张表,课程表、课程章节表、课程视频表、课程资源表

    第三百七十四节,Django+Xadmin打造上线标准的在线教育平台—创建课程app,在models.py文件生成4张表,课程表.课程章节表.课程视频表.课程资源表 创建名称为app_courses的 ...

  3. “全栈2019”Java第七十四章:内部类与静态内部类相互嵌套

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  4. 孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4

    孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...

  5. (四十五)c#Winform自定义控件-水波图表

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  6. (七十三)c#Winform自定义控件-资源加载窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  7. (三十)c#Winform自定义控件-文本框(三)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (二十)c#Winform自定义控件-有后退的窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. (五十)c#Winform自定义控件-滑块

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

随机推荐

  1. IIS配置后本地访问正常,但外网无法访问

    很久没有部署IIS网站项目了,都有些手生了,这不今天就遇到了问题.首先确定的是,我的网站配置没有问题,因为内网访问正常.内网访问情况如下: 但是外网访问时确是这样的: 怎么回事儿呢?我就想是不是防火墙 ...

  2. NanoPi NEO2 学习笔记 1:安装系统、首次开机和一些设置

    初识NEO2 前几天搞到了一块NanoPi NEO2,A53的核心,512M内存,一个千兆网口,非常小的体积,质量也不错,非常满意,140元的价格可以买到这样一块ARM开发板也是非常划算了,非常适合低 ...

  3. 关于line-height 行高的一些理解和技巧

    大家都知道,如何设置文字垂直居中,也就是:设置line-height 和 外围盒子的高度height一致: 其实这里有个地方,是多余的,也就是height,设不设置都居中: 那么,行高是生产高度的? ...

  4. unity shader 纹理&透明效果

    1.纹理映射基础 (1)纹理映射通过(u,v)坐标实现.注意:这句话时博主当时面试一家外企被问到的问题. (2)添加纹理属性:——MainTex("Main Tex",2D)=&q ...

  5. JDBC主要API学习总结

    JDBC主要API学习 一.JDBC主要API简介 JDBC API 是一系列的接口,它使得应用程序能够进行数据库联接,执行SQL语句,并且得到返回结果. 二.Driver 接口 Java.sql.D ...

  6. Python机器学习笔记:不得不了解的机器学习知识点(2)

    之前一篇笔记: Python机器学习笔记:不得不了解的机器学习知识点(1) 1,什么样的资料集不适合用深度学习? 数据集太小,数据样本不足时,深度学习相对其它机器学习算法,没有明显优势. 数据集没有局 ...

  7. evevt

    EventEvent 是一个事件对象,当一个事件发生后,和当前事件发生相关的详细信息会被临时存储到一个指定的地方,也就是event对象,以方便我们在需要的时候调用. 在这一点上,非常类似于飞机当中的黑 ...

  8. 一个手写的Vue放大镜,复制即可使用

    一个手写的vue放大镜 组件使用less,请确保已安装loader 本组件为放大镜组件,传参列表为: width: 必传,设置放大镜的宽高(正方形),放大区域等同,放大倍数为2倍 picList:必传 ...

  9. vue实现手机号码的校验(防抖函数的应用场景)

    上一篇博文我们讲到了节流函数的应用场景,我们知道了节流函数可以用在模糊查询.scroller.onresize等场景:今天这篇我们来讲防抖函数的应用场景:: 通过上一篇博文的学习,我们知道了防抖函数的 ...

  10. SpringBoot + JPA问题汇总

    实体类有继承父类,但父类没有单独标明注解 异常表现 Caused by: org.hibernate.AnnotationException: No identifier specified for ...