(三十九)c#Winform自定义控件-面包屑导航
程序员文章站
2022-05-07 10:24:48
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492 目录 ......
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
开源地址:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
目录
准备工作
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 }
完整代码如下
1 using system; 2 using system.collections.generic; 3 using system.componentmodel; 4 using system.drawing; 5 using system.data; 6 using system.linq; 7 using system.text; 8 using system.windows.forms; 9 using system.drawing.drawing2d; 10 11 namespace hzh_controls.controls 12 { 13 public partial class uccrumbnavigation : usercontrol 14 { 15 private color m_navcolor = color.fromargb(100, 100, 100); 16 17 public color navcolor 18 { 19 get { return m_navcolor; } 20 set 21 { 22 if (value == color.empty || value == color.transparent) 23 return; 24 m_navcolor = value; 25 refresh(); 26 } 27 } 28 29 30 private string[] m_navigations = new string[] { "目录1", "目录2", "目录3" }; 31 graphicspath[] m_paths; 32 public string[] navigations 33 { 34 get { return m_navigations; } 35 set 36 { 37 m_navigations = value; 38 if (value == null) 39 m_paths = new graphicspath[0]; 40 else 41 m_paths = new graphicspath[value.length]; 42 refresh(); 43 } 44 } 45 46 public override font font 47 { 48 get 49 { 50 return base.font; 51 } 52 set 53 { 54 base.font = value; 55 refresh(); 56 } 57 } 58 59 public override system.drawing.color forecolor 60 { 61 get 62 { 63 return base.forecolor; 64 } 65 set 66 { 67 base.forecolor = value; 68 refresh(); 69 } 70 } 71 72 public uccrumbnavigation() 73 { 74 initializecomponent(); 75 this.setstyle(controlstyles.allpaintinginwmpaint, true); 76 this.setstyle(controlstyles.doublebuffer, true); 77 this.setstyle(controlstyles.resizeredraw, true); 78 this.setstyle(controlstyles.selectable, true); 79 this.setstyle(controlstyles.supportstransparentbackcolor, true); 80 this.setstyle(controlstyles.userpaint, true); 81 this.mousedown += uccrumbnavigation_mousedown; 82 } 83 84 void uccrumbnavigation_mousedown(object sender, mouseeventargs e) 85 { 86 if (!designmode) 87 { 88 if (m_paths != null && m_paths.length > 0) 89 { 90 for (int i = 0; i < m_paths.length; i++) 91 { 92 if (m_paths[i].isvisible(e.location)) 93 { 94 hzh_controls.forms.frmtips.showtipssuccess(this.findform(), m_navigations[i]); 95 } 96 } 97 } 98 } 99 } 100 101 protected override void onpaint(painteventargs e) 102 { 103 base.onpaint(e); 104 105 if (m_navigations != null && m_navigations.length > 0) 106 { 107 var g = e.graphics; 108 int intlastx = 0; 109 int intlength = m_navigations.length; 110 for (int i = 0; i < m_navigations.length; i++) 111 { 112 graphicspath path = new graphicspath(); 113 string strtext = m_navigations[i]; 114 system.drawing.sizef sizef = g.measurestring(strtext.replace(" ", "a"), font); 115 int inttextwidth = (int)sizef.width + 1; 116 path.addline(new point(intlastx + 1, 1), new point(intlastx + 1 + (i == 0 ? 0 : 10) + inttextwidth, 1)); 117 118 //if (i != (intlength - 1)) 119 //{ 120 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)); 121 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)); 122 //} 123 //else 124 //{ 125 // path.addline(new point(intlastx + 1 + (i == 0 ? 0 : 10) + inttextwidth, 1), new point(intlastx + 1 + (i == 0 ? 0 : 10) + inttextwidth, this.height - 1)); 126 //} 127 128 path.addline(new point(intlastx + 1 + (i == 0 ? 0 : 10) + inttextwidth, this.height - 1), new point(intlastx + 1, this.height - 1)); 129 130 if (i != 0) 131 { 132 path.addline(new point(intlastx, this.height - 1), new point(intlastx + 1 + 10, this.height / 2)); 133 path.addline(new point(intlastx + 1 + 10, this.height / 2), new point(intlastx + 1, 1)); 134 } 135 else 136 { 137 path.addline(new point(intlastx + 1, this.height - 1), new point(intlastx + 1, 1)); 138 } 139 g.fillpath(new solidbrush(m_navcolor), path); 140 141 g.drawstring(strtext, this.font, new solidbrush(this.forecolor), new pointf(intlastx + 2 + (i == 0 ? 0 : 10), (this.height - sizef.height) / 2 + 1)); 142 m_paths[i] = path; 143 intlastx += ((i == 0 ? 0 : 10) + inttextwidth + (i == (intlength - 1) ? 0 : 10)); 144 } 145 } 146 147 } 148 } 149 }
1 namespace hzh_controls.controls 2 { 3 partial class uccrumbnavigation 4 { 5 /// <summary> 6 /// 必需的设计器变量。 7 /// </summary> 8 private system.componentmodel.icontainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的资源。 12 /// </summary> 13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14 protected override void dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.dispose(); 19 } 20 base.dispose(disposing); 21 } 22 23 #region 组件设计器生成的代码 24 25 /// <summary> 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// </summary> 29 private void initializecomponent() 30 { 31 this.suspendlayout(); 32 // 33 // uccrumbnavigation 34 // 35 this.autoscalemode = system.windows.forms.autoscalemode.none; 36 this.cursor = system.windows.forms.cursors.hand; 37 this.forecolor = system.drawing.color.fromargb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))); 38 this.minimumsize = new system.drawing.size(0, 25); 39 this.name = "uccrumbnavigation"; 40 this.size = new system.drawing.size(220, 25); 41 this.resumelayout(false); 42 43 } 44 45 #endregion 46 47 48 } 49 }
用处及效果
最后的话
如果你喜欢的话,请到 点个星 星吧
上一篇: 十二连城现在在哪里?十二连城有什么历史传说和故事?
下一篇: 唐玄宗为什么会相信安禄山?真相是什么