用C#实现多张图片平行移动,图片一直循环的核心算法
用C#实现多张图片平行移动,图片一直循环的核心算法 求指教
using System; using System.Drawing; using System.Threading; using System.Windows.Forms; using Timer = System.Threading.Timer; namespace ImageCycleMove { public partial class FormMain : Form { #region 只读全局字段 private readonly Timer _timer; #endregion #region 全局字段 private int _changeCritical; private int _left; private int _leftPictureBoxIndex; private PictureBox[] _pictureBoxs; #endregion #region 构造函数 public FormMain() { InitializeComponent(); _timer = new Timer(TimerProcess, null, Timeout.Infinite, Timeout.Infinite); Closing += FormMain_Closing; NudImageCount.ValueChanged += StartCycleMove; NudImageMargin.ValueChanged += StartCycleMove; NudInterval.ValueChanged += NudInterval_ValueChanged; SizeChanged += StartCycleMove; } #endregion #region 控件事件 void ButStop_Click(object sender, EventArgs e) { _timer.Change(Timeout.Infinite, Timeout.Infinite); ButStop.Enabled = false; ButStart.Enabled = true; } void FormMain_Closing(object sender, e) { _timer.Change(Timeout.Infinite, Timeout.Infinite); } void NudInterval_ValueChanged(object sender, EventArgs e) { _timer.Change(0, (int)NudInterval.Value); } void StartCycleMove(object sender, EventArgs e) { StartCycleMoveProcess(); } #endregion #region 事件处理 void TimerProcess(object o) { try { if (InvokeRequired) Invoke(new Action<object>(TimerProcess), new[] { o }); else { _left--; foreach (var pictureBox in _pictureBoxs) pictureBox.Left--; if (_left < _changeCritical) { _left = 0; var pictureBox = _pictureBoxs[_leftPictureBoxIndex]; pictureBox.Left = Panel.Width + (int)NudImageMargin.Value; _leftPictureBoxIndex++; if (_leftPictureBoxIndex >= (int)NudImageCount.Value) _leftPictureBoxIndex = 0; } } } catch { } } #endregion #region 私有方法 private void CreateImages() { var imageCount = (int) NudImageCount.Value; var imageMargin = (int) NudImageMargin.Value; var containerWidth = Panel.Width; var eachImageWidth = (containerWidth - (imageCount - 2) * imageMargin) / (imageCount - 1); var eachImageHeight = Panel.Height; _changeCritical = -(eachImageWidth + imageMargin); _pictureBoxs = new PictureBox[imageCount]; for (var i = 0; i < imageCount; i++) { var bitmap = new Bitmap(eachImageWidth, eachImageHeight); var random = new Random(DateTime.Now.Millisecond); var g = Graphics.FromImage(bitmap); var backColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); g.Clear(backColor); g.DrawString("", new Font("SimHei", 10f), new SolidBrush(Color.FromArgb(255 - backColor.R, 255 - backColor.R, 255 - backColor.R)), 5, 5); g.DrawString(i.ToString("00"), new Font("SimHei", eachImageWidth / 4f, FontStyle.Bold), new SolidBrush(Color.FromArgb(255 - backColor.R, 255 - backColor.R, 255 - backColor.R)), 5, 15); var pictureBox = new PictureBox { Width = eachImageWidth, Height = eachImageHeight, Image = bitmap, Location = new Point(i * eachImageWidth + i * imageMargin, 0) }; _pictureBoxs[i] = pictureBox; Panel.Controls.Add(pictureBox); } } private void StartCycleMoveProcess() { ButStart.Enabled = false; Panel.Controls.Clear(); _left = 0; _leftPictureBoxIndex = 0; CreateImages(); _timer.Change(0, (int)NudInterval.Value); ButStop.Enabled = true; } #endregion