• 【C#】Winform实现轮播图


    复制后,需要修改的代码:

    1、图片文件夹路劲:string folderPath = "C:\\Users\\Administrator\\Desktop\\images";

    2、项目命名空间:namespace BuildAction

    全窗口代码:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using System.IO;
    11. using System.Threading;
    12. namespace BuildAction
    13. {
    14. public partial class Form1 : Form
    15. {
    16. private string[] imageFiles; // 存储图片文件路径
    17. private int currentIndex = 0; // 当前显示的图片索引
    18. private Thread rotateThread; // 轮播线程
    19. public Form1()
    20. {
    21. InitializeComponent();
    22. }
    23. private void StartRotate()
    24. {
    25. // 读取指定文件夹下的所有图片
    26. string folderPath = "C:\\Users\\Administrator\\Desktop\\images";
    27. imageFiles = Directory.GetFiles(folderPath, "*.png", SearchOption.TopDirectoryOnly);
    28. rotateThread = new Thread(() =>
    29. {
    30. while (true)
    31. {
    32. // 显示下一张图片
    33. currentIndex = (currentIndex + 1) % imageFiles.Length;
    34. this.Invoke(new Action(() =>
    35. {
    36. pictureBox1.Image = Image.FromFile(imageFiles[currentIndex]);
    37. }));
    38. // 等待 2 秒后切换图片
    39. Thread.Sleep(2000);
    40. }
    41. });
    42. rotateThread.IsBackground = true;
    43. rotateThread.Start();
    44. }
    45. private void StopRotate()
    46. {
    47. if (rotateThread != null && rotateThread.IsAlive)
    48. {
    49. rotateThread.Abort();
    50. }
    51. }
    52. private void Form1_Load(object sender, EventArgs e)
    53. {
    54. // 开始轮播
    55. StartRotate();
    56. }
    57. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    58. {
    59. StopRotate();
    60. }
    61. private void button1_Click(object sender, EventArgs e)
    62. {
    63. MessageBox.Show("测试多线程是否堵塞", "提醒");
    64. }
    65. }
    66. }

  • 相关阅读:
    jpsall脚本
    浏览器存储(webStorage)常用API以及简单使用
    sqli-labs/Less-62
    Servlet
    记一次渗透测试事件
    基于JAVA-在线考试系统-计算机毕业设计源码+系统+数据库+lw文档+部署
    LeetCode题:1:两数之和
    嵌入式系统开发笔记108:IO的使用方法与面向对象程序设计
    【Python】FastAPI 项目创建 与 Docker 部署
    音视频直播开发问题分析总结 -- 花屏&绿屏
  • 原文地址:https://blog.csdn.net/oqqHun123/article/details/133876929