Archive Browser
Download imageboxsample-part3.zip, last updated 23/08/2010 (443.55 KB)
Download- md5: 18e195e5dce9f1b6b33738aadcfa7c12
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"), ToolboxBitmap(typeof(ImageBox), "Cyotek.Windows.Forms.ImageBox.bmp")]
public partial class ImageBox : ScrollableControl
{
#region Private Member Declarations
private bool _autoPan;
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 _invertMouse;
private bool _isPanning;
private bool _showGrid;
private Point _startMousePosition;
private Point _startScrollPosition;
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.UpdateStyles();
this.BackColor = Color.White;
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;
this.AutoPan = true;
}
#endregion Public Constructors
#region Events
public event EventHandler AutoPanChanged;
public event EventHandler BorderStyleChanged;
public event EventHandler GridCellSizeChanged;
public event EventHandler GridColorAlternateChanged;
public event EventHandler GridColorChanged;
public event EventHandler GridScaleChanged;
public event EventHandler ImageChanged;
public event EventHandler InvertMouseChanged;
public event EventHandler PanEnd;
public event EventHandler PanStart;
public event EventHandler 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 bool IsInputKey(Keys keyData)
{
bool result;
if ((keyData & Keys.Right) == Keys.Right | (keyData & Keys.Left) == Keys.Left | (keyData & Keys.Up) == Keys.Up | (keyData & Keys.Down) == Keys.Down)
result = true;
else
result = base.IsInputKey(keyData);
return result;
}
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 OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
switch (e.KeyCode)
{
case Keys.Left:
this.AdjustScroll(-(e.Modifiers == Keys.None ? this.HorizontalScroll.SmallChange : this.HorizontalScroll.LargeChange), 0);
break;
case Keys.Right:
this.AdjustScroll(e.Modifiers == Keys.None ? this.HorizontalScroll.SmallChange : this.HorizontalScroll.LargeChange, 0);
break;
case Keys.Up:
this.AdjustScroll(0, -(e.Modifiers == Keys.None ? this.VerticalScroll.SmallChange : this.VerticalScroll.LargeChange));
break;
case Keys.Down:
this.AdjustScroll(0, e.Modifiers == Keys.None ? this.VerticalScroll.SmallChange : this.VerticalScroll.LargeChange);
break;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left && this.AutoPan && this.Image != null)
{
if (!this.IsPanning)
{
_startMousePosition = e.Location;
this.IsPanning = true;
}
if (this.IsPanning)
{
int x;
int y;
Point position;
if (!this.InvertMouse)
{
x = -_startScrollPosition.X + (_startMousePosition.X - e.Location.X);
y = -_startScrollPosition.Y + (_startMousePosition.Y - e.Location.Y);
}
else
{
x = -(_startScrollPosition.X + (_startMousePosition.X - e.Location.X));
y = -(_startScrollPosition.Y + (_startMousePosition.Y - e.Location.Y));
}
position = new Point(x, y);
this.UpdateScrollPosition(position);
}
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (this.IsPanning)
this.IsPanning = 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
[DefaultValue(true), Category("Behavior")]
public bool AutoPan
{
get { return _autoPan; }
set
{
if (_autoPan != value)
{
_autoPan = value;
this.OnAutoPanChanged(EventArgs.Empty);
}
}
}
[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(false), Category("Behavior")]
public bool InvertMouse
{
get { return _invertMouse; }
set
{
if (_invertMouse != value)
{
_invertMouse = value;
this.OnInvertMouseChanged(EventArgs.Empty);
}
}
}
[DefaultValue(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
public bool IsPanning
{
get { return _isPanning; }
protected set
{
if (_isPanning != value)
{
_isPanning = value;
_startScrollPosition = this.AutoScrollPosition;
if (value)
{
this.Cursor = Cursors.SizeAll;
this.OnPanStart(EventArgs.Empty);
}
else
{
this.Cursor = Cursors.Default;
this.OnPanEnd(EventArgs.Empty);
}
}
}
}
[DefaultValue(true), Category("Appearance")]
public bool ShowGrid
{
get { return _showGrid; }
set
{
if (_showGrid != value)
{
_showGrid = value;
this.OnShowGridChanged(EventArgs.Empty);
}
}
}
#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.AdjustViewPort();
}
protected virtual void AdjustScroll(int x, int y)
{
Point scrollPosition;
scrollPosition = new Point(this.HorizontalScroll.Value + x, this.VerticalScroll.Value + y);
this.UpdateScrollPosition(scrollPosition);
}
protected virtual void AdjustSize()
{
if (this.AutoSize && this.Dock == DockStyle.None)
base.Size = base.PreferredSize;
}
protected virtual void AdjustViewPort()
{
if (this.AutoScroll && this.Image != null)
this.AutoScrollMinSize = this.Image.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 OnAutoPanChanged(EventArgs e)
{
if (this.AutoPanChanged != null)
this.AutoPanChanged(this, e);
}
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 OnInvertMouseChanged(EventArgs e)
{
if (this.InvertMouseChanged != null)
this.InvertMouseChanged(this, e);
}
protected virtual void OnPanEnd(EventArgs e)
{
if (this.PanEnd != null)
this.PanEnd(this, e);
}
protected virtual void OnPanStart(EventArgs e)
{
if (this.PanStart != null)
this.PanStart(this, e);
}
protected virtual void OnShowGridChanged(EventArgs e)
{
this.InitializeGridTile();
if (this.ShowGridChanged != null)
this.ShowGridChanged(this, e);
}
protected virtual void UpdateScrollPosition(Point position)
{
this.AutoScrollPosition = position;
this.Invalidate();
this.OnScroll(new ScrollEventArgs(ScrollEventType.ThumbPosition, 0));
}
#endregion Protected Methods
}
}
Donate
This software may be used free of charge, but as with all free software there are costs involved to develop and maintain.
If this site or its services have saved you time, please consider a donation to help with running costs and timely updates.
Donate