zoukankan      html  css  js  c++  java
  • 自绘GroupBox控件

    下面显示的是效果图:

    然后是代码,其中有一些解释:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    namespace scyGroupBox
    {
    public partial class scyGroupBox : GroupBox
    {

    private GraphicsPath gpRegion = null;
    private bool _mouseHover = false;
    private bool _mouseDown = false;
    private int textHeight = 0; //文本高度

    public scyGroupBox()
    {
    InitializeComponent();
    BackColor
    = Color.YellowGreen;
    gpRegion
    = CreateRadiusRect(this.ClientRectangle, radius);
    if (gpRegion != null)
    {
    this.Region = new Region(gpRegion);
    }
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true);
    }

    #region 圆角大小 Property
    private int radius = 3;
    public int Radius
    {
    get
    {
    return radius;
    }
    set
    {
    if (value < 0)
    {
    return;
    }
    else
    {
    radius
    = value;
    }
    }
    }
    #endregion

    #region 边线颜色
    private Color borderColor = Color.DarkGreen;
    public Color BorderColor
    {
    get { return borderColor; }
    set { borderColor = value; }
    }
    #endregion

    #region 上部渐变颜色
    private Color uShineColor = Color.FromArgb(140, Color.White);
    public Color UShineColor
    {
    get { return uShineColor; }
    set { uShineColor = value; }
    }
    #endregion

    /// <summary>
    /// 描绘具有圆角的button原型路径
    /// </summary>
    protected virtual GraphicsPath CreateRadiusRect(Rectangle rectangle, int radius)
    {
    GraphicsPath path
    = new GraphicsPath();

    int l = rectangle.Left; //左边
    int t = rectangle.Top; //上边
    int w = rectangle.Width; //宽度
    int h = rectangle.Height;// 高度
    int d = radius / 2; //除以2,得到半径

    path.AddArc(l,t,d,d,
    180,90); //左上角
    path.AddLine(l + radius, t, l + w - radius, t);//上边线

    path.AddArc(l
    + w - d, t, d, d, 270, 90);//右上角
    path.AddLine(l + w, t + radius, l + w, t + h - radius); //右边线

    path.AddArc(l
    + w - d, t + h - d, d, d, 0, 90);//右下角
    path.AddLine(l + w - radius, t + h, l + radius, t + h); //下边线

    path.AddArc(l, t
    + h - d, d, d, 90, 90); //左下角
    path.AddLine(l, t + h - radius, l, t + radius); //左边线

    path.CloseFigure();
    return path;
    }

    protected override void OnSizeChanged(EventArgs e)
    {
    gpRegion
    = CreateRadiusRect(this.ClientRectangle, radius);
    if (gpRegion != null)
    {
    this.Region = new Region(gpRegion);
    }
    base.OnSizeChanged(e);
    }

    //按照button路径进行border绘制
    protected virtual void DrawBorder(Graphics g, Pen pen)
    {
    if (gpRegion != null)
    {
    g.SmoothingMode
    = SmoothingMode.HighQuality;
    g.InterpolationMode
    = InterpolationMode.HighQualityBicubic;
    g.DrawPath(pen, CreateRadiusRect(
    new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1), radius));
    g.SmoothingMode
    = SmoothingMode.Default;
    g.InterpolationMode
    = InterpolationMode.Default;
    }
    pen.Dispose();
    pen
    = null;
    }

    //绘制上部渐变色
    protected virtual void DrawUpperShine(Graphics g)
    {
    Color c1
    = Color.FromArgb(200,uShineColor);
    Color c2
    = Color.FromArgb(100,Color.White);
    Rectangle rc
    = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, Width - 1, Height / 2);
    if (_mouseDown)
    {
    rc
    = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, Width - 1, (Height - 10) / 2);
    }
    using (LinearGradientBrush lbrush = new LinearGradientBrush(rc, c1, c2, LinearGradientMode.Vertical))
    {
    g.FillRectangle(lbrush,rc);
    }
    }

    //设置字体的大小,宽高,位置信息
    protected virtual void DrawText(Graphics g)
    {
    if (!String.IsNullOrEmpty(this.Text.Trim()))
    {
    int x = 0;
    int y = 0;
    int w = 0;
    int h = 0;

    Size size
    = g.MeasureString(Text, Font).ToSize();
    w
    = size.Width > Width ? Width : size.Width;
    h
    = size.Height > Height ? Height : size.Height;

    Pen pen
    = new Pen(ForeColor);

    g.DrawString(Text, Font, pen.Brush,
    new RectangleF((float)x + 10, (float)y + 3, (float)w + 3, (float)h));
    pen.Dispose();
    pen
    = null;
    textHeight
    = w;
    }
    }

    //绘制内部的方块
    protected virtual void DrawInner(Graphics g, Rectangle rectangle, Pen p)
    {
    int l = rectangle.Left; //左边
    int t = rectangle.Top;//上边
    int w = rectangle.Width;//宽度
    int h = rectangle.Height;//高度

    g.DrawLine(p, l
    + 10, t + 20, l + w - 10, t + 20);//上边线
    g.DrawLine(p, l + w - 10, t + 20, l + w - 10, t + h - 10); //右边线
    g.DrawLine(p, l + w - 10, t + h - 10, l + 10, t + h - 10); //下边线
    g.DrawLine(p, l + 10, t + h - 10, l + 10, t + 20); //左边线

    Rectangle r
    = new Rectangle(l + 10, t + 20, w - 18, h - 28);


    g.FillRectangle(
    new SolidBrush(Color.Wheat), r);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
    Pen pen
    = new Pen(borderColor);
    Brush brush
    = new SolidBrush(BackColor);
    e.Graphics.FillPath(brush, gpRegion);
    DrawText(e.Graphics);
    DrawBorder(e.Graphics,pen);
    DrawInner(e.Graphics,
    this.ClientRectangle,new Pen(Color.Red));
    DrawUpperShine(e.Graphics);
    }

    }
    }

    后续做详细的解释。

  • 相关阅读:
    css3动画事件 animationend animationiteration animationstart
    dom对象---增加class属性,去除class属性
    数组的indexOf() 方法
    line-height中的五种取值方式和继承
    html 中line-height相关的四种box模型
    真正的能理解CSS中的line-height,height与line-height
    background-size:contain与cover的区别
    激活win10系统
    table-layout:fixed 应用
    js数组fill()方法
  • 原文地址:https://www.cnblogs.com/scy251147/p/2125529.html
Copyright © 2011-2022 走看看