首页 > 其他分享 >[转载]WinForm全窗口显示遮罩层效果和进度条

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

时间:2023-04-11 09:00:56浏览次数:29  
标签:遮罩 进度条 int void progressBar new true public 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

标签:遮罩,进度条,int,void,progressBar,new,true,public,WinForm
From: https://www.cnblogs.com/east115/p/17305015.html

相关文章

  • C#编程之c#串口(winform、wpf)
    本文主要向大家介绍了C#编程之c#串口通信讲解(一)(winform、wpf),通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。串口操作需要注意的几点如下:1、如果是USB转串口;则需要安装USB转串口驱动,附件有此驱动。2、串口打开状态最好不要直接插拔串口,可能会导致中控板或者串口线烧坏。......
  • C# WinForm操作配置文件AppSettings获取、增加、删除、修改
    在C#WinForm开发中,如果想要修改AppSettings中的值,发现用下面这个代码并没有成功。ConfigurationManager.AppSettings.Set(key,value);//修改值,但是没有成功下面提供可以用的获取、增加、删除、修改appSettings的方法。publicclassWinConfigHelper{///<summary>......
  • Winform/Caharp中使用HttpClient时添加请求头以及响应文件流实现文件下载
    场景Winform中怎样使用HttpClient调用http的get和post接口并将接口返回json数据解析为实体类:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124157296在上面使用HttpClient调用get以及post接口的基础上,如何在请求接口时添加请求头,比如接口需要鉴权,获取token之后......
  • C# 启动winform程序并传参
    启动程序:stringpath="d:\\test.exe";//外部winform程序stringargs="aabbcc";System.Diagnostics.Process.Start(path,args);//启动程序外部winform程序test.exe接收参数:Program.cs///<summary>///应用程序的主入口点。///</sum......
  • vue做播放器和进度条
    <template><video-playerclass="video-playervjs-custom-skin"ref="videoPlayer":playsinline="true":options="playerOptions"@play="onPlayerPlay($event)"@pause="onP......
  • C# winform操作网页文件
    虽然不想,但是活得干,所以还得啊,C#在大学的时候自学过一段时间,算是初学者吧,但已有一年时间没怎么碰过,现在修改实验室里的软件,需要在winform中对本地网页文件进行操作,但我想无论是本地还是网站上的文件其原理都是一样一样的吧。好了废话不多说,现说下问题,1.winform加载一个本地网页文件......
  • 设计进度条
    设计进度条这也是我第一次接触到progress这个标签,就是用来设计进度条的,然后进度条还需要我们后期进行设计,才能让原本的蓝灰配变得更加“灵动”一些哈!最简单的进度条,源码是这样的:<progressid="pro"value="30"></progress>最简单的呈现效果,没有加一点儿修饰的样子是这样的:......
  • WinForm的Listbox控件实现拖拽数据功能
    WinForm的Listbox控件实现拖拽数据功能相关函数:MouseDownDragDropDragEnter思路:当左侧的空间鼠标按下(MouseDown)以后,触发DragDrop操作,然后移动到右侧控件中,将会触发右侧控件的DragEnter事件。右侧同理MouseDown:触发拖动操作DragDrop:从当前容器中拖拽动作完成时的操作(发......
  • Visual Studio创建.net 6的WinForm一直提示"正在加载设计器"
    用VS创建.net6的WinForm,一直停留在"正在加载设计器"的界面*其实不止.net6,也能解决其它非.netframework的WinForm状况 原因1.删除了本地的安装缓存(VS安装时会把安装文件先下载到本地)2.关闭了或没打开NuGet 解决方法1.VS菜单->工具->选项->NuGet包管理器->程......
  • net framwork winform
    winform是窗体项目该winform项目使用的数据库是mysql,vs版本是2022创建一个netframework项目 随便选一个框架版本  将form窗体中加入 一个DataGridView、四个button(新增、更新、删除、查询)按钮、一个textbox。这些控件在 视图---工具栏     把窗体中的......