【控件扩展】带圆角、边框、渐变的panel

下载地址:  http://files.cnblogs.com/chengulv/custompanel_demo.zip

using System;
namespace LC.Fun
{

    /// <summary>Panel扩展 带圆角,颜色渐变</summary>
    [System.Drawing.ToolboxBitmapAttribute(typeof(System.Windows.Forms.Panel))]
    public class RoundPanel : System.Windows.Forms.Panel
    {
        /// <summary>渐变的方向</summary>
        public enum LinearGradientMode
        {
            Horizontal = 0,
            Vertical = 1,
            ForwardDiagonal = 2,
            BackwardDiagonal = 3,
            None = 4
        }

        /// <summary>
        /// 圆角的位置
        /// </summary>
        [FlagsAttribute()]
        public enum CornerCurveMode
        {
            None = 0,
            TopLeft = 1,
            TopRight = 2,
            TopLeft_TopRight = 3,
            BottomLeft = 4,
            TopLeft_BottomLeft = 5,
            TopRight_BottomLeft = 6,
            TopLeft_TopRight_BottomLeft = 7,
            BottomRight = 8,
            BottomRight_TopLeft = 9,
            BottomRight_TopRight = 10,
            BottomRight_TopLeft_TopRight = 11,
            BottomRight_BottomLeft = 12,
            BottomRight_TopLeft_BottomLeft = 13,
            BottomRight_TopRight_BottomLeft = 14,
            All = 15

        }

        // Fields
        private System.Drawing.Color _BackColour1 = System.Drawing.SystemColors.Window;
        private System.Drawing.Color _BackColour2 = System.Drawing.SystemColors.Window;
        private LinearGradientMode _GradientMode = LinearGradientMode.None;
        private System.Windows.Forms.BorderStyle _BorderStyle = System.Windows.Forms.BorderStyle.None;
        private System.Drawing.Color _BorderColour = System.Drawing.SystemColors.WindowFrame;
        private int _BorderWidth = 1;
        private int _Curvature = 0;
        // Properties
        //   Shadow the Backcolor property so that the base class will still render with a transparent backcolor
        private CornerCurveMode _CurveMode = CornerCurveMode.All;

        [System.ComponentModel.DefaultValueAttribute(typeof(System.Drawing.Color), "Window"), System.ComponentModel.CategoryAttribute("自定义cx"), System.ComponentModel.DescriptionAttribute("背景色1")]
        public new System.Drawing.Color BackColor
        {
            get
            {
                return this._BackColour1;
            }
            set
            {
                this._BackColour1 = value;
                if (this.DesignMode == true)
                {
                    this.Invalidate();
                }
            }
        }

        [System.ComponentModel.DefaultValueAttribute(typeof(System.Drawing.Color), "Window"), System.ComponentModel.CategoryAttribute("自定义cx"), System.ComponentModel.DescriptionAttribute("背景色2")]
        public System.Drawing.Color BackColor2
        {
            get
            {
                return this._BackColour2;
            }
            set
            {
                this._BackColour2 = value;
                if (this.DesignMode == true)
                {
                    this.Invalidate();
                }
            }
        }

        [System.ComponentModel.DefaultValueAttribute(typeof(LinearGradientMode), "None"), System.ComponentModel.CategoryAttribute("自定义cx"), System.ComponentModel.DescriptionAttribute("颜色渐变的方向")]
        public LinearGradientMode GradientMode
        {
            get
            {
                return this._GradientMode;
            }
            set
            {
                this._GradientMode = value;
                if (this.DesignMode == true)
                {
                    this.Invalidate();
                }
            }
        }

        [System.ComponentModel.DefaultValueAttribute(typeof(System.Windows.Forms.BorderStyle), "None"), System.ComponentModel.CategoryAttribute("自定义cx"), System.ComponentModel.DescriptionAttribute("边框的样式")]
        public new System.Windows.Forms.BorderStyle BorderStyle
        {
            get
            {
                return this._BorderStyle;
            }
            set
            {
                this._BorderStyle = value;
                if (this.DesignMode == true)
                {
                    this.Invalidate();
                }
            }
        }

        [System.ComponentModel.DefaultValueAttribute(typeof(System.Drawing.Color), "WindowFrame"), System.ComponentModel.CategoryAttribute("自定义cx"), System.ComponentModel.DescriptionAttribute("边框的颜色")]
        public System.Drawing.Color BorderColor
        {
            get
            {
                return this._BorderColour;
            }
            set
            {
                this._BorderColour = value;
                if (this.DesignMode == true)
                {
                    this.Invalidate();
                }
            }
        }

