下载地址:  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 = ,
Vertical = ,
ForwardDiagonal = ,
BackwardDiagonal = ,
None =
} /// <summary>
/// 圆角的位置
/// </summary>
[FlagsAttribute()]
public enum CornerCurveMode
{
None = ,
TopLeft = ,
TopRight = ,
TopLeft_TopRight = ,
BottomLeft = ,
TopLeft_BottomLeft = ,
TopRight_BottomLeft = ,
TopLeft_TopRight_BottomLeft = ,
BottomRight = ,
BottomRight_TopLeft = ,
BottomRight_TopRight = ,
BottomRight_TopLeft_TopRight = ,
BottomRight_BottomLeft = ,
BottomRight_TopLeft_BottomLeft = ,
BottomRight_TopRight_BottomLeft = ,
All = } // 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 = ;
private int _Curvature = ;
// 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), ""), 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), ""), 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 = ;
if (!(this._CurveMode == CornerCurveMode.None))
{
if (this._Curvature > (this.ClientRectangle.Width / ))
{
curve = DoubleToInt(this.ClientRectangle.Width / );
}
else
{
curve = this._Curvature;
}
if (curve > (this.ClientRectangle.Height / ))
{
curve = DoubleToInt(this.ClientRectangle.Height / );
}
}
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 == )
{
rect.Width += ;
}
if (this.ClientRectangle.Height == )
{
rect.Height += ;
}
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 = ;
System.Drawing.Rectangle rect = this.ClientRectangle;
int offset = ;
if (this._BorderStyle == System.Windows.Forms.BorderStyle.FixedSingle)
{
if (this._BorderWidth > )
{
offset = DoubleToInt(this.BorderWidth / );
}
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 == )
{
graphPath.AddRectangle(System.Drawing.Rectangle.Inflate(rect, -offset, -offset));
}
else
{
int rectWidth = rect.Width - - offset;
int rectHeight = rect.Height - - offset;
int curveWidth = ;
if ((this._CurveMode & CornerCurveMode.TopRight) != )
{
curveWidth = (curve * );
}
else
{
curveWidth = ;
}
graphPath.AddArc(rectWidth - curveWidth, offset, curveWidth, curveWidth, , );
if ((this._CurveMode & CornerCurveMode.BottomRight) != )
{
curveWidth = (curve * );
}
else
{
curveWidth = ;
}
graphPath.AddArc(rectWidth - curveWidth, rectHeight - curveWidth, curveWidth, curveWidth, , );
if ((this._CurveMode & CornerCurveMode.BottomLeft) != )
{
curveWidth = (curve * );
}
else
{
curveWidth = ;
}
graphPath.AddArc(offset, rectHeight - curveWidth, curveWidth, curveWidth, , );
if ((this._CurveMode & CornerCurveMode.TopLeft) != )
{
curveWidth = (curve * );
}
else
{
curveWidth = ;
}
graphPath.AddArc(offset, offset, curveWidth, curveWidth, , );
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 - , rectangle.Y);
graphics.DrawLine(System.Drawing.SystemPens.ControlDark, rectangle.X, rectangle.Y, rectangle.X, rectangle.Height - );
graphics.DrawLine(System.Drawing.SystemPens.ControlDarkDark, rectangle.X + , rectangle.Y + , rectangle.Width - , rectangle.Y + );
graphics.DrawLine(System.Drawing.SystemPens.ControlDarkDark, rectangle.X + , rectangle.Y + , rectangle.X + , rectangle.Height - );
graphics.DrawLine(System.Drawing.SystemPens.ControlLight, rectangle.X + , rectangle.Height - , rectangle.Width - , rectangle.Height - );
graphics.DrawLine(System.Drawing.SystemPens.ControlLight, rectangle.Width - , rectangle.Y + , rectangle.Width - , rectangle.Height - );
graphics.DrawLine(System.Drawing.SystemPens.ControlLightLight, rectangle.X, rectangle.Height - , rectangle.Width - , rectangle.Height - );
graphics.DrawLine(System.Drawing.SystemPens.ControlLightLight, rectangle.Width - , rectangle.Y, rectangle.Width - , rectangle.Height - );
} public static int DoubleToInt(double value)
{
return System.Decimal.ToInt32(System.Decimal.Floor(System.Decimal.Parse((value).ToString())));
} }
}

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

  1. C#在WinForm中重写ProgressBar控件(带%的显示)

    废话少说,直接上码: namespace csPublish { [ToolboxItem(true)] class textProgressBar : System.Windows.Forms.Pr ...

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

    给easyui datebox扩展一个清空的实例 步骤一:拓展插件 /** * 给时间框控件扩展一个清除的按钮 */ $.fn.datebox.defaults.cleanText = '清空'; ( ...

  3. WPF开源控件扩展库 - MaterialDesignExtensions

    Material Design Extensions 在WPF开源控件库 Material Design in XAML Toolkit(本站介绍:链接)的基础上进行了控件扩展和特性新增.本开源项目中 ...

  4. c# 遍历子控件,比如Form下的group,或者panel

    方法很好用.目的是遍历所有容器的子控件... 方法1private void GetControl(Control.ControlCollection ctc, ref int checkNull) ...

  5. 设置UI控件的Layer属性(边框可见,边框颜色,边框宽度,边框圆角)

    设置UI控件的Layer属性 #import "ViewController.h" @interface ViewController () @property (strong, ...

  6. 仿饿了么增加购物车旋转控件 - 自带闪转腾挪动画 的button

    本篇文章已授权微信公众号 guolin_blog (郭霖)独家公布 转载请标明出处: http://blog.csdn.net/zxt0601/article/details/54235736 本文出 ...

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

    业务需求: 业务工作中经常需要对表格中的数据进行处理,包括过滤.复合计算等.过滤需要有过滤条件,复合计算需要计算公式.这两种场景都需要一个表达式编辑器.GridControl自带过滤条件的表达式编辑器 ...

  8. NUI控件扩展

    摘要:NUI组件是公司新一代的前端开发框架,它精致优雅的前端编程模型,是大家能够,或者想接受学习它的重要原因,在使用它的时候,一定不免会想增加自己的控件,让别人也能够如此优雅的使用. 其实NUI的扩展 ...

  9. qt 控件 背景色 透明 除去边框

    在调试ui的时候,需要将背景色变为透明,与母控件的颜色一致,并且除去边框. 参考链接: http://www.qtcentre.org/threads/12148-how-QTextEdit-tran ...

随机推荐

  1. iOS-GCD用法

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...

  2. InitGoogleLogging坑爹

    google::InitGoogleLogging(argv[0]);    //::google::InitGoogleLogging(argv[0]); 加上这句,竟然没有日志

  3. Word Ladder

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  4. Examining Open vSwitch Traffic Patterns

    In this post, I want to provide some additional insight on how the use of Open vSwitch (OVS) affects ...

  5. OpenFlow Switch学习笔记(三)——Flow Tables

    这次我们主要讨论下OpenFlow Switch的核心组件之一——Flow Tables,以了解其内部的 matching 以及 action handling 机制.下文将会分为几个部分来逐步详述O ...

  6. 三分钟了解Activity工作流

    一. 什么是工作流 以请假为例,现在大多数公司的请假流程是这样的 员工打电话(或网聊)向上级提出请假申请——上级口头同意——上级将请假记录下来——月底将请假记录上交公司——公司将请假录入电脑 采用工作 ...

  7. Windows上的文件合并命令

    从Linux转到Windowns后,发现很多好用的shell命令都没有了,但实际情况是Windows一样有DOS时代的命令窗口,在CLI年代用DOS的人也要干活. 比如,今天想将几个单独的sql文件整 ...

  8. hdu 5206 Four Inages Strategy

    题目大意: 判断空间上4个点是否形成一个正方形 分析: 标称思想 : 在p2,p3,p4中枚举两个点作为p1的邻点,不妨设为pi,pj,然后判断p1pi与p1pj是否相等.互相垂直,然后由向量法,最后 ...

  9. DB2中的ROW_NUMBER() OVER()函数用法

      ROW_NUMBER() OVER()大概有俩方面的作用 1,分页, 并返回分页结果集.2,是对数据进行处理 分组 db2的分页: select tmp.* from ( SELECT rownu ...

  10. JavaScript substring() 方法

    定义和用法 substring() 方法用于提取字符串中介于两个指定下标之间的字符. 语法 stringObject.substring(start,stop) 参数 描述 start 必需.一个非负 ...