In Depth Banner
Skip Navigation Links

Select your preferred language

Listing DebugControl.cs

using System;

using System.IO;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Windows.Forms;

 

/*

 *

 *  DebugControl is a simple Windows Forms control that reads all the image files

 *  in a directory and provides a thumbnail preview of them.

 *

 *  As always, if this control is of commercial use to you, feel free to use it under

 *  the terms of your subscription.

 *

 *

 */

 

namespace WellFormed

{

  /// <summary>

  /// Summary description for DebugControl.

  /// </summary>

  public class DebugControl : System.Windows.Forms.UserControl

  {

 

    string _path;

    [

    Category("Data"),

    Description("The current directory path")

    ]

    public string Path

    {

      get{return _path;}

      set{

        _path=value;

        System.Diagnostics.Trace.WriteLine("New path = "+value);

        UpdateListBox();

      }

    }

 

    //used to store the icons used in the list box

    private Bitmap _folderImage;

    private Bitmap _imageImage;

 

    //Controls dragged to the user control surface

    private System.Windows.Forms.ListBox listBox1;

    private System.Windows.Forms.PictureBox pictureBox1;

    private System.ComponentModel.IContainer components;

 

    public DebugControl()

    {

      // This call is required by the Windows.Forms Form Designer.

      InitializeComponent();

 

      //Load the icons and make them transparent

      _folderImage=(Bitmap)Bitmap.FromStream(this.GetType().Assembly.GetManifestResourceStream("DebugControl.Folder.bmp"));

      _imageImage=(Bitmap)Bitmap.FromStream(this.GetType().Assembly.GetManifestResourceStream("DebugControl.Image.bmp"));

      _folderImage.MakeTransparent(Color.Magenta);

      _imageImage.MakeTransparent(Color.Magenta);

 

      //Set the current directory

      Path=Directory.GetCurrentDirectory();

    }

 

    /// <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 Component 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.listBox1 = new System.Windows.Forms.ListBox();

      this.pictureBox1 = new System.Windows.Forms.PictureBox();

      this.SuspendLayout();

      //

      // listBox1

      //

      this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;

      this.listBox1.Location = new System.Drawing.Point(8, 8);

      this.listBox1.Name = "listBox1";

      this.listBox1.Size = new System.Drawing.Size(120, 160);

      this.listBox1.TabIndex = 0;

      this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);

