WinForm 基于.NET6使用PNG图片实现透明背景启动页面(文末附加.NET Framework 4.8相关操作)
参考
- 豆包
- https://www.codeleading.com/article/83363029133/
- https://www.delftstack.com/zh/howto/csharp/csharp-repeat-string-x-times/#google_vignette
- https://www.cnblogs.com/liquan/p/8859206.html
- https://bbs.csdn.net/topics/90398756
- https://learn.microsoft.com/zh-cn/dotnet/core/compatibility/core-libraries/5.0/thread-abort-obsolete
- https://www.cnblogs.com/yuxuetaoxp/p/3873074.html
- https://blog.csdn.net/qq_27508477/article/details/87716638
- https://www.cnblogs.com/gzskys/p/5661302.html
- https://www.cnblogs.com/log9527blog/p/17616377.html
- https://blog.csdn.net/kucoffee12/article/details/83538104
- https://wenku.csdn.net/answer/82z74jbqnk
- https://blog.csdn.net/weixin_40333655/article/details/101448061
- https://blog.csdn.net/qq_36240878/article/details/84024369
- https://zhuanlan.zhihu.com/p/203689190
- https://blog.csdn.net/weixin_45023644/article/details/122893571
- https://blog.csdn.net/weixin_44429033/article/details/123645544
- https://bbs.csdn.net/topics/330035058
- https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100110550
环境
软件/系统 | 版本 | 说明 |
---|---|---|
Windows | windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器 | |
Microsoft Visual Studio | Community 2022 (64 位) - Current 版本 17.13.6 | |
.NET SDK | 6.0 |
预览
相关知识点
- 启动页需要以
ShowDialog
形式启动。 - UI更新需要在所在线程更新,或者通过
BeginInvoke
或Invoke
操作 ,否则报错System.InvalidOperationException:“线程间操作无效: 从不是创建控件“progressBar1”的线程访问它。”
BeginInvoke
、Invoke
区别在于执行方式的同步性和异步性。- 窗体设置前景色
TransparencyKey
透明才可以实现透明效果。 - WinFrom 支持多种方式引入资源文件,测试 .net6 出现
Resources.resx
导入图片后,图片的类型变成了 Bitmap,需要以Image.FromHbitmap(Properties.Resources._1747273852844.GetHbitmap())
形式进行读取,才能给PictureBox
设置图片,但是会变成非透明图片,可能需要重新绘制。
源码
布局需要给
StartPage.cs
拖拽 Label、ProgressBar、PictureBox 三个控件,然后隐藏窗体工具栏。
- 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()); } } }
- 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(); }); } } }
- Form1.cs
namespace WinFormsApp4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // 居中 this.StartPosition = FormStartPosition.CenterScreen; } } }
.NET Framework 4.8 实现更新进度条
- 线程方式
// 线程方式 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
- 计时器方式
// 计时器方式 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
博 主 :夏秋初
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18877388
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18877388
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。