Csharp/C Sharp by API/System.Windows.Forms/ImageList

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

ImageList.ColorDepth

<source lang="csharp">

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());
 }

}


 </source>


ImageList.Images

<source lang="csharp"> 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.ListView browserListView;
     private System.Windows.Forms.Label currentLabel;
     private System.Windows.Forms.Label displayLabel;
     private System.Windows.Forms.ImageList fileFolder;
     
     string currentDirectory = Directory.GetCurrentDirectory();
 public Form1() {
       InitializeComponent();
       Image folderImage = Image.FromFile("winter.jpg" );
       Image fileImage = Image.FromFile("winter.jpg" );
       fileFolder.Images.Add( folderImage );
       fileFolder.Images.Add( fileImage );
       LoadFilesInDirectory( currentDirectory );
       displayLabel.Text = currentDirectory;
 }
   private void browserListView_Click( object sender, EventArgs e )
   {
     if ( browserListView.SelectedItems.Count != 0 )
     {
        if ( browserListView.Items[0].Selected )
        {
           DirectoryInfo directoryObject = new DirectoryInfo( currentDirectory );
           if ( directoryObject.Parent != null )
              LoadFilesInDirectory( directoryObject.Parent.FullName );
        }else {
           string chosen = browserListView.SelectedItems[ 0 ].Text;
           if ( Directory.Exists( currentDirectory + @"\" + chosen ) )
           {
              if ( currentDirectory == @"C:\" )
                 LoadFilesInDirectory( currentDirectory + chosen );
              else
                 LoadFilesInDirectory(currentDirectory + @"\" + chosen );
           }
        }
        displayLabel.Text = currentDirectory;
     } 
  } 
  public void LoadFilesInDirectory( string currentDirectoryValue )
  {
     try
     {
        browserListView.Items.Clear();
        browserListView.Items.Add( "Go Up One Level" );
        currentDirectory = currentDirectoryValue;
        DirectoryInfo newCurrentDirectory = new DirectoryInfo( currentDirectory );
        DirectoryInfo[] directoryArray = newCurrentDirectory.GetDirectories();
        FileInfo[] fileArray = newCurrentDirectory.GetFiles();
        foreach ( DirectoryInfo dir in directoryArray )
        {
           ListViewItem newDirectoryItem = browserListView.Items.Add( dir.Name );
           newDirectoryItem.ImageIndex = 0;
        }
        foreach ( FileInfo file in fileArray )
        {
           ListViewItem newFileItem = browserListView.Items.Add( file.Name );
           newFileItem.ImageIndex = 1;
        }
     } catch ( UnauthorizedAccessException ) {
        Console.WriteLine( "Unauthorized Access Exception");
     }
  }
     private void InitializeComponent()
     {
        this.browserListView = new System.Windows.Forms.ListView();
        this.fileFolder = new System.Windows.Forms.ImageList(new System.ruponentModel.Container());
        this.currentLabel = new System.Windows.Forms.Label();
        this.displayLabel = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // browserListView
        // 
        this.browserListView.Location = new System.Drawing.Point(12, 60);
        this.browserListView.Name = "browserListView";
        this.browserListView.Size = new System.Drawing.Size(456, 197);
        this.browserListView.SmallImageList = this.fileFolder;
        this.browserListView.TabIndex = 0;
        this.browserListView.View = System.Windows.Forms.View.List;
        this.browserListView.Click += new System.EventHandler(this.browserListView_Click);
        // 
        // fileFolder
        // 
        this.fileFolder.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
        this.fileFolder.ImageSize = new System.Drawing.Size(16, 16);
        this.fileFolder.TransparentColor = System.Drawing.Color.Transparent;
        // 
        // currentLabel
        // 
        this.currentLabel.AutoSize = true;
        this.currentLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.currentLabel.Location = new System.Drawing.Point(10, 19);
        this.currentLabel.Name = "currentLabel";
        this.currentLabel.Size = new System.Drawing.Size(122, 20);
        this.currentLabel.TabIndex = 1;
        this.currentLabel.Text = "Now in Directory:";
        // 
        // displayLabel
        // 
        this.displayLabel.AutoSize = true;
        this.displayLabel.Location = new System.Drawing.Point(138, 19);
        this.displayLabel.Name = "displayLabel";
        this.displayLabel.Size = new System.Drawing.Size(0, 0);
        this.displayLabel.TabIndex = 2;
        // 
        // ListViewTestForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(480, 270);
        this.Controls.Add(this.displayLabel);
        this.Controls.Add(this.currentLabel);
        this.Controls.Add(this.browserListView);
        this.Name = "ListViewTestForm";
        this.Text = "ListViewTest";
        this.ResumeLayout(false);
        this.PerformLayout();
     }
 [STAThread]
 static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

}


 </source>


ImageList.Images.AddStrip

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;

class SimpleToolBar: Form {

    public static void Main()
    {
         Application.Run(new SimpleToolBar());
    }
    public SimpleToolBar()
    {
         Menu = new MainMenu();
         Menu.MenuItems.Add("File");
         Menu.MenuItems.Add("Edit");
         Menu.MenuItems.Add("View");
         Menu.MenuItems.Add("Help");
  
         Bitmap bm = new Bitmap(GetType(),"SimpleToolBar.bmp");
  
         ImageList imglst = new ImageList();
         imglst.Images.AddStrip(bm);
         imglst.TransparentColor = Color.Cyan;
  
         ToolBar tbar = new ToolBar();
         tbar.Parent = this;
         tbar.ImageList = imglst;
         tbar.ShowToolTips = true;
  
         string[] astr = {"New", "Open", "Save", "Print", "Cut", "Copy", "Paste" };
  
         for (int i = 0; i < 7; i++)
         {
              ToolBarButton tbarbtn = new ToolBarButton();
              tbarbtn.ImageIndex = i;
              tbarbtn.ToolTipText = astr[i];
  
              tbar.Buttons.Add(tbarbtn);
         }
    }

}


 </source>


ImageList.ImageSize

<source lang="csharp">

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());
 }

}


 </source>