      this.listBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.listBox1_MeasureItem);

      this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);

      this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);

      //

      // pictureBox1

      //

      this.pictureBox1.Location = new System.Drawing.Point(136, 8);

      this.pictureBox1.Name = "pictureBox1";

      this.pictureBox1.Size = new System.Drawing.Size(120, 160);

      this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

      this.pictureBox1.TabIndex = 1;

      this.pictureBox1.TabStop = false;

      //

      // DebugControl

      //

      this.Controls.Add(this.pictureBox1);

      this.Controls.Add(this.listBox1);

      this.Name = "DebugControl";

      this.Size = new System.Drawing.Size(264, 176);

      this.Load += new System.EventHandler(this.DebugControl_Load);

      this.Layout += new System.Windows.Forms.LayoutEventHandler(this.DebugControl_Layout);

      this.ResumeLayout(false);

 

    }

    #endregion

 

    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)

    {

      //When the selection changes

      DirectoryItem di = (DirectoryItem)this.listBox1.SelectedItem;

      //Decide if the item is a directory or an image

      if(di!=null && di.ItemType==DirectoryItem.ItemTypes.Image)

      {

        //if it's an image

        if(this.pictureBox1.Image!=null)

        {

          //clear out the old preview

          this.pictureBox1.Image.Dispose();

          this.pictureBox1.Image=null; //free up the old thumbnail

        }

        //load up the image

        Bitmap temp=(Bitmap)Bitmap.FromFile(di.Path);

        //extract a thumbnail from it

        this.pictureBox1.Image=temp.GetThumbnailImage(this.pictureBox1.Width,this.pictureBox1.Height,null,IntPtr.Zero);

        //and tidy up

        temp.Dispose();

      }

    }

 

    protected void UpdateListBox()

    {

      //if there is no path, return

      if(Path==null | Path=="")

        return;

 

      //clean out the list box

      this.listBox1.Items.Clear();

 

      // in design mode we don't want it scanning the directories

      if(this.DesignMode)

      {

        DirectoryItem di=new DirectoryItem();

        di.Path=di.Name="Directory Placeholder";

        di.ItemType=DirectoryItem.ItemTypes.Folder;

        this.listBox1.Items.Add(di);

        return;

      }

 

      if(Path!=Directory.GetDirectoryRoot(Path))

      {

        DirectoryItem di=new DirectoryItem();

        //link to the parent directory

        di.Path=di.Name="..";

        di.ItemType=DirectoryItem.ItemTypes.Folder;

        this.listBox1.Items.Add(di);

      }

      //assemble a list of directories that can be selected first

      string[] directories = Directory.GetDirectories(_path,"*.*");

      foreach(string s in directories)

      {

        DirectoryItem i =new DirectoryItem();

        i.Path=s;

        i.Name=new DirectoryInfo(s).Name;

        i.ItemType=DirectoryItem.ItemTypes.Folder;

        this.listBox1.Items.Add(i);

      }

      //then a list of all the image files in the current directory

      string[] filetypes=new string[]{"*.bmp","*.jpg","*.gif","*.png"};

      ArrayList al=new ArrayList();

      foreach(string ext in filetypes)

      {

        string[] images=Directory.GetFiles(_path,ext);

        foreach(string s in images)

          al.Add(s);

      }

      //these are sorted alphabetically

      al.Sort();

      //and turned into image entries for the list box

      foreach(string s in al)

      {

        DirectoryItem i = new DirectoryItem();

        i.Path=s;

        i.Name=new DirectoryInfo(s).Name;

        i.ItemType=DirectoryItem.ItemTypes.Image;

        this.listBox1.Items.Add(i);

      }

    }

 

    private void listBox1_DoubleClick(object sender, System.EventArgs e)

    {

      if(this.listBox1.SelectedItem!=null)

      {

        DirectoryItem di=(DirectoryItem)this.listBox1.SelectedItem;

        //if the item is a directory or the path to the parent directory, select it

        if(di.ItemType==DirectoryItem.ItemTypes.Folder)

        {

          if(di.Path=="..")

            Path=Directory.GetParent(Path).FullName;

          else

            Path=di.Path;

        }

      }

    }

 

    private void listBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)

    {

      //measure the items. If its a valid one, use the string length and the icon size plus a bit of padding

      if(e.Index>-1)

      {

        e.ItemWidth=(int)(20+e.Graphics.MeasureString(((DirectoryItem)((ListBox)sender).Items[e.Index]).Name,Font,1024,StringFormat.GenericTypographic).Width);

        e.ItemHeight=(int)Math.Max(22,Font.Height+4);

      }

      else

      {

        e.ItemHeight=22;

        e.ItemWidth=this.listBox1.Width;

      }

    }

 

    private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)

    {

      if(e.Index==-1)

        return;

 

      //Draws each item

      System.Diagnostics.Trace.Write(string.Format("Drawing item {0}",this.listBox1.Items[e.Index].ToString()));

      e.DrawBackground();

      //Figure out the forecolour from the select state

      SolidBrush b=new SolidBrush(((e.State & DrawItemState.Selected)!=DrawItemState.Selected) ? this.ForeColor : Color.White);

      DirectoryItem di=(DirectoryItem)((ListBox)sender).Items[e.Index];

      //draw the correct icon

      if(di.ItemType==DirectoryItem.ItemTypes.Folder)

      {

        System.Diagnostics.Trace.WriteLine("-Directory");

        e.Graphics.DrawImage(_folderImage,new Rectangle(2,e.Bounds.Top+(e.Bounds.Height/2)-8,16,16),0,0,16,16,GraphicsUnit.Pixel);

      }

      else

      {

        System.Diagnostics.Trace.WriteLine("-Image file");

        e.Graphics.DrawImage(_imageImage,new Rectangle(2,e.Bounds.Top+(e.Bounds.Height/2)-8,16,16),0,0,16,16,GraphicsUnit.Pixel);

      }

      //and finally, the string

      e.Graphics.DrawString(di.Name,Font,b,22f,e.Bounds.Top+2,StringFormat.GenericTypographic);

 

      b.Dispose();

 

    }

 

    private void DebugControl_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)

    {

      //Reposition the listbox and picturebox if needed

      this.listBox1.Location=new Point(0,0);

      this.listBox1.Size=new Size(this.Width/2,this.Height);

      this.pictureBox1.Location=new Point(this.Width/2,0);

      this.pictureBox1.Size=new Size(this.Width/2,this.Height);

    }

 

    private void DebugControl_Load(object sender, System.EventArgs e)

    {

      if(this.DesignMode)

        this.pictureBox1.Image=Image.FromStream(this.GetType().Assembly.GetManifestResourceStream("DebugControl.PicturePlaceholder.bmp"));

    }

  }

 

 

  /// <summary>

  /// This simple class represents a single directory item entry and is stored in the listbox

  /// </summary>

  class DirectoryItem

  {

    public enum ItemTypes

    {

      Image,

      Folder

    }

 

    string _path;

    public string Path

    {

      get{return _path;}

      set{_path=value;}

    }

 

    string _name;

    public string Name

    {

      get{return _name;}

      set{_name=value;}

    }

 

 

    ItemTypes _itemType;

    public ItemTypes ItemType

    {

      get{return _itemType;}

      set{_itemType=value;}

    }

 

    public override string ToString()

    {

      return Name;

    }

 

  }

}

 

Hit your Back button to return to the article

Copyright © Bob Powell 2003-2009. All rights reserved