using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace LYFZ.ComponentLibrary
{
   
    /// <summary>
    /// 透明按钮
    /// </summary>
    [ToolboxBitmap(typeof(TransparentControl))]
   public  class TransparentControl :System.Windows.Forms.Control
    {
        private bool _transparentBG = true;
        private int _alpha = 30;


        private System.ComponentModel.Container components = new System.ComponentModel.Container();


        public TransparentControl()
            : this(30, true)
        {
           
        }

     
        public TransparentControl(int Alpha, bool showLoadingImage)
        {
            try
            {
                this.SetStyle(ControlStyles.DoubleBuffer, true);
                this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                this.SetStyle(ControlStyles.UserPaint, true);
                this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
                this.SetStyle(ControlStyles.StandardDoubleClick, false);
                this.SetStyle(ControlStyles.Selectable, true);
                this.SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
                ResizeRedraw = true;
                BackColor = Color.Transparent;
                ForeColor = Color.White;//初始文本颜色

                Font = new Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);//控件字体


                base.CreateControl();
                this._alpha = Alpha;
                this.thisAlpha = _alpha;
                this.Width = 60;
                this.Height = 60;
            }
            catch { }


            
            //此处可以放置一个显示加载进度的图片
            //if (showLoadingImage)
            //{
            //    PictureBox pictureBox_Loading = new PictureBox();
            //    pictureBox_Loading.BackColor = System.Drawing.Color.White;
            //    pictureBox_Loading.Image = global::MyOpaqueLayer.Properties.Resources.loading;//图片名称叫loading
            //    pictureBox_Loading.Name = "pictureBox_Loading";
            //    pictureBox_Loading.Size = new System.Drawing.Size(48, 48);
            //    pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            //    Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);
            //    pictureBox_Loading.Location = Location;
            //    pictureBox_Loading.Anchor = AnchorStyles.None;
            //    this.Controls.Add(pictureBox_Loading);
            //}
        }


        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!((components == null)))
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private int thisAlpha = 0;

        /// <summary>
        /// 按下
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this._alpha = 20;
            Invalidate(false);
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this._alpha = 35;
            Invalidate(false);
        }

        /// <summary>
        /// 失去焦点后
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this._alpha = thisAlpha;
            Invalidate(false);
        }
        /// <summary>
        /// 鼠标弹起后
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            this._alpha = 35;
            Invalidate(false);
        }
        /// <summary>
        /// 鼠标移上控件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this._alpha = 35;
            Invalidate(false);
        }
        /// <summary>
        /// 鼠标离开控件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            this._alpha = thisAlpha;
            Invalidate(false);

        }

        /// <summary>
        /// 自定义绘制窗体
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            
           
            
            float vlblControlWidth;
            float vlblControlHeight;
            base.InvokePaintBackground(this, new PaintEventArgs(e.Graphics, base.ClientRectangle));
            Pen labelBorderPen;
            SolidBrush labelBackColorBrush;
            Color drawColor = Color.FromArgb(this._alpha, this.BackColor);
            
            if (_transparentBG)
            {
                labelBorderPen = new Pen(drawColor, 0);
                labelBackColorBrush = new SolidBrush(drawColor);
            }
            else
            {
                labelBorderPen = new Pen(this.BackColor, 0);
                labelBackColorBrush = new SolidBrush(this.BackColor);
            }
           // base.OnPaint(e);
            vlblControlWidth = this.Size.Width;
            vlblControlHeight = this.Size.Height;
            Rectangle rele= new Rectangle(0, 0, this.Size.Width, this.Size.Height);
           
            
           
            e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
            e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);
            
            
        }
        /// <summary>
        /// 
        /// </summary>
        protected override CreateParams CreateParams//v1.10 
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;  // 开启 WS_EX_TRANSPARENT,使控件支持透明
                return cp;
            }
        }

        [Category("TransparentControl"), Description("是否使用透明,默认为True")]
        public bool TransparentBG
        {
            get { return _transparentBG; }
            set
            {
                _transparentBG = value;
                this.Invalidate();
            }
        }

        [Category("TransparentControl"), Description("设置透明度")]
        public int Alpha
        {
            get { return _alpha; }
            set
            {
                if (value <= 255 && value>=0)
                {
                    _alpha = value;
                    thisAlpha = _alpha;
                }
                this.Invalidate();
            }
        }

    }
}