前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
官网:https://www.hzhcontrols.cn
GitHub:https://github.com/kwwwvagaa/NetWinformControl
如果觉得写的还行,请点个 star 支持一下吧
目录
c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-CSDN博客
准备工作
GDI+画的,不了解GDI+可以百度了解下先
开始
添加一个用户控件,命名UCCrumbNavigation
提供属性
1 private Color m_navColor = Color.FromArgb(100, 100, 100);
2
3 public Color NavColor
4 {
5 get { return m_navColor; }
6 set
7 {
8 if (value == Color.Empty || value == Color.Transparent)
9 return;
10 m_navColor = value;
11 Refresh();
12 }
13 }
14
15
16 private string[] m_navigations = new string[] { "目录1", "目录2", "目录3" };
17 GraphicsPath[] m_paths;
18 public string[] Navigations
19 {
20 get { return m_navigations; }
21 set
22 {
23 m_navigations = value;
24 if (value == null)
25 m_paths = new GraphicsPath[0];
26 else
27 m_paths = new GraphicsPath[value.Length];
28 Refresh();
29 }
30 }
31
32 public override Font Font
33 {
34 get
35 {
36 return base.Font;
37 }
38 set
39 {
40 base.Font = value;
41 Refresh();
42 }
43 }
44
45 public override System.Drawing.Color ForeColor
46 {
47 get
48 {
49 return base.ForeColor;
50 }
51 set
52 {
53 base.ForeColor = value;
54 Refresh();
55 }
56 }
重绘
1 protected override void OnPaint(PaintEventArgs e)
2 {
3 base.OnPaint(e);
4
5 if (m_navigations != null && m_navigations.Length > 0)
6 {
7 var g = e.Graphics;
8 int intLastX = 0;
9 int intLength = m_navigations.Length;
10 for (int i = 0; i < m_navigations.Length; i++)
11 {
12 GraphicsPath path = new GraphicsPath();
13 string strText = m_navigations[i];
14 System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
15 int intTextWidth = (int)sizeF.Width + 1;
16 path.AddLine(new Point(intLastX + 1, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1));
17
18 //if (i != (intLength - 1))
19 //{
20 path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth + 10, this.Height / 2));
21 path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth + 10, this.Height / 2), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth - 1, this.Height - 1));
22 //}
23 //else
24 //{
25 // path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, this.Height - 1));
26 //}
27
28 path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, this.Height - 1), new Point(intLastX + 1, this.Height - 1));
29
30 if (i != 0)
31 {
32 path.AddLine(new Point(intLastX, this.Height - 1), new Point(intLastX + 1 + 10, this.Height / 2));
33 path.AddLine(new Point(intLastX + 1 + 10, this.Height / 2), new Point(intLastX + 1, 1));
34 }
35 else
36 {
37 path.AddLine(new Point(intLastX + 1, this.Height - 1), new Point(intLastX + 1, 1));
38 }
39 g.FillPath(new SolidBrush(m_navColor), path);
40
41 g.DrawString(strText, this.Font, new SolidBrush(this.ForeColor), new PointF(intLastX + 2 + (i == 0 ? 0 : 10), (this.Height - sizeF.Height) / 2 + 1));
42 m_paths[i] = path;
43 intLastX += ((i == 0 ? 0 : 10) + intTextWidth + (i == (intLength - 1) ? 0 : 10));
44 }
45 }
46
47 }
处理一下点击事件
1 void UCCrumbNavigation_MouseDown(object sender, MouseEventArgs e)
2 {
3 if (!DesignMode)
4 {
5 if (m_paths != null && m_paths.Length > 0)
6 {
7 for (int i = 0; i < m_paths.Length; i++)
8 {
9 if (m_paths[i].IsVisible(e.Location))
10 {
11 HZH_Controls.Forms.FrmTips.ShowTipsSuccess(this.FindForm(), m_navigations[i]);
12 }
13 }
14 }
15 }
16 }
完整代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace HZH_Controls.Controls
{
public partial class UCCrumbNavigation : UserControl
{
private Color m_navColor = Color.FromArgb(100, 100, 100);
public Color NavColor
{
get { return m_navColor; }
set
{
if (value == Color.Empty || value == Color.Transparent)
return;
m_navColor = value;
Refresh();
}
}
private string[] m_navigations = new string[] { "目录1", "目录2", "目录3" };
GraphicsPath[] m_paths;
public string[] Navigations
{
get { return m_navigations; }
set
{
m_navigations = value;
if (value == null)
m_paths = new GraphicsPath[0];
else
m_paths = new GraphicsPath[value.Length];
Refresh();
}
}
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
Refresh();
}
}
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
Refresh();
}
}
public UCCrumbNavigation()
{
InitializeComponent();
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.MouseDown += UCCrumbNavigation_MouseDown;
}
void UCCrumbNavigation_MouseDown(object sender, MouseEventArgs e)
{
if (!DesignMode)
{
if (m_paths != null && m_paths.Length > 0)
{
for (int i = 0; i < m_paths.Length; i++)
{
if (m_paths[i].IsVisible(e.Location))
{
HZH_Controls.Forms.FrmTips.ShowTipsSuccess(this.FindForm(), m_navigations[i]);
}
}
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (m_navigations != null && m_navigations.Length > 0)
{
var g = e.Graphics;
int intLastX = 0;
int intLength = m_navigations.Length;
for (int i = 0; i < m_navigations.Length; i++)
{
GraphicsPath path = new GraphicsPath();
string strText = m_navigations[i];
System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
int intTextWidth = (int)sizeF.Width + 1;
path.AddLine(new Point(intLastX + 1, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1));
//if (i != (intLength - 1))
//{
path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth + 10, this.Height / 2));
path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth + 10, this.Height / 2), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth - 1, this.Height - 1));
//}
//else
//{
// path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, this.Height - 1));
//}
path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, this.Height - 1), new Point(intLastX + 1, this.Height - 1));
if (i != 0)
{
path.AddLine(new Point(intLastX, this.Height - 1), new Point(intLastX + 1 + 10, this.Height / 2));
path.AddLine(new Point(intLastX + 1 + 10, this.Height / 2), new Point(intLastX + 1, 1));
}
else
{
path.AddLine(new Point(intLastX + 1, this.Height - 1), new Point(intLastX + 1, 1));
}
g.FillPath(new SolidBrush(m_navColor), path);
g.DrawString(strText, this.Font, new SolidBrush(this.ForeColor), new PointF(intLastX + 2 + (i == 0 ? 0 : 10), (this.Height - sizeF.Height) / 2 + 1));
m_paths[i] = path;
intLastX += ((i == 0 ? 0 : 10) + intTextWidth + (i == (intLength - 1) ? 0 : 10));
}
}
}
}
}
namespace HZH_Controls.Controls
{
partial class UCCrumbNavigation
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// UCCrumbNavigation
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.Cursor = System.Windows.Forms.Cursors.Hand;
this.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.MinimumSize = new System.Drawing.Size(0, 25);
this.Name = "UCCrumbNavigation";
this.Size = new System.Drawing.Size(220, 25);
this.ResumeLayout(false);
}
#endregion
}
}