在Winform中实现半透明遮罩层

本文介绍了一种自定义Windows Forms控件的方法,通过设置不同的透明度和背景选项,可以在界面上创建带有遮罩效果的控件。文章提供了完整的C#代码实现,并展示了如何在控件中加入加载图标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图如下,正常时:

遮罩时:

控件代码:

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();
            }
        }

    }


}

 

源码下载:

遮罩层源码

 

 

转载于:https://www.cnblogs.com/zhaobl/archive/2010/01/08/1642145.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值