C#實現Winform自訂半透明遮罩層

來源:互聯網
上載者:User

在網頁中通過div+css實現半透明效果不難,今天我們看看一種在winfrom中實現的方法:

如下,正常時:

顯示遮罩層時:

自訂遮罩層控制項的源碼如下:

View Code

using System;using System.Drawing;using System.Windows.Forms;using System.ComponentModel;namespace MyOpaqueLayer{    /// <summary>    /// 自訂控制項:半透明控制項    /// </summary>    /*      * [ToolboxBitmap(typeof(MyOpaqueLayer))]     * 用於指定當把你做好的自訂控制項添加到工具列時,工具列顯示的表徵圖。     * 正確寫法應該是     * [ToolboxBitmap(typeof(XXXXControl),"xxx.bmp")]     * 其中XXXXControl是你的自訂控制項,"xxx.bmp"是你要用的表徵圖名稱。    */    [ToolboxBitmap(typeof(MyOpaqueLayer))]    public class MyOpaqueLayer : System.Windows.Forms.Control    {        private bool _transparentBG = true;//是否使用透明        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.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);        }        /// <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;  // 開啟 WS_EX_TRANSPARENT,使控制項支援透明                return cp;            }        }        /*         * [Category("myOpaqueLayer"), Description("是否使用透明,預設為True")]         * 一般用於說明你自訂控制項的屬性(Property)。         * Category用於說明該屬性屬於哪個分類,Description自然就是該屬性的含義解釋。         */        [Category("MyOpaqueLayer"), Description("是否使用透明,預設為True")]        public bool TransparentBG        {            get            {                return _transparentBG;            }            set            {                _transparentBG = value;                this.Invalidate();            }        }        [Category("MyOpaqueLayer"), Description("設定透明度")]        public int Alpha        {            get            {                return _alpha;            }            set            {                _alpha = value;                this.Invalidate();            }        }    }}

OpaqueCommand的方法:ShowOpaqueLayer(顯示遮罩層)和HideOpaqueLayer(隱藏遮罩層)

View Code

using System;using System.Windows.Forms;namespace 載入中{    class OpaqueCommand    {        private MyOpaqueLayer.MyOpaqueLayer m_OpaqueLayer = null;//半透明蒙板層        /// <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.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);            }        }    }}

源碼下載:自訂半透明遮罩層-源碼.rar

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.