/// /// publicpartialclassUCBtnsGroup:UserControl { /// ///选中改变事件 /// [Description("选中改变事件"),Category("自定义")] publiceventEventHandlerSelectedItemChanged; /// ///Themdatasource /// privateDictionarym_dataSource=newDictionary(); /// ///数据源 /// ///Thedatasource. [Description("数据源"),Category("自定义")] publicDictionaryDataSource { get{returnm_dataSource;} set { m_dataSource=value; Reload(); } } /// ///Themselectitem /// privateListm_selectItem=newList(); /// ///选中项 /// ///Theselectitem. [Description("选中项"),Category("自定义")] publicListSelectItem { get{returnm_selectItem;} set { m_selectItem=value; if(m_selectItem==null) m_selectItem=newList(); SetSelected(); } } /// ///Themismultiple /// privateboolm_isMultiple=false; /// ///是否多选 /// ///trueifthisinstanceismultiple;otherwise,false. [Description("是否多选"),Category("自定义")] publicboolIsMultiple { get{returnm_isMultiple;} set{m_isMultiple=value;} } /// ///Initializesanewinstanceoftheclass. /// publicUCBtnsGroup() { InitializeComponent(); } /// ///Reloadsthisinstance. /// privatevoidReload() { try { ControlHelper.FreezeControl(flowLayoutPanel1,true); this.flowLayoutPanel1.Controls.Clear(); if(DataSource!=null) { foreach(variteminDataSource) { UCBtnExtbtn=newUCBtnExt(); btn.BackColor=System.Drawing.Color.Transparent; btn.BtnBackColor=System.Drawing.Color.White; btn.BtnFont=newSystem.Drawing.Font("微软雅黑",10F); btn.BtnForeColor=System.Drawing.Color.Gray; btn.BtnText=item.Value; btn.ConerRadius=5; btn.Cursor=System.Windows.Forms.Cursors.Hand; btn.FillColor=System.Drawing.Color.White; btn.Font=newSystem.Drawing.Font("微软雅黑",15F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Pixel); btn.IsRadius=true; btn.IsShowRect=true; btn.IsShowTips=false; btn.Location=newSystem.Drawing.Point(5,5); btn.Margin=newSystem.Windows.Forms.Padding(5); btn.Name=item.Key; btn.RectColor=System.Drawing.Color.FromArgb(224,224,224); btn.RectWidth=1; btn.Size=newSystem.Drawing.Size(72,38); btn.TabStop=false; btn.BtnClick=btn_BtnClick; this.flowLayoutPanel1.Controls.Add(btn); } } } finally { ControlHelper.FreezeControl(flowLayoutPanel1,false); } SetSelected(); } /// ///HandlestheBtnClickeventofthebtncontrol. /// ///Thesourceoftheevent. ///Theinstancecontainingtheeventdata. voidbtn_BtnClick(objectsender,EventArgse) { varbtn=senderasUCBtnExt; if(m_selectItem.Contains(btn.Name)) { btn.RectColor=System.Drawing.Color.FromArgb(224,224,224); m_selectItem.Remove(btn.Name); } else { if(!m_isMultiple) { foreach(variteminm_selectItem) { varlst=this.flowLayoutPanel1.Controls.Find(item,false); if(lst.Length==1) { var_btn=lst[0]asUCBtnExt; _btn.RectColor=System.Drawing.Color.FromArgb(224,224,224); } } m_selectItem.Clear(); } btn.RectColor=System.Drawing.Color.FromArgb(255,77,59); m_selectItem.Add(btn.Name); } if(SelectedItemChanged!=null) SelectedItemChanged(this,e); } /// ///Setstheselected. /// privatevoidSetSelected() { if(m_selectItem!=null&&m_selectItem.Count>0&&DataSource!=null&&DataSource.Count>0) { try { ControlHelper.FreezeControl(flowLayoutPanel1,true); if(m_isMultiple) { foreach(variteminm_selectItem) { varlst=this.flowLayoutPanel1.Controls.Find(item,false); if(lst.Length==1) { varbtn=lst[0]asUCBtnExt; btn.RectColor=System.Drawing.Color.FromArgb(255,77,59); } } } else { UCBtnExtbtn=null; foreach(variteminm_selectItem) { varlst=this.flowLayoutPanel1.Controls.Find(item,false); if(lst.Length==1) { btn=lst[0]asUCBtnExt; break; } } if(btn!=null) { m_selectItem=newList(){btn.Name}; btn.RectColor=System.Drawing.Color.FromArgb(255,77,59); } } } finally { ControlHelper.FreezeControl(flowLayoutPanel1,false); } } } }}usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Drawing.Drawing2D;namespaceHZH_Controls.Controls{ /// ///ClassUCStep. ///Implementsthe /// /// [DefaultEvent("IndexChecked")] publicpartialclassUCStep:UserControl { /// ///Occurswhen[indexchecked]. /// [Description("步骤更改事件"),Category("自定义")] publiceventEventHandlerIndexChecked; /// ///Themstepbackcolor /// privateColorm_stepBackColor=Color.FromArgb(189,189,189); /// ///步骤背景色 /// ///Thecolorofthestepback. [Description("步骤背景色"),Category("自定义")] publicColorStepBackColor { get{returnm_stepBackColor;} set { m_stepBackColor=value; Refresh(); } } /// ///Themstepforecolor /// privateColorm_stepForeColor=Color.FromArgb(255,77,59); /// ///步骤前景色 /// ///Thecolorofthestepfore. [Description("步骤前景色"),Category("自定义")] publicColorStepForeColor { get{returnm_stepForeColor;} set { m_stepForeColor=value; Refresh(); } } /// ///Themstepfontcolor /// privateColorm_stepFontColor=Color.White; /// ///步骤文字颜色 /// ///Thecolorofthestepfont. [Description("步骤文字景色"),Category("自定义")] publicColorStepFontColor { get{returnm_stepFontColor;} set { m_stepFontColor=value; Refresh(); } } /// ///Themstepwidth /// privateintm_stepWidth=35; /// ///步骤宽度 /// ///Thewidthofthestep. [Description("步骤宽度景色"),Category("自定义")] publicintStepWidth { get{returnm_stepWidth;} set { m_stepWidth=value; Refresh(); } } /// ///Themsteps /// privatestring[]m_steps=newstring[]{"step1","step2","step3"}; /// ///Getsorsetsthesteps. /// ///Thesteps. [Description("步骤"),Category("自定义")] publicstring[]Steps { get{returnm_steps;} set { if(m_steps==null||m_steps.Length<=1) return; m_steps=value; Refresh(); } } /// ///Themstepindex /// privateintm_stepIndex=0; /// ///Getsorsetstheindexofthestep. /// ///Theindexofthestep. [Description("步骤位置"),Category("自定义")] publicintStepIndex { get{returnm_stepIndex;} set { if(value>Steps.Length) return; m_stepIndex=value; Refresh(); if(IndexChecked!=null) { IndexChecked(this,null); } } } /// ///Themlinewidth /// privateintm_lineWidth=2; /// ///Getsorsetsthewidthoftheline. /// ///Thewidthoftheline. [Description("连接线宽度,最小2"),Category("自定义")] publicintLineWidth { get{returnm_lineWidth;} set { if(value<2) return; m_lineWidth=value; Refresh(); } } /// ///Themimgcompleted /// privateImagem_imgCompleted=null; /// ///Getsorsetstheimgcompleted. /// ///Theimgcompleted. [Description("已完成步骤图片,当不为空时,已完成步骤将不再显示数字,建议24*24大小"),Category("自定义")] publicImageImgCompleted { get{returnm_imgCompleted;} set { m_imgCompleted=value; Refresh(); } } /// ///ThemLSTcacherect /// Listm_lstCacheRect=newList(); /// ///Initializesanewinstanceoftheclass. /// publicUCStep() { 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=UCStep_MouseDown; } /// ///HandlestheMouseDowneventoftheUCStepcontrol. /// ///Thesourceoftheevent. ///Theinstancecontainingtheeventdata. voidUCStep_MouseDown(objectsender,MouseEventArgse) { varindex=m_lstCacheRect.FindIndex(p=>p.Contains(e.Location)); if(index>=0) { StepIndex=index1; } } /// ///引发事件。 /// ///包含事件数据的。 protectedoverridevoidOnPaint(PaintEventArgse) { base.OnPaint(e); varg=e.Graphics; g.SetGDIHigh(); if(m_steps!=null&&m_steps.Length>0) { System.Drawing.SizeFsizeFirst=g.MeasureString(m_steps[0],this.Font); inty=(this.Height-m_stepWidth-10-(int)sizeFirst.Height)/2; if(y<0) y=0; intintTxtY=ym_stepWidth10; intintLeft=0; if(sizeFirst.Width>m_stepWidth) { intLeft=(int)(sizeFirst.Width-m_stepWidth)/21; } intintRight=0; System.Drawing.SizeFsizeEnd=g.MeasureString(m_steps[m_steps.Length-1],this.Font); if(sizeEnd.Width>m_stepWidth) { intRight=(int)(sizeEnd.Width-m_stepWidth)/21; } intintSplitWidth=20; intSplitWidth=(this.Width-m_steps.Length-(m_steps.Length*m_stepWidth)-intRight-intLeft)/(m_steps.Length-1); if(intSplitWidth<20) intSplitWidth=20; m_lstCacheRect=newList(); for(inti=0;ii) { g.FillEllipse(newSolidBrush(m_stepForeColor),newRectangle(newPoint(intLefti*(m_stepWidthintSplitWidth)2,y2),newSize(m_stepWidth-4,m_stepWidth-4))); } if(m_stepIndex>i&&m_imgCompleted!=null) { g.DrawImage(m_imgCompleted,newRectangle(newPoint((intLefti*(m_stepWidthintSplitWidth)(m_stepWidth-24)/2),y(m_stepWidth-24)/2),newSize(24,24)),0,0,m_imgCompleted.Width,m_imgCompleted.Height,GraphicsUnit.Pixel,null); } else { System.Drawing.SizeF_numSize=g.MeasureString((i1).ToString(),this.Font); g.DrawString((i1).ToString(),Font,newSolidBrush(m_stepFontColor),newPoint(intLefti*(m_stepWidthintSplitWidth)(m_stepWidth-(int)_numSize.Width)/21,y(m_stepWidth-(int)_numSize.Height)/21)); } #endregion System.Drawing.SizeFsizeTxt=g.MeasureString(m_steps[i],this.Font); g.DrawString(m_steps[i],Font,newSolidBrush(m_stepIndex>i?m_stepForeColor:m_stepBackColor),newPoint(intLefti*(m_stepWidthintSplitWidth)(m_stepWidth-(int)sizeTxt.Width)/21,intTxtY)); } for(inti=0;ii) { if(i!=m_steps.Length-1) { if(m_stepIndex==i1) { g.DrawLine(newPen(m_stepForeColor,m_lineWidth),newPoint(intLefti*(m_stepWidthintSplitWidth)m_stepWidth-3,y((m_stepWidth)/2)),newPoint(intLefti*(m_stepWidthintSplitWidth)m_stepWidthintSplitWidth/2,y((m_stepWidth)/2))); g.DrawLine(newPen(m_stepBackColor,m_lineWidth),newPoint(intLefti*(m_stepWidthintSplitWidth)m_stepWidthintSplitWidth/2,y((m_stepWidth)/2)),newPoint(intLeft(i1)*(m_stepWidthintSplitWidth)10,y((m_stepWidth)/2))); } else { g.DrawLine(newPen(m_stepForeColor,m_lineWidth),newPoint(intLefti*(m_stepWidthintSplitWidth)m_stepWidth-3,y((m_stepWidth)/2)),newPoint(intLeft(i1)*(m_stepWidthintSplitWidth)10,y((m_stepWidth)/2))); } } } else { if(i!=m_steps.Length-1) { g.DrawLine(newPen(m_stepBackColor,m_lineWidth),newPoint(intLefti*(m_stepWidthintSplitWidth)m_stepWidth-3,y((m_stepWidth)/2)),newPoint(intLeft(i1)*(m_stepWidthintSplitWidth)10,y((m_stepWidth)/2))); } } } } } }}-IMDN开发者社群-imdn.cn">
/// /// publicpartialclassUCBtnsGroup:UserControl { /// ///选中改变事件 /// [Description("选中改变事件"),Category("自定义")] publiceventEventHandlerSelectedItemChanged; /// ///Themdatasource /// privateDictionarym_dataSource=newDictionary(); /// ///数据源 /// ///Thedatasource. [Description("数据源"),Category("自定义")] publicDictionaryDataSource { get{returnm_dataSource;} set { m_dataSource=value; Reload(); } } /// ///Themselectitem /// privateListm_selectItem=newList(); /// ///选中项 /// ///Theselectitem. [Description("选中项"),Category("自定义")] publicListSelectItem { get{returnm_selectItem;} set { m_selectItem=value; if(m_selectItem==null) m_selectItem=newList(); SetSelected(); } } /// ///Themismultiple /// privateboolm_isMultiple=false; /// ///是否多选 /// ///trueifthisinstanceismultiple;otherwise,false. [Description("是否多选"),Category("自定义")] publicboolIsMultiple { get{returnm_isMultiple;} set{m_isMultiple=value;} } /// ///Initializesanewinstanceoftheclass. /// publicUCBtnsGroup() { InitializeComponent(); } /// ///Reloadsthisinstance. /// privatevoidReload() { try { ControlHelper.FreezeControl(flowLayoutPanel1,true); this.flowLayoutPanel1.Controls.Clear(); if(DataSource!=null) { foreach(variteminDataSource) { UCBtnExtbtn=newUCBtnExt(); btn.BackColor=System.Drawing.Color.Transparent; btn.BtnBackColor=System.Drawing.Color.White; btn.BtnFont=newSystem.Drawing.Font("微软雅黑",10F); btn.BtnForeColor=System.Drawing.Color.Gray; btn.BtnText=item.Value; btn.ConerRadius=5; btn.Cursor=System.Windows.Forms.Cursors.Hand; btn.FillColor=System.Drawing.Color.White; btn.Font=newSystem.Drawing.Font("微软雅黑",15F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Pixel); btn.IsRadius=true; btn.IsShowRect=true; btn.IsShowTips=false; btn.Location=newSystem.Drawing.Point(5,5); btn.Margin=newSystem.Windows.Forms.Padding(5); btn.Name=item.Key; btn.RectColor=System.Drawing.Color.FromArgb(224,224,224); btn.RectWidth=1; btn.Size=newSystem.Drawing.Size(72,38); btn.TabStop=false; btn.BtnClick=btn_BtnClick; this.flowLayoutPanel1.Controls.Add(btn); } } } finally { ControlHelper.FreezeControl(flowLayoutPanel1,false); } SetSelected(); } /// ///HandlestheBtnClickeventofthebtncontrol. /// ///Thesourceoftheevent. ///Theinstancecontainingtheeventdata. voidbtn_BtnClick(objectsender,EventArgse) { varbtn=senderasUCBtnExt; if(m_selectItem.Contains(btn.Name)) { btn.RectColor=System.Drawing.Color.FromArgb(224,224,224); m_selectItem.Remove(btn.Name); } else { if(!m_isMultiple) { foreach(variteminm_selectItem) { varlst=this.flowLayoutPanel1.Controls.Find(item,false); if(lst.Length==1) { var_btn=lst[0]asUCBtnExt; _btn.RectColor=System.Drawing.Color.FromArgb(224,224,224); } } m_selectItem.Clear(); } btn.RectColor=System.Drawing.Color.FromArgb(255,77,59); m_selectItem.Add(btn.Name); } if(SelectedItemChanged!=null) SelectedItemChanged(this,e); } /// ///Setstheselected. /// privatevoidSetSelected() { if(m_selectItem!=null&&m_selectItem.Count>0&&DataSource!=null&&DataSource.Count>0) { try { ControlHelper.FreezeControl(flowLayoutPanel1,true); if(m_isMultiple) { foreach(variteminm_selectItem) { varlst=this.flowLayoutPanel1.Controls.Find(item,false); if(lst.Length==1) { varbtn=lst[0]asUCBtnExt; btn.RectColor=System.Drawing.Color.FromArgb(255,77,59); } } } else { UCBtnExtbtn=null; foreach(variteminm_selectItem) { varlst=this.flowLayoutPanel1.Controls.Find(item,false); if(lst.Length==1) { btn=lst[0]asUCBtnExt; break; } } if(btn!=null) { m_selectItem=newList(){btn.Name}; btn.RectColor=System.Drawing.Color.FromArgb(255,77,59); } } } finally { ControlHelper.FreezeControl(flowLayoutPanel1,false); } } } }}usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Drawing.Drawing2D;namespaceHZH_Controls.Controls{ /// ///ClassUCStep. ///Implementsthe /// /// [DefaultEvent("IndexChecked")] publicpartialclassUCStep:UserControl { /// ///Occurswhen[indexchecked]. /// [Description("步骤更改事件"),Category("自定义")] publiceventEventHandlerIndexChecked; /// ///Themstepbackcolor /// privateColorm_stepBackColor=Color.FromArgb(189,189,189); /// ///步骤背景色 /// ///Thecolorofthestepback. [Description("步骤背景色"),Category("自定义")] publicColorStepBackColor { get{returnm_stepBackColor;} set { m_stepBackColor=value; Refresh(); } } /// ///Themstepforecolor /// privateColorm_stepForeColor=Color.FromArgb(255,77,59); /// ///步骤前景色 /// ///Thecolorofthestepfore. [Description("步骤前景色"),Category("自定义")] publicColorStepForeColor { get{returnm_stepForeColor;} set { m_stepForeColor=value; Refresh(); } } /// ///Themstepfontcolor /// privateColorm_stepFontColor=Color.White; /// ///步骤文字颜色 /// ///Thecolorofthestepfont. [Description("步骤文字景色"),Category("自定义")] publicColorStepFontColor { get{returnm_stepFontColor;} set { m_stepFontColor=value; Refresh(); } } /// ///Themstepwidth /// privateintm_stepWidth=35; /// ///步骤宽度 /// ///Thewidthofthestep. [Description("步骤宽度景色"),Category("自定义")] publicintStepWidth { get{returnm_stepWidth;} set { m_stepWidth=value; Refresh(); } } /// ///Themsteps /// privatestring[]m_steps=newstring[]{"step1","step2","step3"}; /// ///Getsorsetsthesteps. /// ///Thesteps. [Description("步骤"),Category("自定义")] publicstring[]Steps { get{returnm_steps;} set { if(m_steps==null||m_steps.Length<=1) return; m_steps=value; Refresh(); } } /// ///Themstepindex /// privateintm_stepIndex=0; /// ///Getsorsetstheindexofthestep. /// ///Theindexofthestep. [Description("步骤位置"),Category("自定义")] publicintStepIndex { get{returnm_stepIndex;} set { if(value>Steps.Length) return; m_stepIndex=value; Refresh(); if(IndexChecked!=null) { IndexChecked(this,null); } } } /// ///Themlinewidth /// privateintm_lineWidth=2; /// ///Getsorsetsthewidthoftheline. /// ///Thewidthoftheline. [Description("连接线宽度,最小2"),Category("自定义")] publicintLineWidth { get{returnm_lineWidth;} set { if(value<2) return; m_lineWidth=value; Refresh(); } } /// ///Themimgcompleted /// privateImagem_imgCompleted=null; /// ///Getsorsetstheimgcompleted. /// ///Theimgcompleted. [Description("已完成步骤图片,当不为空时,已完成步骤将不再显示数字,建议24*24大小"),Category("自定义")] publicImageImgCompleted { get{returnm_imgCompleted;} set { m_imgCompleted=value; Refresh(); } } /// ///ThemLSTcacherect /// Listm_lstCacheRect=newList(); /// ///Initializesanewinstanceoftheclass. /// publicUCStep() { 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=UCStep_MouseDown; } /// ///HandlestheMouseDowneventoftheUCStepcontrol. /// ///Thesourceoftheevent. ///Theinstancecontainingtheeventdata. voidUCStep_MouseDown(objectsender,MouseEventArgse) { varindex=m_lstCacheRect.FindIndex(p=>p.Contains(e.Location)); if(index>=0) { StepIndex=index1; } } /// ///引发事件。 /// ///包含事件数据的。 protectedoverridevoidOnPaint(PaintEventArgse) { base.OnPaint(e); varg=e.Graphics; g.SetGDIHigh(); if(m_steps!=null&&m_steps.Length>0) { System.Drawing.SizeFsizeFirst=g.MeasureString(m_steps[0],this.Font); inty=(this.Height-m_stepWidth-10-(int)sizeFirst.Height)/2; if(y<0) y=0; intintTxtY=ym_stepWidth10; intintLeft=0; if(sizeFirst.Width>m_stepWidth) { intLeft=(int)(sizeFirst.Width-m_stepWidth)/21; } intintRight=0; System.Drawing.SizeFsizeEnd=g.MeasureString(m_steps[m_steps.Length-1],this.Font); if(sizeEnd.Width>m_stepWidth) { intRight=(int)(sizeEnd.Width-m_stepWidth)/21; } intintSplitWidth=20; intSplitWidth=(this.Width-m_steps.Length-(m_steps.Length*m_stepWidth)-intRight-intLeft)/(m_steps.Length-1); if(intSplitWidth<20) intSplitWidth=20; m_lstCacheRect=newList(); for(inti=0;ii) { g.FillEllipse(newSolidBrush(m_stepForeColor),newRectangle(newPoint(intLefti*(m_stepWidthintSplitWidth)2,y2),newSize(m_stepWidth-4,m_stepWidth-4))); } if(m_stepIndex>i&&m_imgCompleted!=null) { g.DrawImage(m_imgCompleted,newRectangle(newPoint((intLefti*(m_stepWidthintSplitWidth)(m_stepWidth-24)/2),y(m_stepWidth-24)/2),newSize(24,24)),0,0,m_imgCompleted.Width,m_imgCompleted.Height,GraphicsUnit.Pixel,null); } else { System.Drawing.SizeF_numSize=g.MeasureString((i1).ToString(),this.Font); g.DrawString((i1).ToString(),Font,newSolidBrush(m_stepFontColor),newPoint(intLefti*(m_stepWidthintSplitWidth)(m_stepWidth-(int)_numSize.Width)/21,y(m_stepWidth-(int)_numSize.Height)/21)); } #endregion System.Drawing.SizeFsizeTxt=g.MeasureString(m_steps[i],this.Font); g.DrawString(m_steps[i],Font,newSolidBrush(m_stepIndex>i?m_stepForeColor:m_stepBackColor),newPoint(intLefti*(m_stepWidthintSplitWidth)(m_stepWidth-(int)sizeTxt.Width)/21,intTxtY)); } for(inti=0;ii) { if(i!=m_steps.Length-1) { if(m_stepIndex==i1) { g.DrawLine(newPen(m_stepForeColor,m_lineWidth),newPoint(intLefti*(m_stepWidthintSplitWidth)m_stepWidth-3,y((m_stepWidth)/2)),newPoint(intLefti*(m_stepWidthintSplitWidth)m_stepWidthintSplitWidth/2,y((m_stepWidth)/2))); g.DrawLine(newPen(m_stepBackColor,m_lineWidth),newPoint(intLefti*(m_stepWidthintSplitWidth)m_stepWidthintSplitWidth/2,y((m_stepWidth)/2)),newPoint(intLeft(i1)*(m_stepWidthintSplitWidth)10,y((m_stepWidth)/2))); } else { g.DrawLine(newPen(m_stepForeColor,m_lineWidth),newPoint(intLefti*(m_stepWidthintSplitWidth)m_stepWidth-3,y((m_stepWidth)/2)),newPoint(intLeft(i1)*(m_stepWidthintSplitWidth)10,y((m_stepWidth)/2))); } } } else { if(i!=m_steps.Length-1) { g.DrawLine(newPen(m_stepBackColor,m_lineWidth),newPoint(intLefti*(m_stepWidthintSplitWidth)m_stepWidth-3,y((m_stepWidth)/2)),newPoint(intLeft(i1)*(m_stepWidthintSplitWidth)10,y((m_stepWidth)/2))); } } } } } }} - IMDN开发者社群-imdn.cn">
于 2020-12-11 发布
0 315
c#Winform自定义控件-基类控件,按钮,有图标的按钮,选择按钮组,复选框,单选框,进度条,分割线,树,横向列表,列表,分页控件,导航菜单,键盘,文本框,表格,日期控件,Tab页,下拉框,步骤控件,有标题的面板,圆形进度条,面包屑导航,开关等等。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;namespace HZH_Controls.Controls{ /// /// Class UCBtnsGroup. /// Implements the /// /// public partial class UCBtnsGroup : UserControl { /// /// 选中改变事件 /// [Description("选中改变事件"), Category("自定义")] public event EventHandler SelectedItemChanged; /// /// The m data source /// private Dictionary m_dataSource = new Dictionary(); /// /// 数据源 /// /// The data source. [Description("数据源"), Category("自定义")] public Dictionary DataSource { get { return m_dataSource; } set { m_dataSource = value; Reload(); } } /// /// The m select item /// private List m_selectItem = new List(); /// /// 选中项 /// /// The select item. [Description("选中项"), Category("自定义")] public List SelectItem { get { return m_selectItem; } set { m_selectItem = value; if (m_selectItem == null) m_selectItem = new List(); SetSelected(); } } /// /// The m is multiple /// private bool m_isMultiple = false; /// /// 是否多选 /// /// true if this instance is multiple; otherwise, false. [Description("是否多选"), Category("自定义")] public bool IsMultiple { get { return m_isMultiple; } set { m_isMultiple = value; } } /// /// Initializes a new instance of the class. /// public UCBtnsGroup() { InitializeComponent(); } /// /// Reloads this instance. /// private void Reload() { try { ControlHelper.FreezeControl(flowLayoutPanel1, true); this.flowLayoutPanel1.Controls.Clear(); if (DataSource != null) { foreach (var item in DataSource) { UCBtnExt btn = new UCBtnExt(); btn.BackColor = System.Drawing.Color.Transparent; btn.BtnBackColor = System.Drawing.Color.White; btn.BtnFont = new System.Drawing.Font("微软雅黑", 10F); btn.BtnForeColor = System.Drawing.Color.Gray; btn.BtnText = item.Value; btn.ConerRadius = 5; btn.Cursor = System.Windows.Forms.Cursors.Hand; btn.FillColor = System.Drawing.Color.White; btn.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); btn.IsRadius = true; btn.IsShowRect = true; btn.IsShowTips = false; btn.Location = new System.Drawing.Point(5, 5); btn.Margin = new System.Windows.Forms.Padding(5); btn.Name = item.Key; btn.RectColor = System.Drawing.Color.FromArgb(224, 224, 224); btn.RectWidth = 1; btn.Size = new System.Drawing.Size(72, 38); btn.TabStop = false; btn.BtnClick = btn_BtnClick; this.flowLayoutPanel1.Controls.Add(btn); } } } finally { ControlHelper.FreezeControl(flowLayoutPanel1, false); } SetSelected(); } /// /// Handles the BtnClick event of the btn control. /// /// The source of the event. /// The instance containing the event data. void btn_BtnClick(object sender, EventArgs e) { var btn = sender as UCBtnExt; if (m_selectItem.Contains(btn.Name)) { btn.RectColor = System.Drawing.Color.FromArgb(224, 224, 224); m_selectItem.Remove(btn.Name); } else { if (!m_isMultiple) { foreach (var item in m_selectItem) { var lst = this.flowLayoutPanel1.Controls.Find(item, false); if (lst.Length == 1) { var _btn = lst[0] as UCBtnExt; _btn.RectColor = System.Drawing.Color.FromArgb(224, 224, 224); } } m_selectItem.Clear(); } btn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59); m_selectItem.Add(btn.Name); } if (SelectedItemChanged != null) SelectedItemChanged(this, e); } /// /// Sets the selected. /// private void SetSelected() { if (m_selectItem != null && m_selectItem.Count > 0 && DataSource != null && DataSource.Count > 0) { try { ControlHelper.FreezeControl(flowLayoutPanel1, true); if (m_isMultiple) { foreach (var item in m_selectItem) { var lst = this.flowLayoutPanel1.Controls.Find(item, false); if (lst.Length == 1) { var btn = lst[0] as UCBtnExt; btn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59); } } } else { UCBtnExt btn = null; foreach (var item in m_selectItem) { var lst = this.flowLayoutPanel1.Controls.Find(item, false); if (lst.Length == 1) { btn = lst[0] as UCBtnExt; break; } } if (btn != null) { m_selectItem = new List() { btn.Name }; btn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59); } } } finally { ControlHelper.FreezeControl(flowLayoutPanel1, false); } } } }}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{ /// /// Class UCStep. /// Implements the /// /// [DefaultEvent("IndexChecked")] public partial class UCStep : UserControl { /// /// Occurs when [index checked]. /// [Description("步骤更改事件"), Category("自定义")] public event EventHandler IndexChecked; /// /// The m step back color /// private Color m_stepBackColor = Color.FromArgb(189, 189, 189); /// /// 步骤背景色 /// /// The color of the step back. [Description("步骤背景色"), Category("自定义")] public Color StepBackColor { get { return m_stepBackColor; } set { m_stepBackColor = value; Refresh(); } } /// /// The m step fore color /// private Color m_stepForeColor = Color.FromArgb(255, 77, 59); /// /// 步骤前景色 /// /// The color of the step fore. [Description("步骤前景色"), Category("自定义")] public Color StepForeColor { get { return m_stepForeColor; } set { m_stepForeColor = value; Refresh(); } } /// /// The m step font color /// private Color m_stepFontColor = Color.White; /// /// 步骤文字颜色 /// /// The color of the step font. [Description("步骤文字景色"), Category("自定义")] public Color StepFontColor { get { return m_stepFontColor; } set { m_stepFontColor = value; Refresh(); } } /// /// The m step width /// private int m_stepWidth = 35; /// /// 步骤宽度 /// /// The width of the step. [Description("步骤宽度景色"), Category("自定义")] public int StepWidth { get { return m_stepWidth; } set { m_stepWidth = value; Refresh(); } } /// /// The m steps /// private string[] m_steps = new string[] { "step1", "step2", "step3" }; /// /// Gets or sets the steps. /// /// The steps. [Description("步骤"), Category("自定义")] public string[] Steps { get { return m_steps; } set { if (m_steps == null || m_steps.Length Steps.Length) return; m_stepIndex = value; Refresh(); if (IndexChecked != null) { IndexChecked(this, null); } } } /// /// The m line width /// private int m_lineWidth = 2; /// /// Gets or sets the width of the line. /// /// The width of the line. [Description("连接线宽度,最小2"), Category("自定义")] public int LineWidth { get { return m_lineWidth; } set { if (value < 2) return; m_lineWidth = value; Refresh(); } } /// /// The m img completed /// private Image m_imgCompleted = null; /// /// Gets or sets the img completed. /// /// The img completed. [Description("已完成步骤图片,当不为空时,已完成步骤将不再显示数字,建议24*24大小"), Category("自定义")] public Image ImgCompleted { get { return m_imgCompleted; } set { m_imgCompleted = value; Refresh(); } } /// /// The m LST cache rect /// List m_lstCacheRect = new List(); /// /// Initializes a new instance of the class. /// public UCStep() { 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 = UCStep_MouseDown; } /// /// Handles the MouseDown event of the UCStep control. /// /// The source of the event. /// The instance containing the event data. void UCStep_MouseDown(object sender, MouseEventArgs e) { var index = m_lstCacheRect.FindIndex(p => p.Contains(e.Location)); if (index >= 0) { StepIndex = index 1; } } /// /// 引发 事件。 /// /// 包含事件数据的 。 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); var g = e.Graphics; g.SetGDIHigh(); if (m_steps != null && m_steps.Length > 0) { System.Drawing.SizeF sizeFirst = g.MeasureString(m_steps[0], this.Font); int y = (this.Height - m_stepWidth - 10 - (int)sizeFirst.Height) / 2; if (y < 0) y = 0; int intTxtY = y m_stepWidth 10; int intLeft = 0; if (sizeFirst.Width > m_stepWidth) { intLeft = (int)(sizeFirst.Width - m_stepWidth) / 2 1; } int intRight = 0; System.Drawing.SizeF sizeEnd = g.MeasureString(m_steps[m_steps.Length - 1], this.Font); if (sizeEnd.Width > m_stepWidth) { intRight = (int)(sizeEnd.Width - m_stepWidth) / 2 1; } int intSplitWidth = 20; intSplitWidth = (this.Width - m_steps.Length - (m_steps.Length * m_stepWidth) - intRight - intLeft) / (m_steps.Length - 1); if (intSplitWidth < 20) intSplitWidth = 20; m_lstCacheRect = new List(); for (int i = 0; i < m_steps.Length; i ) { #region 画圆,横线 Rectangle rectEllipse = new Rectangle(new Point(intLeft i * (m_stepWidth intSplitWidth), y), new Size(m_stepWidth, m_stepWidth)); m_lstCacheRect.Add(rectEllipse); g.FillEllipse(new SolidBrush(m_stepBackColor), rectEllipse); if (m_stepIndex > i) { g.FillEllipse(new SolidBrush(m_stepForeColor), new Rectangle(new Point(intLeft i * (m_stepWidth intSplitWidth) 2, y 2), new Size(m_stepWidth - 4, m_stepWidth - 4))); } if (m_stepIndex > i && m_imgCompleted != null) { g.DrawImage(m_imgCompleted, new Rectangle(new Point((intLeft i * (m_stepWidth intSplitWidth) (m_stepWidth - 24) / 2), y (m_stepWidth - 24) / 2), new Size(24, 24)), 0, 0, m_imgCompleted.Width, m_imgCompleted.Height, GraphicsUnit.Pixel, null); } else { System.Drawing.SizeF _numSize = g.MeasureString((i 1).ToString(), this.Font); g.DrawString((i 1).ToString(), Font, new SolidBrush(m_stepFontColor), new Point(intLeft i * (m_stepWidth intSplitWidth) (m_stepWidth - (int)_numSize.Width) / 2 1, y (m_stepWidth - (int)_numSize.Height) / 2 1)); } #endregion System.Drawing.SizeF sizeTxt = g.MeasureString(m_steps[i], this.Font); g.DrawString(m_steps[i], Font, new SolidBrush(m_stepIndex > i ? m_stepForeColor : m_stepBackColor), new Point(intLeft i * (m_stepWidth intSplitWidth) (m_stepWidth - (int)sizeTxt.Width) / 2 1, intTxtY)); } for (int i = 0; i < m_steps.Length; i ) { if (m_stepIndex > i) { if (i != m_steps.Length - 1) { if (m_stepIndex == i 1) { g.DrawLine(new Pen(m_stepForeColor, m_lineWidth), new Point(intLeft i * (m_stepWidth intSplitWidth) m_stepWidth - 3, y ((m_stepWidth) / 2)), new Point(intLeft i * (m_stepWidth intSplitWidth) m_stepWidth intSplitWidth / 2, y ((m_stepWidth) / 2))); g.DrawLine(new Pen(m_stepBackColor, m_lineWidth), new Point(intLeft i * (m_stepWidth intSplitWidth) m_stepWidth intSplitWidth / 2, y ((m_stepWidth) / 2)), new Point(intLeft (i 1) * (m_stepWidth intSplitWidth) 10, y ((m_stepWidth) / 2))); } else { g.DrawLine(new Pen(m_stepForeColor, m_lineWidth), new Point(intLeft i * (m_stepWidth intSplitWidth) m_stepWidth - 3, y ((m_stepWidth) / 2)), new Point(intLeft (i 1) * (m_stepWidth intSplitWidth) 10, y ((m_stepWidth) / 2))); } } } else { if (i != m_steps.Length - 1) { g.DrawLine(new Pen(m_stepBackColor, m_lineWidth), new Point(intLeft i * (m_stepWidth intSplitWidth) m_stepWidth - 3, y ((m_stepWidth) / 2)), new Point(intLeft (i 1) * (m_stepWidth intSplitWidth) 10, y ((m_stepWidth) / 2))); } } } } } }}
下载说明:请别用迅雷下载,失败请重下,重下不扣分!
-
WPF 抽奖器
简单的抽奖
- 2014-03-03下载
- 积分:1
-
C#操作Access数据库源码
基于SQL语句新建,添加,查找,等操作
- 2015-01-30下载
- 积分:1
-
IEEE电脑鼠走迷宫(完整参赛代码)
本程序是我们小组集体参加电脑鼠比赛的完整参赛代码,内付Maze主函数的完整流程图!
- 2019-08-01下载
- 积分:1
-
wpf 漂亮的UI界面(含左侧导航菜单)
wpf 漂亮的UI界面(含左侧导航菜单)
- 2019-03-19下载
- 积分:1
-
C# 198个winform 实例源码下载
198个winform 经典实例源码
- 2015-01-27下载
- 积分:1
-
C# 获取本地拨号列表并实现拨号/创建拨号功能
C# 获取本地拨号列表并实现拨号/创建拨号功能
- 2013-10-11下载
- 积分:1
-
微信活动大转盘安装包以及cshtml源码
微信活动大转盘安装包以及cshtml源码
- 2015-03-18下载
- 积分:1
-
C#爬虫实现百度图片下载器和小说阅读器
百度图片下载器输入关键字选择图片类型下载到指定目录下;小说阅读器爬取的是笔趣阁网站,依次获取小说分类、小说名称、小说章节以及章节内容,可实现对当前小说的下载、暂停下载和恢复下载。程序实现都涉及到了多线程的知识。两个项目,血赚不亏!
- 2020-12-06下载
- 积分:1
-
C# 各种计算器例子
C# 各种计算器例子
- 2015-06-10下载
- 积分:1
-
C#程序的157个建议(含源代码)
目录前 言第一部分 语言篇第1章 基本语言要素 / 2建议1:正确操作字符串 / 2建议2:使用默认转型方法 / 6建议3:区别对待强制转型与as和is / 9建议4:TryParse比Parse好 / 12建议5:使用int?来确保值类型也可以为null / 15建议6:区别readonly和const的使用方法 / 16建议7:将0值作为枚举的默认值 / 19建议8:避免给枚举类型的元素提供显式的值 / 20建议9:习惯重载运算符 / 22建议10:创建对象时需要考虑是否实现比较器 / 23建议11:区别对待==和Equals / 27建议12:重写Equals时也要重写GetHashCode / 29建议13:为类型输出格式化字符串 / 32建议14:正确实现浅拷贝和深拷贝 / 36建议15:使用dynamic来简化反射实现 / 40第2章 集合和LINQ / 43建议16:元素数量可变的情况下不应使用数组 / 43建议17:多数情况下使用foreach进行循环遍历 / 45建议18:foreach不能代替for / 51建议19:使用更有效的对象和集合初始化 / 53建议20:使用泛型集合代替非泛型集合 / 54建议21:选择正确的集合 / 57建议22:确保集合的线程安全 / 61建议23:避免将List作为自定义集合类的基类 / 64建议24:迭代器应该是只读的 / 67建议25:谨慎集合属性的可写操作 / 68建议26:使用匿名类型存储LINQ查询结果 / 70建议27:在查询中使用Lambda表达式 / 73建议28:理解延迟求值和主动求值之间的区别 / 75建议29:区别LINQ查询中的IEnumerable和IQueryable / 78建议30:使用LINQ取代集合中的比较器和迭代器 / 80建议31:在LINQ查询中避免不必要的迭代 / 83第3章 泛型、委托和事件 / 86建议32:总是优先考虑泛型 / 86建议33:避免在泛型类型中声明静态成员 / 88建议34:为泛型参数设定约束 / 90建议35:使用default为泛型类型变量指定初始值 / 92建议36:使用FCL中的委托声明 / 94建议37:使用Lambda表达式代替方法和匿名方法 / 96建议38:小心闭包中的陷阱 / 99建议39:了解委托的实质 / 103建议40:使用event关键字为委托施加保护 / 106建议41:实现标准的事件模型 / 108建议42:使用泛型参数兼容泛型接口的不可变性 / 109建议43:让接口中的泛型参数支持协变 / 111建议44:理解委托中的协变 / 112建议45:为泛型类型参数指定逆变 / 114第4章 资源管理和序列化 / 116建议46:显式释放资源需继承接口IDisposable / 116建议47:即使提供了显式释放方法,也应该在终结器中提供隐式清理 / 119建议48:Dispose方法应允许被多次调用 / 120建议49:在Dispose模式中应提取一个受保护的虚方法 / 121建议50:在Dispose模式中应区别对待托管资源和非托管资源 / 123建议51:具有可释放字段的类型或拥有本机资源的类型应该是可释放的 / 124建议52:及时释放资源 / 125建议53:必要时应将不再使用的对象引用赋值为null / 127建议54:为无用字段标注不可序列化 / 131建议55:利用定制特性减少可序列化的字段 / 136建议56:使用继承ISerializable接口更灵活地控制序列化过程 / 137建议57:实现ISerializable的子类型应负责父类的序列化 / 140第5章 异常与自定义异常 / 144建议58:用抛出异常代替返回错误代码 / 144建议59:不要在不恰当的场合下引发异常 / 147建议60:重新引发异常时使用Inner Exception / 150建议61:避免在finally内撰写无效代码 / 151建议62:避免嵌套异常 / 157建议63:避免“吃掉”异常 / 160建议64:为循环增加Tester-Doer模式而不是将try-catch置于循环内 / 161建议65:总是处理未捕获的异常 / 162建议66:正确捕获多线程中的异常 / 166建议67:慎用自定义异常 / 168建议68:从System.Exception或其他常见的基本异常中派生异常 / 170建议69:应使用finally避免资源泄漏 / 172建议70:避免在调用栈较低的位置记录异常 / 175第6章 异步、多线程、任务和并行 / 177建议71:区分异步和多线程应用场景 / 177建议72:在线程同步中使用信号量 / 180建议73:避免锁定不恰当的同步对象 / 184建议74:警惕线程的IsBackground / 188建议75:警惕线程不会立即启动 / 189建议76:警惕线程的优先级 / 191建议77:正确停止线程 / 193建议78:应避免线程数量过多 / 194建议79:使用ThreadPool或BackgroundWorker代替Thread / 196建议80:用Task代替ThreadPool / 198建议81:使用Parallel简化同步状态下Task的使用 / 202建议82:Parallel简化但不等同于Task默认行为 / 204建议83:小心Parallel中的陷阱 / 205建议84:使用PLINQ / 208建议85:Task中的异常处理 / 209建议86:Parallel中的异常处理 / 214建议87:区分WPF和WinForm的线程模型 / 216建议88:并行并不总是速度更快 / 220建议89:在并行方法体中谨慎使用锁 / 222第二部分 架构篇 第7章 成员设计 / 226建议90:不要为抽象类提供公开的构造方法 / 226建议91:可见字段应该重构为属性 / 226建议92:谨慎将数组或集合作为属性 / 227建议93:构造方法应初始化主要属性和字段 / 228建议94:区别对待override和new / 229建议95:避免在构造方法中调用虚成员 / 235建议96:成员应优先考虑公开基类型或接口 / 236建议97:优先考虑将基类型或接口作为参数传递 / 237建议98:用params减少重复参数 / 237建议99:重写时不应使用子类参数 / 238建议100:静态方法和实例方法没有区别 / 239建议101:使用扩展方法,向现有类型“添加”方法 / 240第8章 类型设计 / 243建议102:区分接口和抽象类的应用场合 / 243建议103:区分组合和继承的应用场合 / 245建议104:用多态代替条件语句 / 248建议105:使用私有构造函数强化单例 / 251建议106:为静态类添加静态构造函数 / 253建议107:区分静态类和单例 / 255建议108:将类型标识为sealed / 255建议109:谨慎使用嵌套类 / 256建议110:用类来代替enum / 257建议111:避免双向耦合 / 260建议112:将现实世界中的对象抽象为类,将可复用对象圈起来就是命名空间 / 262第9章 安全性设计 / 264建议113:声明变量前考虑最大值 / 264建议114:MD5不再安全 / 265建议115:通过HASH来验证文件是否被篡改 / 268建议116:避免用非对称算法加密文件 / 269建议117:使用SSL确保通信中的数据安全 / 273建议118:使用SecureString保存密钥等机密字符串 / 284建议119:不要使用自己的加密算法 / 289建议120:为程序集指定强名称 / 289建议121:为应用程序设定运行权限 / 291第三部分 编码规范及习惯 第10章 命名规范 / 296建议122:以.为命名空间命名 / 296建议123:程序集不必与命名空间同名 / 296建议124:考虑在命名空间中使用复数 / 297建议125:避免用FCL的类型名称命名自己的类型 / / 297建议126:用名词和名词组给类型命名 / 298建议127:用形容词组给接口命名 / 299建议128:考虑让派生类的名字以基类名字作为后缀 / 300建议129:泛型类型参数要以T作为前缀 / 300建议130:以复数命名枚举类型,以单数命名枚举元素 / 301建议131:用PascalCasing命名公开元素 / 302建议132:考虑用类名作为属性名 / 302建议133:用camelCasing命名私有字段和局部变量 / 303建议134:有条件地使用前缀 / 304建议135: 考虑使用肯定性的短语命名布尔属性 / 305建议136:优先使用后缀表示已有类型的新版本 / 306建议137:委托和事件类型应添加上级后缀 / 307建议138:事件和委托变量使用动词或形容词短语命名 / 308建议139:事件处理器命名采用组合方式 / 309第11章 代码整洁 / 311建议140:使用默认的访问修饰符 / 311建议141:不知道该不该用大括号时,就用 / 312建议142:总是提供有意义的命名 / 314建议143:方法抽象级别应在同一层次 / 315建议144:一个方法只做一件事 / 316建议145:避免过长的方法和过长的类 / 317建议146:只对外公布必要的操作 / 318建议147:重构多个相关属性为一个类 / 319建议148:不重复代码 / 320建议149:使用表驱动法避免过长的if和switch分支 / 321建议150:使用匿名方法、Lambda表达式代替方法 / 324建议151:使用事件访问器替换公开的事件成员变量 / 325建议152:最少,甚至是不要注释 / 326建议153:若抛出异常,则必须要注释 / 326第12章 规范开发行为 / 327建议154:不要过度设计,在敏捷中体会重构的乐趣 / 327建议155:随生产代码一起提交单元测试代码 / 336建议156:利用特性为应用程序提供多个版本 / 342建议157:从写第一个界面开始,就进行自动化测试 / 344
- 2015-09-15下载
- 积分:1