        [System.ComponentModel.DefaultValueAttribute(typeof(int), "1"), System.ComponentModel.CategoryAttribute("自定义cx"), System.ComponentModel.DescriptionAttribute("边框大小")]
        public int BorderWidth
        {
            get
            {
                return this._BorderWidth;
            }
            set
            {
                this._BorderWidth = value;
                if (this.DesignMode == true)
                {
                    this.Invalidate();
                }
            }
        }

        [System.ComponentModel.DefaultValueAttribute(typeof(int), "0"), System.ComponentModel.CategoryAttribute("自定义cx"), System.ComponentModel.DescriptionAttribute("圆角大小")]
        public int Curvature
        {
            get
            {
                return this._Curvature;
            }
            set
            {
                this._Curvature = value;
                if (this.DesignMode == true)
                {
                    this.Invalidate();
                }
            }
        }

        [System.ComponentModel.DefaultValueAttribute(typeof(CornerCurveMode), "All"), System.ComponentModel.CategoryAttribute("自定义cx"), System.ComponentModel.DescriptionAttribute("圆角的位置")]
        public CornerCurveMode CurveMode
        {
            get
            {
                return this._CurveMode;
            }
            set
            {
                this._CurveMode = value;
                if (this.DesignMode == true)
                {
                    this.Invalidate();
                }
            }
        }

        private int adjustedCurve
        {
            get
            {
                int curve = 0;
                if (!(this._CurveMode == CornerCurveMode.None))
                {
                    if (this._Curvature > (this.ClientRectangle.Width / 2))
                    {
                        curve = DoubleToInt(this.ClientRectangle.Width / 2);
                    }
                    else
                    {
                        curve = this._Curvature;
                    }
                    if (curve > (this.ClientRectangle.Height / 2))
                    {
                        curve = DoubleToInt(this.ClientRectangle.Height / 2);
                    }
                }
                return curve;
            }
        }

        public RoundPanel()
            : base()
        {
            this.SetDefaultControlStyles();
            this.customInitialisation();
        }

