WinForm 基于.NET6使用PNG图片实现透明背景启动页面(文末附加.NET Framework 4.8相关操作)

参考

环境

软件/系统 版本 说明
Windows windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器
Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.13.6
.NET SDK 6.0

预览

image

相关知识点

  1. 启动页需要以ShowDialog形式启动。
  2. UI更新需要在所在线程更新,或者通过 BeginInvokeInvoke操作 ,否则报错System.InvalidOperationException:“线程间操作无效: 从不是创建控件“progressBar1”的线程访问它。”
  3. BeginInvokeInvoke区别在于执行方式的同步性和异步性。
  4. 窗体设置前景色TransparencyKey透明才可以实现透明效果。
  5. WinFrom 支持多种方式引入资源文件,测试 .net6 出现 Resources.resx 导入图片后,图片的类型变成了 Bitmap,需要以Image.FromHbitmap(Properties.Resources._1747273852844.GetHbitmap())形式进行读取,才能给 PictureBox 设置图片,但是会变成非透明图片,可能需要重新绘制。

源码

布局需要给 StartPage.cs 拖拽 Label、ProgressBar、PictureBox 三个控件,然后隐藏窗体工具栏。

  1. Program.cs
    namespace WinFormsApp4
    {
    	internal static class Program
    	{
    		/// <summary>
    		///  The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main()
    		{
    			// To customize application configuration such as set high DPI settings or default font,
    			// see https://aka.ms/applicationconfiguration.
    			ApplicationConfiguration.Initialize();
    			StartPage startPage = new StartPage();
    			startPage.ShowDialog();
    			Application.Run(new Form1());
    		}
    	}
    }
    
  2. StartPage.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify;
    
    namespace WinFormsApp4
    {
    	public partial class StartPage : Form
    	{
    		private Thread thread;
    		public StartPage()
    		{
    			InitializeComponent();
    			// 透明
    			this.TransparencyKey = this.BackColor;
    			// 居中
    			this.StartPosition = FormStartPosition.CenterScreen;
    			// 通过界面上操作给pictureBox1添加图片是透明图片,通过这种方式可能不是透明图片
    			// this.pictureBox1.Image = Image.FromHbitmap(Properties.Resources._1747273852844.GetHbitmap());
    			// 加载动画
    			thread = new Thread(LoadCallBack);
    			thread.IsBackground = true;
    			thread.Start();
    		}
    
    		private void LoadCallBack()
    		{
    			for (int i = 0; i < 100; i++)
    			{
    				Thread.Sleep(60);
    				// 在创建控件的基础句柄所在线程上异步执行指定委托。
    				this.BeginInvoke(() =>
    				{
    					this.progressBar1.Value = i;
    					this.label1.Text = "程序启动中" + String.Concat(Enumerable.Repeat(".", i % 6));
    				});
    			}
    			this.BeginInvoke(() =>
    			{
    				this.Close();
    			});
    		}
    	}
    }
    
    
  3. Form1.cs
    	namespace WinFormsApp4
    	{
    		public partial class Form1 : Form
    		{
    			public Form1()
    			{
    				InitializeComponent();
    				// 居中
    				this.StartPosition = FormStartPosition.CenterScreen;
    			}
    		}
    	}
    

.NET Framework 4.8 实现更新进度条

  1. 线程方式
    // 线程方式
    private void Starting_Load(object sender, EventArgs e)
    {
    	Thread thread = new Thread(UpdateProgress);
    	thread.IsBackground = true;
    	thread.Start();
    }
    private void UpdateProgress()
    {
    	for (int i = 0; i < 100; i++)
    	{
    		Thread.Sleep(20);
    		this.BeginInvoke(new Action(() =>
    									{
    										this.progressBar1.Value = i;
    										this.label1.Text = "程序启动中" + String.Concat(Enumerable.Repeat(".", i % 6));
    
    									}));
    	}
    	this.BeginInvoke(new Action(() =>
    								{
    									this.Close();
    								}));
    
    }
    // 线程方式end
    
  2. 计时器方式
    // 计时器方式
    private void Starting_Load(object sender, EventArgs e)
    {
    	this.timer1.Start();
    }
    
    private int currentProgress = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {
    	++currentProgress;
    	this.progressBar1.Value = currentProgress;
    	this.label1.Text = "程序启动中" + String.Concat(Enumerable.Repeat(".", currentProgress % 6));
    	if (currentProgress >= 100)
    	{
    		this.timer1.Stop();
    		this.Close();
    	}
    }
    // 计时器方式end
    
posted @ 2025-05-15 11:42  夏秋初  阅读(30)  评论(0)    收藏  举报