[转载]WinForm全窗口显示遮罩层效果和进度条

Posted 东经115

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[转载]WinForm全窗口显示遮罩层效果和进度条相关的知识,希望对你有一定的参考价值。

运行效果如下:

自定义控件核心代码:

/// <summary>
/// 遮罩层
/// </summary>
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public class MaskLayer : Control

    /// <summary>
    /// 遮罩层
    /// </summary>
    public MaskLayer()
    
        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.Opaque, true);
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        CreateControl();
        Visible = false;
 
        this.Dock = DockStyle.Fill;
        this.Controls.Add(progressBar);
    
    /// <summary>
    /// 进度条
    /// </summary>
    public ProgressBar progressBar = new ProgressBar();
 
    /// <summary>
    /// 设置进度条显示值
    /// </summary>
    /// <param name="value"></param>
    public void SetProgressBarValue(int value)
    
        this.BeginInvoke(new Action(() =>
        
            if (value <= progressBar.Maximum)
            
                progressBar.Value = value;
            
        ));
    
    private int _Alpha = 125;
    /// <summary>
    /// 透明度<para>范围:0~255(完全透明~完全不透明)</para><para>默认:125(半透明)</para>
    /// </summary>
    [Category("DemoUI"), Description("透明度\\r\\n范围:0~255(完全透明~完全不透明)\\r\\n默认:125(半透明)")]
    public int Alpha
    
        get  return _Alpha; 
        set
        
            if (value < 0) value = 0;
            if (value > 255) value = 255;
            _Alpha = value;
            Invalidate();
        
    
 
    /// <summary>
    /// 是否处于显示状态
    /// </summary>
    [Category("LESLIE_UI"), Description("是否处于显示状态"), Browsable(false)]
    public bool IsShow  get; private set;  = true;
 
 
 
    /// <summary>
    /// OnPaint
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPaint(PaintEventArgs e)
    
        base.OnPaint(e);
        SolidBrush BackColorBrush = new SolidBrush(Color.FromArgb(_Alpha, BackColor));
        e.Graphics.FillRectangle(BackColorBrush, e.ClipRectangle);
        BackColorBrush.Dispose();
    
    /// <summary>
    /// 是否启用点击隐藏功能<para>默认:是</para>
    /// </summary>
    [Category("DemoUI"), Description("是否启用点击隐藏功能\\r\\n默认:否")]
    public bool EnabledClickHide  get; set;  = false;
    /// <summary>
    /// 点击事件
    /// </summary>
    /// <param name="e"></param>
    protected override void OnClick(EventArgs e)
    
        base.OnClick(e);
        if (EnabledClickHide)
        
            HideMask();
        
    
 
    /// <summary>
    /// 显示遮罩层
    /// </summary>
    public void ShowMask()
    
        try
        
            this.BeginInvoke(new Action(() =>
            
                IsShow = true;
                SendKeys.Send("Tab");
 
                BringToFront();
                this.Visible = true;
                this.BackColor = Color.Black;
                Show();
 
                int x = (int)(this.Width * 0.1);
                int y = this.Height / 2;
                this.progressBar.Location = new System.Drawing.Point(x, y);
                this.progressBar.Name = "progressBar";
                int w = (int)(this.Width * 0.8);
                this.progressBar.Size = new System.Drawing.Size(w, 23);
                this.progressBar.TabIndex = 2;
            ));
        
        catch (Exception)
        
        
    
 
    /// <summary>
    /// 隐藏遮罩层
    /// </summary>
    public void HideMask()
    
        try
        
            this.BeginInvoke(new Action(() =>
            
                IsShow = false;
                SendToBack();
                Visible = false;
                Hide();
            ));
        
        catch (Exception)
        
        
     

调用控件:

public partial class Form1 : Form

    MaskLayer lMaskLayer1 = new MaskLayer();
    public Form1()
    
        InitializeComponent();
        this.Controls.Add(lMaskLayer1);
    
 
    private void button1_Click(object sender, EventArgs e)
    
        Thread t3 = new Thread(pShow);
        t3.IsBackground = true;
        t3.Start();
    
 
    void pShow()
    
        lMaskLayer1.ShowMask();
        for (int i = 0; i < 100; i++)
        
            lMaskLayer1.SetProgressBarValue(i);
            Thread.Sleep(200);
        
        lMaskLayer1.HideMask();
    

 链接:https://pan.baidu.com/s/1gc6ofo1xT-AgNkL6BY_vuA?pwd=5shv 
提取码:5shv 

本文来自:罗分明博客 原文:http://www.luofenming.com/show.aspx?id=ART2022052200001

winformprogressbar只显示灰色

参考技术A 原始状态就是灰色。winformprogressbar是进度条控件,用于显示某操作动作进度或跑马灯效果。在原始状态下就是灰色的,ProgressBar控件由一个窗口构成,当操作进行时,该窗口将填充系统突出显示颜色。

以上是关于[转载]WinForm全窗口显示遮罩层效果和进度条的主要内容,如果未能解决你的问题,请参考以下文章

Winform应用程序实现通用遮罩层二

异步下载圆形进度条显示进度

Winform应用程序实现通用遮罩层

点击弹出居中带有透明遮罩层窗口:

点击弹出居中带有透明遮罩层窗口

js弹出div并显示遮罩层