Csharp/C Sharp/GUI Windows Form/ImageList

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add to and get Image from Image List

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
public class Form1 : Form
{
    private System.Windows.Forms.Button cmdFillImageList;
    private System.Windows.Forms.Button cmdPaintImages;
    private System.Windows.Forms.ImageList iconImages;
  public Form1() {
        InitializeComponent();
  }
    private void cmdFillImageList_Click(object sender, EventArgs e)
    {
        iconImages.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
        iconImages.ImageSize = new System.Drawing.Size(16, 16);
        string[] iconFiles = Directory.GetFiles(Application.StartupPath, "*.ico");
        foreach (string iconFile in iconFiles)
        {
            Icon newIcon = new Icon(iconFile);
            iconImages.Images.Add(newIcon);
        }
    }
    private void cmdPaintImages_Click(object sender, EventArgs e)
    {
        Graphics g = this.CreateGraphics();
        for (int i = 0; i < iconImages.Images.Count; i++)
        {
            iconImages.Draw(g, 30 + i * 30, 30, i);
        }
        g.Dispose();
    }
    private void InitializeComponent()
    {
        this.cmdFillImageList = new System.Windows.Forms.Button();
        this.cmdPaintImages = new System.Windows.Forms.Button();
        this.iconImages = new System.Windows.Forms.ImageList(new System.ruponentModel.Container());
        this.SuspendLayout();
        // 
        // cmdFillImageList
        // 
        this.cmdFillImageList.Location = new System.Drawing.Point(29, 217);
        this.cmdFillImageList.Name = "cmdFillImageList";
        this.cmdFillImageList.Size = new System.Drawing.Size(118, 23);
        this.cmdFillImageList.TabIndex = 0;
        this.cmdFillImageList.Text = "Fill Image List";
        this.cmdFillImageList.UseVisualStyleBackColor = true;
        this.cmdFillImageList.Click += new System.EventHandler(this.cmdFillImageList_Click);
        // 
        // cmdPaintImages
        // 
        this.cmdPaintImages.Location = new System.Drawing.Point(153, 217);
        this.cmdPaintImages.Name = "cmdPaintImages";
        this.cmdPaintImages.Size = new System.Drawing.Size(112, 23);
        this.cmdPaintImages.TabIndex = 1;
        this.cmdPaintImages.Text = "Paint Images";
        this.cmdPaintImages.UseVisualStyleBackColor = true;
        this.cmdPaintImages.Click += new System.EventHandler(this.cmdPaintImages_Click);
        // 
        // iconImages
        // 
        this.iconImages.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
        this.iconImages.ImageSize = new System.Drawing.Size(16, 16);
        this.iconImages.TransparentColor = System.Drawing.Color.Transparent;
        // 
        // ImageListTest
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.cmdPaintImages);
        this.Controls.Add(this.cmdFillImageList);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "ImageListTest";
        this.Text = "ImageListTest";
        this.ResumeLayout(false);
    }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }
}


Image List Example

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
using System.IO;
namespace ImageListExample
{
  /// <summary>
  /// Summary description for ImageListExample.
  /// </summary>
  public class ImageListExample : System.Windows.Forms.Form
  {
    internal System.Windows.Forms.Button cmdDrawImageList;
    internal System.Windows.Forms.Button cmdFillImageList;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ruponentModel.Container components = null;
    public ImageListExample()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.cmdDrawImageList = new System.Windows.Forms.Button();
      this.cmdFillImageList = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // cmdDrawImageList
      // 
      this.cmdDrawImageList.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.cmdDrawImageList.Location = new System.Drawing.Point(164, 172);
      this.cmdDrawImageList.Name = "cmdDrawImageList";
      this.cmdDrawImageList.Size = new System.Drawing.Size(96, 24);
      this.cmdDrawImageList.TabIndex = 3;
      this.cmdDrawImageList.Text = "Draw Images";
      this.cmdDrawImageList.Click += new System.EventHandler(this.cmdDrawImageList_Click);
      // 
      // cmdFillImageList
      // 
      this.cmdFillImageList.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.cmdFillImageList.Location = new System.Drawing.Point(28, 172);
      this.cmdFillImageList.Name = "cmdFillImageList";
      this.cmdFillImageList.Size = new System.Drawing.Size(104, 24);
      this.cmdFillImageList.TabIndex = 2;
      this.cmdFillImageList.Text = "Fill ImageList";
      this.cmdFillImageList.Click += new System.EventHandler(this.cmdFillImageList_Click);
      // 
      // ImageListExample
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 214);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.cmdDrawImageList,
                                      this.cmdFillImageList});
      this.Name = "ImageListExample";
      this.Text = "ImageList Example";
      this.ResumeLayout(false);
    }
    #endregion
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new ImageListExample());
    }
    ImageList iconImages = new System.Windows.Forms.ImageList();
    private void cmdFillImageList_Click(object sender, System.EventArgs e)
    {
      // Configure the ImageList.
      iconImages.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      iconImages.ImageSize = new System.Drawing.Size(16, 16);
      
      // Get all the icon files in the current directory.
        string[] iconFiles = Directory.GetFiles(Application.StartupPath, "*.ico");
      
      // Create an Image object for each file and add it to the ImageList.
      // You can also use an Image subclass (like Icon).
      foreach (string iconFile in iconFiles)
      {
        Icon newIcon = new Icon(iconFile);
        iconImages.Images.Add(newIcon);
      }
         }
    private void cmdDrawImageList_Click(object sender, System.EventArgs e)
    {
      // Get the graphics device context for the form.
      Graphics g = this.CreateGraphics();
      
      // Draw each image using the ImageList.Draw() method.
      for (int i = 0; i < iconImages.Images.Count; i++)
      {
        iconImages.Draw(g, 30 + i * 30, 30, i);
      }
      
      // Release the graphics device context.
      g.Dispose();
    }
  }
}

<A href="http://www.nfex.ru/Code/CSharpDownload/ImageListExample.zip">ImageListExample.zip( 24 k)</a>