using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ryControls
{
    /// <summary>
    /// 透明图片控件
    /// </summary>
    [ToolboxBitmap(typeof(ZhLoading))]
    public partial class ZhLoading : Control
    {
        private bool _isTransparent = true;//是否透明
        /// <summary>
        /// 是否使用透明,默认为True
        /// </summary>
        [Category("XX控件"), Description("是否使用透明,默认为True")]
        public bool IsTransparent
        {
            get { return _isTransparent; }
            set { _isTransparent = value; }
        }

        private int _alpha = 125;//设置透明度
        /// <summary>
        /// 设置透明度
        /// </summary>
        [Category("XX控件"), Description("设置透明度")]
        public int Alpha
        {
            get { return _alpha; }
            set { _alpha = value; }
        }

        private Control _byControl;
        /// <summary>
        /// 要绑定的控件
        /// </summary>
        [Category("XX控件"), Description("要绑定的控件")]
        public Control ByControl
        {
            get { return _byControl; }
            set
            {
                _byControl = value;
                if (_byControl != null)
                {
                    _byControl.Controls.Add(this);
                }
            }
        }
        /// <summary>
        /// 透明图片控件
        /// </summary>
        public ZhLoading() : this(125, false,null)
        {
            InitializeComponent();
        }
        /// <summary>
        /// 图片
        /// </summary>
        public Image LoadingImage
        {
            get { return pic.Image; }
            set { pic.Image = value;pic.Visible = value != null; }
        }
        PictureBox pic = new PictureBox();
        /// <summary>
        /// 初始化加载控件
        /// </summary>
        /// <param name="Alpha">透明度</param>
        /// <param name="IsShowLoadingImage">是否显示加载图片</param>
        /// <param name="img">图片</param>
        public ZhLoading(int Alpha, bool IsShowLoadingImage, Image img)
        {
            SetStyle(ControlStyles.Opaque, true);//设置背景透明
            base.CreateControl();
            _alpha = Alpha;
            pic.BackColor = Color.Transparent;
            pic.Image = img;
            pic.Size = new Size(48, 48);
            pic.SizeMode = PictureBoxSizeMode.AutoSize;
            pic.Location = new Point(this.Location.X + (this.Width - pic.Width) / 2,
                this.Location.Y + (this.Height - pic.Height) / 2);//居中
            pic.Anchor = AnchorStyles.None;
            this.Controls.Add(pic);
            pic.Visible = IsShowLoadingImage;
            this.Visible = false;
        }
        /// <summary>
        /// 
        /// </summary>
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; // 开启 WS_EX_TRANSPARENT,使控件支持透明
                return cp;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pe"></param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            Pen labelBorderPen;
            SolidBrush labelBackColorBrush;
            if (_isTransparent)
            {
                Color cl = Color.FromArgb(_alpha, this.BackColor);
                labelBorderPen = new Pen(cl, 0);
                labelBackColorBrush = new SolidBrush(cl);
            }
            else
            {
                labelBorderPen = new Pen(this.BackColor, 0);
                labelBackColorBrush = new SolidBrush(this.BackColor);
            }
            base.OnPaint(pe);
            pe.Graphics.DrawRectangle(labelBorderPen, 0, 0, this.Width, this.Height);
            pe.Graphics.FillRectangle(labelBackColorBrush, 0, 0, this.Width, this.Height);
        }

        /// <summary>
        /// 显示加载
        /// </summary>
        public void ShowLoad()
        {
            pic.Size = new Size(48, 48);
            pic.SizeMode = PictureBoxSizeMode.AutoSize;
            pic.Location = new Point(this.Location.X + (this.Width - pic.Width) / 2,
                this.Location.Y + (this.Height - pic.Height) / 2);//居中
            this.BringToFront();
            this.Dock = DockStyle.Fill;
            this.Visible = true;
        }

        /// <summary>
        /// 隐藏加载
        /// </summary>
        public void HideLoad()
        {
            this.Visible = false;
        }
    }
}