定义边框属性类,分别可以用来设置上下左右4个边框的宽度、颜色以及样式
[TypeConverter(typeof(BorderStyleConverter))] //[Editor(typeof(BorderStyleEditor), typeof(UITypeEditor))] public class Border { public int TopWidth { get; set; } public int RightWidth { get; set; } public int BottomWidth { get; set; } public int LeftWidth { get; set; } public Color TopColor { get; set; } public Color RightColor { get; set; } public Color BottomColor { get; set; } public Color LeftColor { get; set; } public ButtonBorderStyle TopBorderStyle { get; set; } public ButtonBorderStyle RightBorderStyle { get; set; } public ButtonBorderStyle BottomBorderStyle { get; set; } public ButtonBorderStyle LeftBorderStyle { get; set; } }
定义边框属性转换器
public class BorderStyleConverter : ExpandableObjectConverter { public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) { return true; } return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value is Border) { Border border = (Border)value; return string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}", border.TopWidth, border.RightWidth, border.BottomWidth, border.LeftWidth, border.TopColor.Name, border.RightColor.Name, border.BottomColor.Name, border.LeftColor.Name, border.TopBorderStyle, border.RightBorderStyle, border.BottomBorderStyle, border.LeftBorderStyle); } return base.ConvertTo(context, culture, value, destinationType); } public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) { return true; } return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { try { string[] pieces = ((string)value).Split(','); if (pieces.Length == 12) { Border border = new Border(); border.TopWidth = int.Parse(pieces[0]); border.RightWidth = int.Parse(pieces[1]); border.BottomWidth = int.Parse(pieces[2]); border.LeftWidth = int.Parse(pieces[3]); border.TopColor = Color.FromName(pieces[4]); border.RightColor = Color.FromName(pieces[5]); border.BottomColor = Color.FromName(pieces[6]); border.LeftColor = Color.FromName(pieces[7]); border.TopBorderStyle = (ButtonBorderStyle)Enum.Parse(typeof(ButtonBorderStyle), pieces[8]); border.RightBorderStyle = (ButtonBorderStyle)Enum.Parse(typeof(ButtonBorderStyle), pieces[9]); border.BottomBorderStyle = (ButtonBorderStyle)Enum.Parse(typeof(ButtonBorderStyle), pieces[10]); border.LeftBorderStyle = (ButtonBorderStyle)Enum.Parse(typeof(ButtonBorderStyle), pieces[11]); return border; } } catch { throw new ArgumentException("Invalid value for BorderStyle."); } } return base.ConvertFrom(context, culture, value); } }
定义设计编辑器的相关属性(可在VS属性窗口实现下拉框或者弹出式的输入编辑)
public class BorderStyleEditor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { return value; } }
自定义控件实现,引入
Border
类,并在OnPaint
事件中进行边框绘制private Border _BorderStyle; [Description("设置边框样式")] public new Border BorderStyle { get { return _BorderStyle; } set { _BorderStyle = value; Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (BorderStyle != null) { ControlPaint.DrawBorder(e.Graphics, ClientRectangle, BorderStyle.LeftColor, BorderStyle.LeftWidth, BorderStyle.LeftBorderStyle, //左边 BorderStyle.TopColor, BorderStyle.TopWidth, BorderStyle.TopBorderStyle, //上边 BorderStyle.RightColor, BorderStyle.RightWidth, BorderStyle.RightBorderStyle, //右边 BorderStyle.BottomColor, BorderStyle.BottomWidth, BorderStyle.BottomBorderStyle);//底边 } }
Winform自定义控件边框
最新推荐文章于 2024-07-16 19:30:35 发布