        private void SetDefaultControlStyles()
        {
            this.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, true);
            this.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, false);
            this.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, true);
            this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
            this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
        }

        private void customInitialisation()
        {
            this.SuspendLayout();
            base.BackColor = System.Drawing.Color.Transparent;
            this.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.ResumeLayout(false);
        }

        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
            pevent.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            System.Drawing.Drawing2D.GraphicsPath graphPath;
            graphPath = this.GetPath();
            //    Create Gradient Brush (Cannot be width or height 0)
            System.Drawing.Drawing2D.LinearGradientBrush filler;
            System.Drawing.Rectangle rect = this.ClientRectangle;
            if (this.ClientRectangle.Width == 0)
            {
                rect.Width += 1;
            }
            if (this.ClientRectangle.Height == 0)
            {
                rect.Height += 1;
            }
            if (this._GradientMode == LinearGradientMode.None)
            {
                filler = new System.Drawing.Drawing2D.LinearGradientBrush(rect, this._BackColour1, this._BackColour1, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
            }
            else
            {
                filler = new System.Drawing.Drawing2D.LinearGradientBrush(rect, this._BackColour1, this._BackColour2, ((System.Drawing.Drawing2D.LinearGradientMode)this._GradientMode));
            }
            pevent.Graphics.FillPath(filler, graphPath);
            filler.Dispose();
            if (this._BorderStyle == System.Windows.Forms.BorderStyle.FixedSingle)
            {
                System.Drawing.Pen borderPen = new System.Drawing.Pen(this._BorderColour, this._BorderWidth);
                pevent.Graphics.DrawPath(borderPen, graphPath);
                borderPen.Dispose();
            }
            else if (this._BorderStyle == System.Windows.Forms.BorderStyle.Fixed3D)
            {
                DrawBorder3D(pevent.Graphics, this.ClientRectangle);
            }
            else if (this._BorderStyle == System.Windows.Forms.BorderStyle.None)
            {
            }
            filler.Dispose();
            graphPath.Dispose();
        }

        protected System.Drawing.Drawing2D.GraphicsPath GetPath()
        {
            System.Drawing.Drawing2D.GraphicsPath graphPath = new System.Drawing.Drawing2D.GraphicsPath();
            if (this._BorderStyle == System.Windows.Forms.BorderStyle.Fixed3D)
            {
                graphPath.AddRectangle(this.ClientRectangle);
            }
            else
            {
                try
                {
                    int curve = 0;
                    System.Drawing.Rectangle rect = this.ClientRectangle;
                    int offset = 0;
                    if (this._BorderStyle == System.Windows.Forms.BorderStyle.FixedSingle)
                    {
                        if (this._BorderWidth > 1)
                        {
                            offset = DoubleToInt(this.BorderWidth / 2);
                        }
                        curve = this.adjustedCurve;
                    }
                    else if (this._BorderStyle == System.Windows.Forms.BorderStyle.Fixed3D)
                    {
                    }
                    else if (this._BorderStyle == System.Windows.Forms.BorderStyle.None)
                    {
                        curve = this.adjustedCurve;
                    }
                    if (curve == 0)
                    {
                        graphPath.AddRectangle(System.Drawing.Rectangle.Inflate(rect, -offset, -offset));
                    }
                    else
                    {
                        int rectWidth = rect.Width - 1 - offset;
                        int rectHeight = rect.Height - 1 - offset;
                        int curveWidth = 1;
                        if ((this._CurveMode & CornerCurveMode.TopRight) != 0)
                        {
                            curveWidth = (curve * 2);
                        }
                        else
                        {
                            curveWidth = 1;
                        }
                        graphPath.AddArc(rectWidth - curveWidth, offset, curveWidth, curveWidth, 270, 90);
                        if ((this._CurveMode & CornerCurveMode.BottomRight) != 0)
                        {
                            curveWidth = (curve * 2);
                        }
                        else
                        {
                            curveWidth = 1;
                        }
                        graphPath.AddArc(rectWidth - curveWidth, rectHeight - curveWidth, curveWidth, curveWidth, 0, 90);
                        if ((this._CurveMode & CornerCurveMode.BottomLeft) != 0)
                        {
                            curveWidth = (curve * 2);
                        }
                        else
                        {
                            curveWidth = 1;
                        }
                        graphPath.AddArc(offset, rectHeight - curveWidth, curveWidth, curveWidth, 90, 90);
                        if ((this._CurveMode & CornerCurveMode.TopLeft) != 0)
                        {
                            curveWidth = (curve * 2);
                        }
                        else
                        {
                            curveWidth = 1;
                        }
                        graphPath.AddArc(offset, offset, curveWidth, curveWidth, 180, 90);
                        graphPath.CloseFigure();
                    }
                }
                catch (System.Exception)
                {
                    graphPath.AddRectangle(this.ClientRectangle);
                }
            }
            return graphPath;
        }

        public static void DrawBorder3D(System.Drawing.Graphics graphics, System.Drawing.Rectangle rectangle)
        {
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            graphics.DrawLine(System.Drawing.SystemPens.ControlDark, rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Y);
            graphics.DrawLine(System.Drawing.SystemPens.ControlDark, rectangle.X, rectangle.Y, rectangle.X, rectangle.Height - 1);
            graphics.DrawLine(System.Drawing.SystemPens.ControlDarkDark, rectangle.X + 1, rectangle.Y + 1, rectangle.Width - 1, rectangle.Y + 1);
            graphics.DrawLine(System.Drawing.SystemPens.ControlDarkDark, rectangle.X + 1, rectangle.Y + 1, rectangle.X + 1, rectangle.Height - 1);
            graphics.DrawLine(System.Drawing.SystemPens.ControlLight, rectangle.X + 1, rectangle.Height - 2, rectangle.Width - 2, rectangle.Height - 2);
            graphics.DrawLine(System.Drawing.SystemPens.ControlLight, rectangle.Width - 2, rectangle.Y + 1, rectangle.Width - 2, rectangle.Height - 2);
            graphics.DrawLine(System.Drawing.SystemPens.ControlLightLight, rectangle.X, rectangle.Height - 1, rectangle.Width - 1, rectangle.Height - 1);
            graphics.DrawLine(System.Drawing.SystemPens.ControlLightLight, rectangle.Width - 1, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1);
        }

        public static int DoubleToInt(double value)
        {
            return System.Decimal.ToInt32(System.Decimal.Floor(System.Decimal.Parse((value).ToString())));
        }

    }
}
时间: 2024-11-08 18:20:33

【控件扩展】带圆角、边框、渐变的panel的相关文章

巧妙实现带圆角的渐变边框

