Winform自定义控件边框

  1. 定义边框属性类,分别可以用来设置上下左右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; }
        }
  2. 定义边框属性转换器

    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);
    }
    }
  3. 定义设计编辑器的相关属性(可在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;
    }
    }
  4. 自定义控件实现,引入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);//底边
                }
    
    
            }

f1037a70bbbd7c44b1c749f0029a377f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值