using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace AnimationTime { public partial class MainForm : Form { private const int MaxImagesPerRow = 4; private const int ImageFrameLeft = 12; private const int ImageFrameTop = 38; private const int FormHorizontalPadding = ImageFrameLeft + 12 + 10; // Internal padding, plus external private const int FormVerticalPadding = ImageFrameTop + 12 + 40; // Internal padding, plus external AnimatedPictureFrame CurrentFrame; public MainForm() { InitializeComponent(); txtCurrentDir.Text = Application.StartupPath; // Create the frame CurrentFrame = new AnimatedPictureFrame(); CurrentFrame.Top = ImageFrameTop; CurrentFrame.Left = ImageFrameLeft; this.Controls.Add(CurrentFrame); } private void btnBrowse_Click(object sender, EventArgs e) { string SelectedFolder = null; using (FolderBrowserDialog BrowseDiag = new FolderBrowserDialog()) { // Set the proper start directory if (Directory.Exists(txtCurrentDir.Text)) BrowseDiag.SelectedPath = txtCurrentDir.Text; else BrowseDiag.SelectedPath = Application.StartupPath; BrowseDiag.Description = "Select the folder containing your .png sprite files"; BrowseDiag.ShowNewFolderButton = false; if (BrowseDiag.ShowDialog() == System.Windows.Forms.DialogResult.OK && Directory.Exists(BrowseDiag.SelectedPath)) SelectedFolder = BrowseDiag.SelectedPath; } if (SelectedFolder != null) { txtCurrentDir.Text = SelectedFolder; LoadImages(SelectedFolder); } } private void btnReload_Click(object sender, EventArgs e) { if (Directory.Exists(txtCurrentDir.Text)) LoadImages(txtCurrentDir.Text); } private void LoadImages(string ImageDirectory) { // Clear out and dispose old resources CurrentFrame.Clear(); // Load new resources List Images = new List(); foreach (string file in Directory.GetFiles(ImageDirectory)) if (file.ToLower().EndsWith(".png")) Images.Add(new AnimatedPicture(file)); if (Images.Count > 0) { // Assume all images are the same size, and find the required cell size to render int Rows = Images.Count / MaxImagesPerRow; if (Images.Count % MaxImagesPerRow > 0) Rows++; int CellSize = Images[0].CellSize; // Add the images to the panel at their proper position int CurrentX = 0; int CurrentY = 0; foreach (AnimatedPicture image in Images) { image.Left = CurrentX * CellSize; image.Top = CurrentY * CellSize; CurrentX++; if (CurrentX >= MaxImagesPerRow) { // Move to the next row CurrentX = 0; CurrentY++; } } // Set the frame height and width to fit the image CurrentFrame.Width = (CellSize * MaxImagesPerRow); CurrentFrame.Height = (CellSize * Rows); // Adjust the form height and width to fit the images this.Width = CurrentFrame.Width + FormHorizontalPadding; this.Height = CurrentFrame.Height + FormVerticalPadding; // Set the new images CurrentFrame.Set(Images); } } } }