当前位置: 首页 > news >正文

[C#] Winform - 加载动画效果

一、预览效果

二、创建项目

1、创建应用

2、添加自定义控件

using System;
using System.Drawing;
using System.Windows.Forms;
using WindowsFormsApp.LoadingControlDemo.Helpers;namespace WindowsFormsApp.LoadingControlDemo.Controls
{public partial class LoadingControl : Control{private Image _image;public Image Image{get { return _image; }set { _image = value; }}private float _angle = 0;public LoadingControl(){InitializeComponent();timer1.Start();}protected override void OnPaint(PaintEventArgs pe){base.OnPaint(pe);AppHelper.RotateImage(_image, pe.Graphics, _angle, Width);}private void Timer1_Tick(object sender, EventArgs e){_angle += 18;if (_angle > 360)_angle -= 360;Invalidate();}}
}

重点:

1、重写“OnPaint”方法,旋转图片;

2、使用Timer,在Tick方法中改变“角度值”,并触发OnPaint。 

3、使用自定义控件

1)从工具箱中拖拽“LoadingControl”控件到MainForm窗体:

2)在MainForm.cs后台代码里面绑定Image:

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;namespace WindowsFormsApp.LoadingControlDemo
{public partial class MainForm : Form{public MainForm(){InitializeComponent();loadingControl1.Image = Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "loading.png"));}}
}

三、帮助文件

AppHelper.cs

using System;
using System.Drawing;namespace WindowsFormsApp.LoadingControlDemo.Helpers
{internal class AppHelper{public static void RotateImage(Image image, Graphics g, float angle, int radius){if (image == null)return;int edge = (int)(radius / Math.Sqrt(2));int space = (radius - edge) / 2;g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;g.TranslateTransform(edge / 2, edge / 2);g.RotateTransform(angle);g.TranslateTransform(-edge / 2, -edge / 2);g.DrawImage(image, space, space, edge - space * 2, edge - space * 2);}}
}

 重点:

1、edge:表示LoadingControl控件(正方形)里面内切圆的内接正方形的边长,保证Image在旋转时不会溢出;

2、space:表示Image的边距,保证Image水平和垂直居中显示。

http://www.lryc.cn/news/599343.html

相关文章:

  • 【blender小技巧】使用blender实现图转换为3D模型,并进行模型网格优化减面操作
  • 【C#学习Day12笔记】抽象类、密封类与子类构造(继承)
  • Welcome to the world of Go language
  • blender基本操作
  • gem5和Spike区别
  • 设计模式在Java中的实际应用:单例、工厂与观察者模式详解
  • AVL树和红黑树的特性以及模拟实现
  • 【开发杂谈】用AI玩AI聊天游戏:使用 Electron 和 Python 开发大模型语音聊天软件
  • golang怎么实现每秒100万个请求(QPS),相关系统架构设计详解
  • MyBatis 之缓存机制核心解析
  • “磁”力全开:钕铁硼重塑现代科技生活
  • 求职招聘小程序源码招聘小程序开发定制
  • 解密国密 SSL 证书:SM2、SM3、SM4 算法的协同安全效应
  • Spring Boot 接口安全设计:接口限流、防重放攻击、签名验证
  • SEC_FirePower 第二天作业
  • 软件异常读写威胁硬盘安全:从过往案例到防护之道
  • Linux运维新人自用笔记(Rsync远程传输备份,服务端、邮箱和客户端配置、脚本)
  • 网络资源模板--基于Android Studio 实现的天气预报App
  • Inception网络架构:深度学习视觉模型的里程碑
  • Java-Properties类和properties文件详解
  • android app适配Android 15可以在Android studio自带的模拟器上进行吗,还是说必须在真机上进行
  • 【Android Studio】安装Trae插件后Android Studio 启动崩溃问题处理
  • AR眼镜重塑外科手术导航:精准“透视”新突破
  • 深入理解 TCP 协议:从原理到实践的技术解析
  • 机器学习之knn算法保姆级教学
  • 扣子平台之提示词生成
  • 双指针算法介绍及使用(下)
  • 进阶向:基于Python的局域网聊天工具(端对端加密)
  • Amazon Bedrock中的Stability AI文本转图像模型:技术原理、应用实践与未来趋势
  • 创始人IP:知识变现的核心资产