博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Gradient Panel With Shadow Support And Lots Of Other Stuff
阅读量:4343 次
发布时间:2019-06-07

本文共 13598 字,大约阅读时间需要 45 分钟。

Properties:

Source Code:
Variables

PointF pf;        SizeF sf = new SizeF();        public  enum ImageAlign        {            Custom,            TopLeft,            TopCenter,            TopRight,            MiddleLeft,            MiddleCenter,            MiddleRight,            BottomLeft,            BottomCenter,            BottomRight        }        public enum TextAlign        {            Custom,            TopLeft,            TopCenter,            TopRight,            MiddleLeft,            MiddleCenter,            MiddleRight,            BottomLeft,            BottomCenter,            BottomRight        }

Properties

TextAlign  _TextAlign = TextAlign.TopLeft;        [Browsable(true), Category("GradientPanel")]        public TextAlign TxtAlign        {            get { return _TextAlign; }            set { _TextAlign = value; Invalidate(); }        }        String  _Text;        [Browsable(true), Category("GradientPanel")]        public override string Text        {            get { return _Text; }            set { _Text = value; Invalidate(); }        }        private float _TextMargin = 1.0f;        [Browsable(true), Category("GradientPanel")]        [DefaultValue("1.0f")]        public float TextMargin        {            get { return _TextMargin; }            set { _TextMargin = value; Invalidate(); }        }                ImageAlign _ImageAlign= ImageAlign.TopLeft;        [Browsable(true), Category("GradientPanel")]        public ImageAlign ImgAlign        {            get { return _ImageAlign; }            set { _ImageAlign = value; Invalidate(); }        }        Image _Image;        [Browsable(true), Category("GradientPanel")]        public Image Image        {            get { return _Image; }            set { _Image = value; Invalidate(); }        }        Point _ImageLocation = new Point(4, 4);        [Browsable(true), Category("GradientPanel")]        [DefaultValue("4,4")]        public Point ImageLocation        {            get { return _ImageLocation; }            set { _ImageLocation = value; Invalidate(); }        }        private float _ImageMargin = 1.0f;        [Browsable(true), Category("GradientPanel")]        [DefaultValue("1.0f")]        public float ImageMargin        {            get { return _ImageMargin; }            set { _ImageMargin = value; Invalidate(); }        }        int _BorderWidth = 1;        [Browsable(true), Category("GradientPanel")]        [DefaultValue(1)]        public int BorderWidth        {            get { return _BorderWidth; }            set { _BorderWidth = value; Invalidate(); }        }        int _ShadowOffSet = 5;        [Browsable(true), Category("GradientPanel")]        [DefaultValue(5)]        public int Shadow        {            get            {                return _ShadowOffSet;            }            set { _ShadowOffSet = Math.Abs(value); Invalidate(); }        }         int _RoundCornerRadius = 4;        [Browsable(true), Category("GradientPanel")]        [DefaultValue(4)]        public int CornerRadius        {            get { return _RoundCornerRadius; }            set { _RoundCornerRadius = Math.Abs(value); Invalidate(); }        }              Color _BorderColor = Color.Gray;        [Browsable(true), Category("GradientPanel")]        [DefaultValue("Color.Gray")]        public Color BorderColor        {            get { return _BorderColor; }            set { _BorderColor = value; Invalidate(); }        }        Color _GradientStartColor = Color.White;        [Browsable(true), Category("GradientPanel")]        [DefaultValue("Color.White")]        public Color GradientStartColor        {            get { return _GradientStartColor; }            set { _GradientStartColor = value; Invalidate(); }        }        Color _GradientEndColor = Color.Gray;        [Browsable(true), Category("GradientPanel")]        [DefaultValue("Color.Gray")]        public Color GradientEndColor        {            get { return _GradientEndColor; }            set { _GradientEndColor = value; Invalidate(); }        }        LinearGradientMode _LinearGradientMode = LinearGradientMode.Vertical;        [Browsable(true), Category("GradientPanel")]        public LinearGradientMode GradientMode        {            get { return _LinearGradientMode; }            set { _LinearGradientMode = value; Invalidate(); }        }        Boolean _DrawLine = false;        [Browsable(true), Category("GradientPanel")]        public Boolean DrawLine        {            get { return _DrawLine; }            set { _DrawLine = value; Invalidate(); }        }        Color _LineColor = Color.Black;        [Browsable(true), Category("GradientPanel")]        public Color LineColor        {            get { return _LineColor; }            set { _LineColor = value; Invalidate(); }        }