ImageList.TransparentColor

<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class ImageListCreateAndAddToToolBar : System.Windows.Forms.Form {

 private ImageList toolBarIcons;
 private ToolBarButton tbExitButton;
 private ToolBarButton tbSaveButton;
 private ToolBar toolBar;
 private System.ruponentModel.Container components = null;
 public ImageListCreateAndAddToToolBar()
 {
   this.ruponents = new System.ruponentModel.Container();
   this.Size = new System.Drawing.Size(300,300);
   toolBar = new ToolBar();
   toolBarIcons = new ImageList();
   tbSaveButton = new ToolBarButton();
   tbExitButton = new ToolBarButton();
   tbSaveButton.ImageIndex = 0;
   tbExitButton.ImageIndex = 1;
   toolBar.ImageList = toolBarIcons;
   toolBar.Size = new System.Drawing.Size(272, 28);
   toolBar.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   toolBar.ShowToolTips = true;
   toolBar.Buttons.AddRange(new ToolBarButton[] {tbSaveButton, tbExitButton});
   toolBar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBar_Clicked);
   
   toolBarIcons.ImageSize = new System.Drawing.Size(32, 32);
   toolBarIcons.Images.Add(new Icon("filesave.ico"));
   toolBarIcons.Images.Add(new Icon("fileexit.ico"));
   toolBarIcons.ColorDepth = ColorDepth.Depth16Bit;
   toolBarIcons.TransparentColor = System.Drawing.Color.Transparent;
   this.Controls.Add(toolBar);    
 }
 [STAThread]
 static void Main() 
 {
   Application.Run(new ImageListCreateAndAddToToolBar());
 }
 
 private void ToolBar_Clicked(object sender, ToolBarButtonClickEventArgs e) 
 {
   MessageBox.Show(e.Button.ToolTipText);
 }

}


 </source>