在Winform中实现半透明遮罩层
2010-01-08 13:29
585 查看
效果图如下,正常时:
遮罩时:
控件代码:
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);
}
/// <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);
}
/// <summary>
///
/// </summary>
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();
}
}
}
}
源码下载:
遮罩层源码
遮罩时:
控件代码:
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);
}
/// <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);
}
/// <summary>
///
/// </summary>
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();
}
}
}
}
源码下载:
遮罩层源码
相关文章推荐
- C# Winform 实现自定义半透明loading加载遮罩层
- Winform应用程序实现通用遮罩层
- WinForm 控件实现半透明效果,并可层叠显示
- 在MFC和WinForm中分别实现超酷的半透明异形窗口
- C#实现winform仿div+css半透明遮罩效果
- Winform应用程序实现通用遮罩层
- C# Winform 实现自定义半透明loading加载遮罩层
- WinForm 控件实现半透明效果,并可层叠显示
- Android沉浸式状态栏实现(半透明浮动状态栏)Activity全屏显示
- 如何实现“WinForm窗体禁用关闭按钮”方法功能及源代码
- C#(WinForm)实现软件注册
- CSS opacity-实现图片半透明效果
- C#(Winform)实现条码打印
- C#的委托事件在winform窗体中实现传值备忘
- CSS3+font字体文件实现圆形半透明菜单具体步骤(图解)
- winForm中DataSet实现数据查询、保存和删除
- WinForm中实现控件中的值是否为空的判断
- C#-WinForm-实现学员对象修改
- WinForm中使用XtraGrid控件,实现在界面中动态修改列显示,列名列宽等
- WinForm实现的图片拖拽与缩放功能示例