博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#实现winform仿div+css半透明遮罩效果
阅读量:5914 次
发布时间:2019-06-19

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

本文实现在winform项目实现网页div+css关透明效果,挺帅的,在网页中要实现div的半透明遮罩层效果不难,在winform项目就不是这么容易了,下面我们来看下效果图先:   正常时:

文章来自学IT网:

下面我们来实现上图的效果控件源码:

using System;using System.Collections.Generic;using System.Text;using System.Drawing;using System.Windows.Forms;using System.ComponentModel;namespace MyOpaqueLayer{    [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 showLoadingImage)        {            SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);            base.CreateControl();            this._alpha = Alpha;            ;            if (showLoadingImage)            {                PictureBox pictureBox_Loading = new PictureBox();                pictureBox_Loading.BackColor = System.Drawing.Color.White;                pictureBox_Loading.Image = global::MyOpaqueLayer.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);        }        ///         /// 自定义绘制窗体        ///         ///         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 |= 0x20;  // 开启 WS_EX_TRANSPARENT,使控件支持透明                return cp;            }        }        [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();            }        }    }}文章来自学IT网:http://www.xueit.com/html/2010-01-08/21-1076980691437.html

  下载:

转载地址:http://aygpx.baihongyu.com/

你可能感兴趣的文章
如何将经纬度利用Google Map API显示C# VS2005 Sample Code
查看>>
开发人员可以提高效率的chrome插件推荐
查看>>
性能测试分享:性能测试工具开发的案例分享(下)
查看>>
linux sar命令详解
查看>>
通过Gearman实现MySQL到Redis的数据复制
查看>>
eclipse 自动为getter和setter添加注释
查看>>
我的友情链接
查看>>
DataSet
查看>>
python unittest之断言及示例
查看>>
online_judge_1106
查看>>
JAVA_内部类
查看>>
虚拟机linux上网问题
查看>>
XMLHttpRequest - 原始AJAX初步
查看>>
csu2161: 漫漫上学路(Hash+最短路)
查看>>
如何避免重构带来的危险
查看>>
有序的双链表
查看>>
MSSQLServer的备份与还原
查看>>
使用MySQL yum源安装MySQL
查看>>
iOS8中使用CoreLocation定位
查看>>
mvn package时设置了maven.test.skip=true依旧执行单元测试
查看>>