Constructor

public GradientPanelR()        {            this.SetStyle(ControlStyles.DoubleBuffer, true);            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);            this.SetStyle(ControlStyles.ResizeRedraw, true);            this.SetStyle(ControlStyles.UserPaint, true);            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);            InitializeComponent();        }

Overriding OnPaintBackground method

protected override void OnPaintBackground(PaintEventArgs e)        {            base.OnPaintBackground(e);            int tmpShadowOffSet = Math.Min(Math.Min(_ShadowOffSet, this.Width - 2), this.Height - 2);            int tmpSoundCornerRadius = Math.Min(Math.Min(_RoundCornerRadius, this.Width - 2), this.Height - 2);            if (this.Width > 1 && this.Height > 1)            {                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;                Rectangle rect = new Rectangle(0, 0, this.Width - tmpShadowOffSet - 1, this.Height - tmpShadowOffSet - 1);                Rectangle rectShadow = new Rectangle(tmpShadowOffSet, tmpShadowOffSet, this.Width - tmpShadowOffSet - 1, this.Height - tmpShadowOffSet - 1);                GraphicsPath graphPathShadow = GetRoundPath(rectShadow, tmpSoundCornerRadius);                GraphicsPath graphPath = GetRoundPath(rect, tmpSoundCornerRadius);                if (tmpSoundCornerRadius > 0)                {                    using (PathGradientBrush gBrush = new PathGradientBrush(graphPathShadow))                    {                        gBrush.WrapMode = WrapMode.Clamp;                        ColorBlend colorBlend = new ColorBlend(3);                        colorBlend.Colors = new Color[]{Color.Transparent,              Color.FromArgb(180, Color.DimGray),              Color.FromArgb(180, Color.DimGray)};                        colorBlend.Positions = new float[] { 0f, .1f, 1f };                        gBrush.InterpolationColors = colorBlend;                        e.Graphics.FillPath(gBrush, graphPathShadow);                    }                }                // Draw backgroud                LinearGradientBrush brush = new LinearGradientBrush(rect,                    this._GradientStartColor,                    this._GradientEndColor,                   GradientMode);                e.Graphics.FillPath(brush, graphPath);                e.Graphics.DrawPath(new Pen(Color.FromArgb(180, this._BorderColor), _BorderWidth), graphPath);                // Draw Image                if (_Image != null)                {                    //e.Graphics.DrawImageUnscaled(_Image, _ImageLocation);                    DrawImage(e.Graphics);                }            }            DrawText(e.Graphics);        }

Drawing Image

#region DrawImage        public void DrawImage(Graphics g)        {                        if (this.Image != null && _ImageAlign != ImageAlign.Custom)            {                pf = new PointF(0, 0);                if (_ImageAlign  == ImageAlign.TopLeft)                {                    pf.X = 1; pf.Y = 1;                    pf.X += _ImageMargin;                    pf.Y += _ImageMargin;                    if (_DrawLine)                    {                       // HatchBrush hbr = new HatchBrush(HatchStyle.ZigZag , _LineColor );                        Pen P = new Pen(_LineColor, 3);                        g.DrawLine(P, new PointF(Image.Width + pf.X + 2 + 10, Image.Height + pf.Y + 2), new PointF(Width - 10, Image.Height + pf.Y + 2));                    }                }                if (_ImageAlign == ImageAlign.TopCenter)                {                    pf.X = ((float)Width - (float)Image.Width) / 2;                    pf.Y += _ImageMargin;                }                if (_ImageAlign == ImageAlign.TopRight)                {                    pf.X = (float)Width - (float)Image.Width;                    pf.Y = 1;                    pf.X -= _ImageMargin;                    pf.Y += _ImageMargin;                }                if (_ImageAlign == ImageAlign.MiddleLeft)                {                    pf.X = 1;                    pf.Y = ((float)Height - (float)Image.Height) / 2;                    pf.X += _ImageMargin;                }                if (_ImageAlign == ImageAlign.MiddleCenter)                {                    pf.X = ((float)Width - (float)Image.Width) / 2;                    pf.Y = ((float)Height - (float)Image.Height) / 2;                }                if (_ImageAlign == ImageAlign.MiddleRight)                {                    pf.X = (float)Width - (float)Image.Width;                    pf.Y = ((float)Height - (float)Image.Height) / 2;                    pf.X -= _ImageMargin;                }                if (_ImageAlign == ImageAlign.BottomLeft)                {                    pf.X = 1;                    pf.Y = ((float)Height - (float)Image.Height);                    pf.X += _ImageMargin;                    pf.Y -= _ImageMargin;                }                if (_ImageAlign == ImageAlign.BottomCenter)                {                    pf.X = ((float)Width - (float)Image.Width) / 2;                    pf.Y = ((float)Height - (float)Image.Height);                    pf.Y -= _ImageMargin;                }                if (_ImageAlign == ImageAlign.BottomRight)                {                    pf.X = (float)Width - (float)Image.Width;                    pf.Y = ((float)Height - (float)Image.Height);                    pf.X -= _ImageMargin;                    pf.Y -= _ImageMargin;                }                g.DrawImage(this.Image, pf);            }            if (this.Image != null && _ImageAlign == ImageAlign.Custom)            {                pf = new PointF(0, 0);                pf.X = _ImageLocation.X ;                pf.Y = _ImageLocation.Y;                                g.DrawImage(this.Image, pf);            }        }        #endregion

Drawing Text

#region DrawText          public void DrawText(Graphics g)        {            SolidBrush b = new SolidBrush(this.ForeColor);            pf = new PointF();            sf = g.MeasureString(this.Text, this.Font);            //calculate textalign            if (_TextAlign == TextAlign.TopLeft)            {                pf.X = 1 + _TextMargin ; pf.Y = 1 + _TextMargin ;            }            if (this._TextAlign == TextAlign.TopCenter)            {                                pf.X = ((float)Width - sf.Width) / 2;                pf.Y =1 + _TextMargin ;             }            if (this._TextAlign == TextAlign.TopRight)            {                                pf.X = (float)Width - sf.Width  - _TextMargin ;                pf.Y = 1 + _TextMargin ;            }            if (this._TextAlign == TextAlign.MiddleLeft)            {                                pf.X = 1 + _TextMargin ;                pf.Y =( (float)Height - sf.Height) / 2;            }            if (this._TextAlign == TextAlign.MiddleCenter)            {                pf.X = ((float)Width - sf.Width) / 2;                pf.Y = ((float)Height - sf.Height) / 2;            }            if (this._TextAlign == TextAlign.MiddleRight)            {                pf.X = (float)Width - sf.Width - _TextMargin ;                pf.Y = ((float)Height - sf.Height) / 2;            }            if (this._TextAlign == TextAlign.BottomLeft)            {                pf.X = 1 + _TextMargin ;                pf.Y = ((float)Height - sf.Height) - _TextMargin ;            }            if (this._TextAlign == TextAlign.BottomCenter)            {                pf.X = ((float)Width - sf.Width) / 2;                pf.Y = ((float)Height - sf.Height) - _TextMargin ;            }            if (this._TextAlign == TextAlign.BottomRight)            {                pf.X = (float)Width - sf.Width - _TextMargin;                pf.Y = ((float)Height - sf.Height) - _TextMargin ;            }            //g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias ;            g.DrawString(this.Text, this.Font, b, pf);                    }               #endregion

Other Helping Methods

public static GraphicsPath GetRoundPath(Rectangle r, int depth)        {            GraphicsPath graphPath = new GraphicsPath();            graphPath.AddArc(r.X, r.Y, depth, depth, 180, 90);            graphPath.AddArc(r.X + r.Width - depth, r.Y, depth, depth, 270, 90);            graphPath.AddArc(r.X + r.Width - depth, r.Y + r.Height - depth, depth, depth, 0, 90);            graphPath.AddArc(r.X, r.Y + r.Height - depth, depth, depth, 90, 90);            graphPath.AddLine(r.X, r.Y + r.Height - depth, r.X, r.Y + depth / 2);            return graphPath;        }

For Complete source mail me at : sandeepparekh9@gmail.com

转载于:https://www.cnblogs.com/samblog/archive/2012/04/01/2429265.html

你可能感兴趣的文章
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
java基础 第十一章(多态、抽象类、接口、包装类、String)
查看>>
Hadoop 服务器配置的副本数量 管不了客户端
查看>>