效果图如下,正常时:
遮罩时:
控件代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Drawing;
usingSystem.Windows.Forms;
ponentModel;
namespaceMyOpaqueLayer
{
[ToolboxBitmap(typeof(MyOpaqueLayer))]
publicclassMyOpaqueLayer:System.Windows.Forms.Control
{
privatebool_transparentBG=true;
privateint_alpha=125;
ponentModel.Containercomponents=ponentModel.Container();
publicMyOpaqueLayer()
:this(125,true)
{
}
publicMyOpaqueLayer(intAlpha,boolshowLoadingImage)
{
SetStyle(System.Windows.Forms.ControlStyles.Opaque,true);
base.CreateControl();
this._alpha=Alpha;
;
if(showLoadingImage)
{
PictureBoxpictureBox_Loading=newPictureBox();
pictureBox_Loading.BackColor=System.Drawing.Color.White;
pictureBox_Loading.Image=global::MyOpaqueLayer.Properties.Resources.loading;
pictureBox_Loading.Name="pictureBox_Loading";
pictureBox_Loading.Size=newSystem.Drawing.Size(48,48);
pictureBox_Loading.SizeMode=System.Windows.Forms.PictureBoxSizeMode.AutoSize;
PointLocation=newPoint(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);
}
}
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(!((components==null)))
{
components.Dispose();
}
}
base.Dispose(disposing);
}
///<summary>
///自定义绘制窗体
///</summary>
///<paramname="e"></param>
protectedoverridevoidOnPaint(System.Windows.Forms.PaintEventArgse)
{
floatvlblControlWidth;
floatvlblControlHeight;
PenlabelBorderPen;
SolidBrushlabelBackColorBrush;
if(_transparentBG)
{
ColordrawColor=Color.FromArgb(this._alpha,this.BackColor);
labelBorderPen=newPen(drawColor,0);
labelBackColorBrush=newSolidBrush(drawColor);
}
else
{
labelBorderPen=newPen(this.BackColor,0);
labelBackColorBrush=newSolidBrush(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>
protectedoverrideCreateParamsCreateParams//v1.10
{
get
{
CreateParamscp=base.CreateParams;
cp.ExStyle|=0x20;//开启WS_EX_TRANSPARENT,使控件支持透明
returncp;
}
}
[Category("myOpaqueLayer"),Description("是否使用透明,默认为True")]
publicboolTransparentBG
{
get{return_transparentBG;}
set
{
_transparentBG=value;
this.Invalidate();
}
}
[Category("myOpaqueLayer"),Description("设置透明度")]
publicintAlpha
{
get{return_alpha;}
set
{
_alpha=value;
this.Invalidate();
}
}
}
}
源码下载:
遮罩层源码