學習winform的自定義控件的開發時看的的一個控件的實現,完全的用戶自定義實現!是大家學習的好例子
先看截圖
標題欄是根據標題的字體來自動伸縮的。當然,標題也支持圖片!
主要代碼:
private void PaintBack(System.Drawing.Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
int ArcWidth = this.C_RoundCorners * 2;
int ArcHeight = this.C_RoundCorners * 2;
int ArcX1 = 0;
int ArcX2 = (this.ShadowControl) ? (this.Width - (ArcWidth + 1)) - this.ShadowThickness : this.Width - (ArcWidth + 1);
int ArcY1=10;
int ArcY2 = (this.ShadowControl) ? (this.Height - (ArcHeight + 1)) - this.ShadowThickness : this.Height - (ArcHeight + 1);
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
System.Drawing.Brush BorderBrush = new SolidBrush(this.BorderColor);
System.Drawing.Pen BorderPen = new Pen(BorderBrush,this.BorderThickness);
System.Drawing.Drawing2D.LinearGradientBrush BackgroundGradientBrush = null;
System.Drawing.Brush BackgroundBrush = new SolidBrush(this.BackgroundColor);
System.Drawing.SolidBrush ShadowBrush = null;
System.Drawing.Drawing2D.GraphicsPath ShadowPath = null;
//畫出陰影效果
if (this.ShadowControl)
{
ShadowBrush = new SolidBrush(this.ShadowColor);
ShadowPath = new System.Drawing.Drawing2D.GraphicsPath();
ShadowPath.AddArc(ArcX1+this.ShadowThickness,ArcY1+this.ShadowThickness,ArcWidth,ArcHeight,180,SweepAngle);//頂端的左角
ShadowPath.AddArc(ArcX2+this.ShadowThickness,ArcY1+this.ShadowThickness,ArcWidth,ArcHeight,270,SweepAngle);//頂端右角
ShadowPath.AddArc(ArcX2+this.ShadowThickness,ArcY2+this.ShadowThickness,ArcWidth,ArcHeight,360,SweepAngle);//底部右角
ShadowPath.AddArc(ArcX1+this.ShadowThickness,ArcY2+this.ShadowThickness,ArcWidth,ArcHeight,90,SweepAngle);//底部左角
ShadowPath.CloseAllFigures();
g.FillPath(ShadowBrush,ShadowPath);
}
//畫出圖形
path.AddArc(ArcX1,ArcY1,ArcWidth,ArcHeight,180,SweepAngle);
path.AddArc(ArcX2,ArcY1,ArcWidth,ArcHeight,270,SweepAngle);
path.AddArc(ArcX2,ArcY2,ArcWidth,ArcHeight,360,SweepAngle);
path.AddArc(ArcX1,ArcY2, ArcWidth, ArcHeight,90, SweepAngle);
path.CloseAllFigures();
if (this.C_BackgroundGradientMode == GroupBoxGradientMode.None)
{
g.FillPath(BackgroundBrush, path);
}
else {
BackgroundGradientBrush = new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height),this.BackgroundColor,this.BackgroundGradientColor,(LinearGradientMode)this.BackgroundGradientMode);
g.FillPath(BackgroundGradientBrush,path);
}
//畫邊框
g.DrawPath(BorderPen,path);
//銷燬Graphic 對象
if (path != null)
{
path.Dispose();
}
if (BorderBrush != null)
{
BorderPen.Dispose();
}
if (BorderPen != null)
{
BorderPen.Dispose();
}
if (BackgroundGradientBrush != null)
{
BackgroundGradientBrush.Dispose();
}
if (BackgroundBrush != null)
{
BackgroundBrush.Dispose();
}
if (ShadowBrush != null)
{
ShadowBrush.Dispose();
}
if (ShadowPath != null)
{
ShadowPath.Dispose();
}
}
代碼太多,提供下載: