Download imageboxsample-part2.zip, last updated 13/08/2010 (441.84 KB)

Download
  • md5: 93acdba6edd5d87cc16504cdf86a5416
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Cyotek.Windows.Forms
{

  // Cyotek ImageBox
  // Copyright (c) 2010 Cyotek. All Rights Reserved.
  // http://cyotek.com

  [DefaultProperty("Image")]
  public partial class ImageBox : ScrollableControl
  {
  #region  Private Member Declarations  

    private BorderStyle _borderStyle;
    private int _gridCellSize;
    private Color _gridColor;
    private Color _gridColorAlternate;
    private ImageBoxGridScale _gridScale;
    private Bitmap _gridTile;
    private System.Drawing.Image _image;
    private bool _showGrid;
    private TextureBrush _texture;

  #endregion  Private Member Declarations  

  #region  Public Constructors  

    public ImageBox()
    {
      InitializeComponent();

      this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
      this.SetStyle(ControlStyles.Selectable, false);
      this.UpdateStyles();

      this.BackColor = Color.White;
      this.TabStop = false;
      this.AutoSize = true;
      this.GridScale = ImageBoxGridScale.Small;
      this.ShowGrid = true;
      this.GridColor = Color.Gainsboro;
      this.GridColorAlternate = Color.White;
      this.GridCellSize = 8;
      this.BorderStyle = BorderStyle.FixedSingle;
    }

  #endregion  Public Constructors  

  #region  Events  

    public event EventHandler BorderStyleChanged;

    public event EventHandler GridCellSizeChanged;

    public event EventHandler<EventArgs> GridColorAlternateChanged;

    public event EventHandler GridColorChanged;

    public event EventHandler GridScaleChanged;

    public event EventHandler<EventArgs> ImageChanged;

    public event EventHandler<EventArgs> ShowGridChanged;

  #endregion  Events  

  #region  Overriden Properties  

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), DefaultValue(true)]
    public override bool AutoSize
    {
      get { return base.AutoSize; }
      set
      {
        if (base.AutoSize != value)
        {
          base.AutoSize = value;
          this.AdjustLayout();
        }
      }
    }

    [DefaultValue(typeof(Color), "White")]
    public override Color BackColor
    {
      get { return base.BackColor; }
      set { base.BackColor = value; }
    }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override Image BackgroundImage
    {
      get { return base.BackgroundImage; }
      set { base.BackgroundImage = value; }
    }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override ImageLayout BackgroundImageLayout
    {
      get { return base.BackgroundImageLayout; }
      set { base.BackgroundImageLayout = value; }
    }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override Font Font
    {
      get { return base.Font; }
      set { base.Font = value; }
    }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override string Text
    {
      get { return base.Text; }
      set { base.Text = value; }
    }

  #endregion  Overriden Properties  

  #region  Public Overridden Methods  

    public override Size GetPreferredSize(Size proposedSize)
    {
      Size size;

      if (this.Image != null)
      {
        int width;
        int height;

        // get the size of the image
        width = this.Image.Size.Width;
        height = this.Image.Size.Height;

        // add an offset based on padding
        width += this.Padding.Horizontal;
        height += this.Padding.Vertical;

        // add an offset based on the border style
        width += this.GetBorderOffset();
        height += this.GetBorderOffset();

        size = new Size(width, height);
      }
      else
        size = base.GetPreferredSize(proposedSize);

      return size;
    }

  #endregion  Public Overridden Methods  

  #region  Protected Overridden Methods  

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
        if (components != null)
          components.Dispose();

        if (_texture != null)
        {
          _texture.Dispose();
          _texture = null;
        }

        if (_gridTile != null)
        {
          _gridTile.Dispose();
          _gridTile = null;
        }
      }
      base.Dispose(disposing);
    }

    protected override void OnBackColorChanged(EventArgs e)
    {
      base.OnBackColorChanged(e);

      this.Invalidate();
    }

    protected override void OnDockChanged(EventArgs e)
    {
      base.OnDockChanged(e);

      if (this.Dock != DockStyle.None)
        this.AutoSize = false;
    }

    protected override void OnPaddingChanged(System.EventArgs e)
    {
      base.OnPaddingChanged(e);
      this.AdjustLayout();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      int borderOffset;
      Rectangle innerRectangle;

      borderOffset = this.GetBorderOffset();

      if (borderOffset != 0)
      {
        // draw the borders
        switch (this.BorderStyle)
        {
          case BorderStyle.FixedSingle:
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, this.ForeColor, ButtonBorderStyle.Solid);
            break;
          case BorderStyle.Fixed3D:
            ControlPaint.DrawBorder3D(e.Graphics, this.ClientRectangle, Border3DStyle.Sunken);
            break;
        }

        // clip the background so we don't overwrite the border
        innerRectangle = Rectangle.Inflate(this.ClientRectangle, -borderOffset, -borderOffset);
        e.Graphics.SetClip(innerRectangle);
      }
      else
        innerRectangle = this.ClientRectangle;


      // draw the background
      if (_texture != null && this.ShowGrid)
        e.Graphics.FillRectangle(_texture, innerRectangle);
      else
      {
        using (SolidBrush brush = new SolidBrush(this.BackColor))
          e.Graphics.FillRectangle(brush, innerRectangle);
      }

      // draw the image
      if (this.Image != null)
      {
        int left;
        int top;

        left = this.Padding.Left + borderOffset;
        top = this.Padding.Top + borderOffset;

        if (this.AutoScroll)
        {
          left += this.AutoScrollPosition.X;
          top += this.AutoScrollPosition.Y;
        }

        e.Graphics.DrawImageUnscaled(this.Image, new Point(left, top));
      }

      // reset the clipping
      if (borderOffset != 0)
        e.Graphics.ResetClip();
    }

    protected override void OnParentChanged(System.EventArgs e)
    {
      base.OnParentChanged(e);
      this.AdjustLayout();
    }

    protected override void OnResize(EventArgs e)
    {
      this.AdjustLayout();

      base.OnResize(e);
    }

    protected override void OnScroll(ScrollEventArgs se)
    {
      this.Invalidate();

      base.OnScroll(se);
    }

  #endregion  Protected Overridden Methods  

  #region  Public Properties  

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new Size AutoScrollMinSize
    {
      get { return base.AutoScrollMinSize; }
      set { base.AutoScrollMinSize = value; }
    }

    [Category("Appearance"), DefaultValue(typeof(BorderStyle), "FixedSingle")]
    public BorderStyle BorderStyle
    {
      get { return _borderStyle; }
      set
      {
        if (_borderStyle != value)
        {
          _borderStyle = value;
          this.OnBorderStyleChanged(EventArgs.Empty);
        }
      }
    }

    [Category("Appearance"), DefaultValue(8)]
    public int GridCellSize
    {
      get { return _gridCellSize; }
      set
      {
        if (_gridCellSize != value)
        {
          _gridCellSize = value;
          this.OnGridCellSizeChanged(EventArgs.Empty);
        }
      }
    }

    [Category("Appearance"), DefaultValue(typeof(Color), "Gainsboro")]
    public Color GridColor
    {
      get { return _gridColor; }
      set
      {
        if (_gridColor != value)
        {
          _gridColor = value;
          this.OnGridColorChanged(EventArgs.Empty);
        }
      }
    }

    [Category("Appearance"), DefaultValue(typeof(Color), "White")]
    public Color GridColorAlternate
    {
      get { return _gridColorAlternate; }
      set
      {
        if (_gridColorAlternate != value)
        {
          _gridColorAlternate = value;
          this.OnGridColorAlternateChanged(EventArgs.Empty);
        }
      }
    }

    [DefaultValue(typeof(ImageBoxGridScale), "Small"), Category("Appearance")]
    public ImageBoxGridScale GridScale
    {
      get { return _gridScale; }
      set
      {
        if (_gridScale != value)
        {
          _gridScale = value;
          this.OnGridScaleChanged(EventArgs.Empty);
        }
      }
    }

    [Category("Appearance"), DefaultValue(null)]
    public System.Drawing.Image Image
    {
      get { return _image; }
      set
      {
        if (_image != value)
        {
          _image = value;
          this.OnImageChanged(EventArgs.Empty);
        }
      }
    }

    [DefaultValue(true), Category("Appearance")]
    public bool ShowGrid
    {
      get { return _showGrid; }
      set
      {
        if (_showGrid != value)
        {
          _showGrid = value;
          this.OnShowGridChanged(EventArgs.Empty);
        }
      }
    }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new bool TabStop
    {
      get { return base.TabStop; }
      set { base.TabStop = value; }
    }

  #endregion  Public Properties  

  #region  Private Methods  

    private int GetBorderOffset()
    {
      int offset;

      switch (this.BorderStyle)
      {
        case BorderStyle.Fixed3D:
          offset = 2;
          break;
        case BorderStyle.FixedSingle:
          offset = 1;
          break;
        default:
          offset = 0;
          break;
      }

      return offset;
    }

    private void InitializeGridTile()
    {
      if (_texture != null)
        _texture.Dispose();

      if (_gridTile != null)
        _gridTile.Dispose();

      if (this.ShowGrid && this.GridCellSize != 0)
      {
        _gridTile = this.CreateGridTileImage(this.GridCellSize, this.GridColor, this.GridColorAlternate);
        _texture = new TextureBrush(_gridTile);
      }

      this.Invalidate();
    }

  #endregion  Private Methods  

  #region  Protected Methods  

    protected virtual void AdjustLayout()
    {
      if (this.AutoSize)
        this.AdjustSize();
      else if (this.AutoScroll)
        this.AdjustScrolling();
    }

    protected virtual void AdjustScrolling()
    {
      if (this.AutoScroll && this.Image != null)
        this.AutoScrollMinSize = this.Image.Size;
    }

    protected virtual void AdjustSize()
    {
      if (this.AutoSize)
      {
        Size size = base.PreferredSize;
        base.Size = size;
      }
    }

    protected virtual Bitmap CreateGridTileImage(int cellSize, Color firstColor, Color secondColor)
    {
      Bitmap result;
      int width;
      int height;
      float scale;

      // rescale the cell size
      switch (this.GridScale)
      {
        case ImageBoxGridScale.Medium:
          scale = 1.5F;
          break;
        case ImageBoxGridScale.Large:
          scale = 2;
          break;
        default:
          scale = 1;
          break;
      }

      cellSize = (int)(cellSize * scale);

      // draw the tile
      width = cellSize * 2;
      height = cellSize * 2;
      result = new Bitmap(width, height);
      using (Graphics g = Graphics.FromImage(result))
      {
        using (SolidBrush brush = new SolidBrush(firstColor))
          g.FillRectangle(brush, new Rectangle(0, 0, width, height));

        using (SolidBrush brush = new SolidBrush(secondColor))
        {
          g.FillRectangle(brush, new Rectangle(0, 0, cellSize, cellSize));
          g.FillRectangle(brush, new Rectangle(cellSize, cellSize, cellSize, cellSize));
        }
      }

      return result;
    }

    protected virtual void OnBorderStyleChanged(EventArgs e)
    {
      this.AdjustLayout();
      this.Invalidate();

      if (this.BorderStyleChanged != null)
        this.BorderStyleChanged(this, e);
    }

    protected virtual void OnGridCellSizeChanged(EventArgs e)
    {
      this.InitializeGridTile();

      if (this.GridCellSizeChanged != null)
        this.GridCellSizeChanged(this, e);
    }

    protected virtual void OnGridColorAlternateChanged(EventArgs e)
    {
      this.InitializeGridTile();

      if (this.GridColorAlternateChanged != null)
        this.GridColorAlternateChanged(this, e);
    }

    protected virtual void OnGridColorChanged(EventArgs e)
    {
      this.InitializeGridTile();

      if (this.GridColorChanged != null)
        this.GridColorChanged(this, e);
    }

    protected virtual void OnGridScaleChanged(EventArgs e)
    {
      this.InitializeGridTile();

      if (this.GridScaleChanged != null)
        this.GridScaleChanged(this, e);
    }

    protected virtual void OnImageChanged(EventArgs e)
    {
      this.AdjustLayout();
      this.Invalidate();

      if (this.ImageChanged != null)
        this.ImageChanged(this, e);
    }

    protected virtual void OnShowGridChanged(EventArgs e)
    {
      this.InitializeGridTile();

      if (this.ShowGridChanged != null)
        this.ShowGridChanged(this, e);
    }

  #endregion  Protected Methods  
  }
}

Donate

Donate