如何实现下面这个渐变的边框效果: 这个问题本身不难,实现的方法也有一些,主要是有一些细节需要注意. border-image border-image 是 CSS 规范 CSS Backgrounds and Borders Module Level 3 (最新一版的关于 background 和 border 的官方规范) 新增的一个属性值. 顾名思义,我们可以给 border 元素添加 image,类似于 background-image,可以是图片也可以是渐变,不再局限于纯色. 使用 bo

WPF为控件扩展的附加属性不起作用需要注意的地方

我给WPF控件扩展了一个名为CornerRadius的附加属性,以便于所有控件在重写ControlTemplate的时候,在ControlTemplate中先加上一个Border,然后利用附加的CornerRadius,设置圆角属性,看起来是这样的: <ControlTemplate TargetType="{x:Type TextBox}"> <Border x:Name="TextBorder" CornerRadius="{Bind

给easyui datebox时间框控件扩展一个清空的实例

给easyui datebox扩展一个清空的实例 步骤一:拓展插件 /** * 给时间框控件扩展一个清除的按钮 */ $.fn.datebox.defaults.cleanText = '清空'; (function ($) { var buttons = $.extend([], $.fn.datebox.defaults.buttons); buttons.splice(1, 0, { text: function (target) { return $(target).datebox("o

Silverlight中DataPager控件扩展

大家一定遇到这样的情况,想改变一下SL的DataPager的显示信息,比如希望分页控件上显示数据的总数.那么就需要扩展一下DataPager控件即可. 其实扩展DataPager很简单,只要获取到DataPager控件上的元素,然后再改变元素上数据.比如DataPager控件上显示“总页数”的元素是一个TextBlock,那么可以通过方法GetTemplateChild获取到,参数是元素的名称.然后通过重写方法OnApplyTemplate即可,下面请看代码 代码 Code highlighti

ASP.NET MVC显示UserControl控件(扩展篇)

昨晚Insus.NET有怀旧一下<念念不忘,ASP.NET MVC显示WebForm网页或UserControl控件>http://www.cnblogs.com/insus/p/3641610.html 那仅是小小尝试,还有很多不明的地方. 那一篇其中有Render用户控件ascx的.如今Insus.NET想重构它一下,让步其能在Action或是Razor语法中使用. 创建一个AscxUtility.cs,这们我们可以在应用程序中多个地方使用. 这样我们就可以重构一下昨晚那个public A

android控件---自定义带文本的ImageButton

由于SDK提供的ImageButton只能添加图片,不能添加文字:而Button控件添加的文字只能显示在图片内部:当我们需要添加文字在图片外部时就不能满足我们的需求了,顾只能自己写个自定义ImageButton.说是ImageButton,其实并不是继承于ImageButton,而是从LinearLayout继承,由于LinearLayout是线性排列,通过setOrientation(LinearLayout.VERTICAL)的方式达到View垂直排列的目的,所以很简单,只需要添加两个Vie

DevExpress控件扩展之表达式编辑器

业务需求: 业务工作中经常需要对表格中的数据进行处理,包括过滤.复合计算等.过滤需要有过滤条件,复合计算需要计算公式.这两种场景都需要一个表达式编辑器.GridControl自带过滤条件的表达式编辑器,我们要做的就是把这个编辑器拿出来,独立于GridControl,进而可以绑定到其它控件上. 实现原理: 找到表达式编辑器内部类UnboundColumnExpressionEditorForm,这是一个窗口类.我们将其边框设置为None,Dock属性设置为Fill,拖放到控件上,使其看上去像个控件

仿饿了么加入购物车旋转控件 - 自带闪转腾挪动画 的按钮

转载请标明出处: http://blog.csdn.net/zxt0601/article/details/54235736 本文出自:[张旭童的博客](http://blog.csdn.net/zxt0601) 代码传送门:喜欢的话,随手点个star.多谢 https://github.com/mcxtzhang/AnimShopButton 概述 在上文,酷炫Path动画已经预告了,今天给大家带来的是利用 纯自定义View,实现的仿饿了么加入购物车控件,自带闪转腾挪动画的按钮. 效果图如下:

duilib list控件扩展

对于简单的list控件已经有前辈分析了自带demo的ListRes全过程,duilib DirectUI库里面的一个简单的例子ListDemo, 他分析了listdemo的来龙去脉,这里我只是将个人理解的list分析一下. 由于自带的listdemo不能满足要求,所以必须个人扩展list,通过尝试 发现 1. 列表头禁止拖动情况 这种情况很简单, 配置xml的时候将 表头宽度 和 元素宽度设置相同就行了,listheader布局如下: <List name="domainlist"