Custom Control: Translucent Controls

Original link: https://blog.csdn.net/y38701522/article/details/8878299 

/// <Summary> /// Custom Controls: Controls translucent /// </ Summary> / * * [the ToolboxBitmap (typeof (MyOpaqueLayer))] * is used to specify when you do add custom controls to the toolbar, the toolbar icon displays. * Correct wording should be * [the ToolboxBitmap (typeof (XXXXControl), "xxx.bmp")] * where XXXXControl your custom control, "xxx.bmp" is the name of the icon you want to use. * / [The ToolboxBitmap ( typeof (MyOpaqueLayer))] public class MyOpaqueLayer: the System.Windows.Forms.Control { Private BOOL _transparentBG = to true ; // if transparent private int _alpha = 125;//设置透明度 private System.ComponentModel.Container components = new System.ComponentModel.Container(); public MyOpaqueLayer() : this(125, true) { } public MyOpaqueLayer(int Alpha, bool IsShowLoadingImage) { SetStyle(System.Windows.Forms.ControlStyles.Opaque, true); base.CreateControl(); this._alpha = Alpha; if (IsShowLoadingImage) { PictureBox pictureBox_Loading = new PictureBox(); pictureBox_Loading.BackColor = System.Drawing.Color.White; pictureBox_Loading.Image = Properties.Resources.logining; 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); } /// <summary> /// 自定义绘制窗体 /// </summary> /// <param name="e"></param> protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { float vlblControlWidth; float vlblControlHeight; Pen labelBorderPen; SolidBrush labelBackColorBrush; if (_transparentBG) { Color drawColor = Color.FromArgb(this._alpha, this.BackColor); 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; e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight); e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight); } protected override CreateParams CreateParams// V1.10 { GET { the CreateParams CP = Base .CreateParams; cp.ExStyle | = 0x00000020 ; // 0x20; // open WS_EX_TRANSPARENT, a transparent support so that the control return CP; } } / * * [the Category ( "myOpaqueLayer") , Description ( "whether to use transparent, the default is True")] * general Description property for your custom control (property). * Category for describing the properties belong to which category, Description is the meaning of the natural attributes explain. * / [The Category ( " MyOpaqueLayer " ), the Description ( "Whether transparent, the default is True " )] public BOOL TransparentBG { GET { return _transparentBG; } SET { _transparentBG = value; the this .Invalidate (); } } [the Category ( " MyOpaqueLayer " ), the Description ( " set the transparency " )] public int the Alpha { GET { return the _alpha; } SET { the _alpha = value; the this .Invalidate (); } } } class OpaqueCommand { Private MyOpaqueLayer m_OpaqueLayer = null ; // semitransparent mask layer /// <Summary> /// display mask layer /// </ Summary> /// <param name = "control"> control </ param> /// <param name = "Alpha"> transparency </ param> /// <param name = "isShowLoadingImage">Whether to display the icon </ param> public void ShowOpaqueLayer(Control control, int alpha, bool isShowLoadingImage) { try { if (this.m_OpaqueLayer == null) { this.m_OpaqueLayer = new MyOpaqueLayer(alpha, isShowLoadingImage); control.Controls.Add(this.m_OpaqueLayer); this.m_OpaqueLayer.Dock = DockStyle.Fill; this.m_OpaqueLayer.BringToFront(); } this.m_OpaqueLayer.Enabled = true; this.m_OpaqueLayer.Visible = true; } catch { } } /// <summary> /// 隐藏遮罩层 /// </summary> public void HideOpaqueLayer() { try { if (this.m_OpaqueLayer != null) { this.m_OpaqueLayer.Visible = false; this.m_OpaqueLayer.Enabled = false; } } catch (Exception ex) { //MessageBox.Show(ex.Message); } } }

 

  /// <summary> /// custom: translucent controls /// </ summary> / * * [ToolboxBitmap (typeof (MyOpaqueLayer))] * is used to specify when you add custom controls to do when the toolbar, the toolbar icon displays. * Correct wording should be * [ToolboxBitmap (typeof (XXXXControl) , "xxx.bmp")] * where XXXXControl your custom control, "xxx.bmp" is the name of the icon you want to use. * / [ToolboxBitmap (typeof (MyOpaqueLayer ))] public class MyOpaqueLayer: System.Windows.Forms.Control {private bool _transparentBG = true; // clear whether private int _alpha = 125; // set the transparency
        private System.ComponentModel.Container = new new System.ComponentModel.Container Components ();
        public MyOpaqueLayer (): the this (125, to true) {}
        public MyOpaqueLayer(int Alpha, bool IsShowLoadingImage)        {            SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);            base.CreateControl();
            this._alpha = Alpha;            if (IsShowLoadingImage)            {                PictureBox pictureBox_Loading = new PictureBox();                pictureBox_Loading.BackColor = System.Drawing.Color.White;                pictureBox_Loading.Image = Properties.Resources.logining;                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);        }
        /// <summary>        /// 自定义绘制窗体        /// </summary>        /// <param name="e"></param>        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)        {            float vlblControlWidth;            float vlblControlHeight;
            Pen labelBorderPen;            SolidBrush labelBackColorBrush;
            if (_transparentBG)            {                Color drawColor = Color.FromArgb(this._alpha, this.BackColor);                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;            e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);            e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);        }

        protected override CreateParams CreateParams // v1.10 {get {CreateParams cp = base.CreateParams; cp.ExStyle | = 0x00000020; // 0x20; // open WS_EX_TRANSPARENT, so that the transparent support control CP return;}}
        / * * [the Category ( "myOpaqueLayer"), Description ( "whether to use transparent, the default is True")] * general Description property for your custom control (property). * Category for describing the properties belong to which category, Description is the meaning of the natural attributes explain. * / [Category ( "MyOpaqueLayer" ), Description ( " whether transparent, the default is True")] public bool TransparentBG { get {return _transparentBG;} set {_transparentBG = value; this.Invalidate ();}}
        [Category ( "MyOpaqueLayer"), Description ( " Transparency settings")] public int {return the _alpha the Alpha {GET; SET} = {the _alpha value; this.Invalidate ();}}}
    class OpaqueCommand {Private MyOpaqueLayer m_OpaqueLayer = null; // translucent mask layer
        /// <summary>        /// 显示遮罩层        /// </summary>        /// <param name="control">控件</param>        /// <param name="alpha">透明度</param>        /// <param name="isShowLoadingImage">是否显示图标</param>        public void ShowOpaqueLayer(Control control, int alpha, bool isShowLoadingImage)        {            try            {                if (this.m_OpaqueLayer == null)                {                    this.m_OpaqueLayer = new MyOpaqueLayer(alpha, isShowLoadingImage);                    control.Controls.Add(this.m_OpaqueLayer);                    this.m_OpaqueLayer.Dock = DockStyle.Fill;                    this.m_OpaqueLayer.BringToFront();                }                this.m_OpaqueLayer.Enabled = true;                this.m_OpaqueLayer.Visible = true;            }            catch { }        }
        /// <summary>        /// 隐藏遮罩层        /// </summary>        public void HideOpaqueLayer()        {            try            {                if (this.m_OpaqueLayer != null)                {                    this.m_OpaqueLayer.Visible = false;                    this.m_OpaqueLayer.Enabled = false;                }            }            catch (Exception ex)            {                //MessageBox.Show(ex.Message);            }        }    }

Guess you like

Origin www.cnblogs.com/chengjunwei/p/11350508.html