|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AutoDiagramer
{
#region frmSettings CLASS
/// <summary>
/// Partial class that creates the GUI part of the frmZoom object
/// </summary>
public partial class frmZoom : Form
{
#region Instance fields
//instanec fields
private Bitmap _Bitmap= null;
private double Zoom = 1.0;
#endregion
#region Constructor
/// <summary>
/// Creates a new frmZoom object
/// </summary>
public frmZoom()
{
InitializeComponent();
}
#endregion
#region Public Properties
/// <summary>
/// Sets the current Bitmap
/// </summary>
public Bitmap CurrentImage
{
set
{
//create a new BitMap with initial zoom of 1.0 (100%)
_Bitmap = value;
this.pnlImage.AutoScroll = true;
this.pnlImage.AutoScrollMinSize = new Size ((int)(_Bitmap.Width * Zoom),
(int)(_Bitmap.Height * Zoom));
this.Invalidate();
}
}
#endregion
#region Private Methods
/// <summary>
/// Zooms the current image to the required Zoom factor
/// </summary>
/// <param name="zFactor">The new zoom factor</param>
private void doZoom(double zFactor)
{
this.pnlImage.Visible = false;
Zoom = zFactor;
this.pnlImage.AutoScrollMinSize = new Size ((int)(_Bitmap.Width * Zoom),
(int)(_Bitmap.Height * Zoom));
this.Invalidate();
this.pnlImage.Visible = true;
pnlImage.Focus();
}
/// <summary>
/// mnuZoom25 menu clicked, do zoom 25%
/// </summary>
/// <param name="sender">mnuZoom25 menu</param>
/// <param name="e">the events args</param>
private void mnuZoom25_Click(object sender, EventArgs e)
{
doZoom(.25);
}
/// <summary>
/// mnuZoom50 menu clicked, do zoom 50%
/// </summary>
/// <param name="sender">mnuZoom50 menu</param>
/// <param name="e">the events args</param>
private void mnuZoom50_Click(object sender, EventArgs e)
{
doZoom(.5);
}
/// <summary>
/// mnuZoom100 menu clicked, do zoom 100%
/// </summary>
/// <param name="sender">mnuZoom100 menu</param>
/// <param name="e">the events args</param>
private void mnuZoom100_Click(object sender, EventArgs e)
{
doZoom(1.0);
}
/// <summary>
/// mnuZoom200 menu clicked, do zoom 200%
/// </summary>
/// <param name="sender">mnuZoom200 menu</param>
/// <param name="e">the events args</param>
private void mnuZoom200_Click(object sender, EventArgs e)
{
doZoom(2.0);
}
/// <summary>
/// paint the image on the pnlImage panel at the right zoom
/// </summary>
/// <param name="sender">pnlImage panel</param>
/// <param name="e">the events args</param>
private void pnlImage_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(_Bitmap, new Rectangle(this.pnlImage.AutoScrollPosition.X,
this.pnlImage.AutoScrollPosition.Y, (int)(_Bitmap.Width*Zoom), (int)(_Bitmap.Height * Zoom)));
}
/// <summary>
/// mnuEnterManualZoom menu clicked, so allow user to enter their own zoom,
/// but check for all possible error prior to allowing new zoom to be applied
/// </summary>
/// <param name="sender">mnuEnterManualZoom menu</param>
/// <param name="e">the events args</param>
private void mnuEnterManualZoom_Click(object sender, EventArgs e)
{
//allow user to enter their own zoom, but check for all possible error prior to allowing
//new zoom to be applied
String zoomEntered = Program.InputBox("Enter your desired zoom factor (Between 0.1 - 2.0)","Enter zoom factor","1.0");
if (!string.IsNullOrEmpty(zoomEntered))
{
try
{
double dZoom = double.Parse(zoomEntered);
if (dZoom < 0.1 || dZoom > 2.0)
{
Program.ErrorBox("You didnt enter a valid zoom factor\r\n\r\n" +
"Zoom MUST be between 0.1 - 2.0");
}
else
{
doZoom(dZoom);
}
}
catch (Exception)
{
Program.ErrorBox("You didnt enter a valid zoom factor");
}
}
}
/// <summary>
/// btnOk button clicked, so close this form
/// </summary>
/// <param name="sender">btnOk button</param>
/// <param name="e">the events args</param>
private void btnOk_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// frmZoom form resized, so re-position all controls
/// </summary>
/// <param name="sender">frmZoom form</param>
/// <param name="e">the events args</param>
private void frmZoom_Resize(object sender, EventArgs e)
{
this.pnlImage.Width = this.Width;
this.pnlImage.Height = this.Height - (this.pnlImage.Location.Y + 2* pnlBottom.Height);
btnOk.Left = this.Width - 84;
}
/// <summary>
/// frmZoom form shown, so set focus to main panel
/// </summary>
/// <param name="sender">frmZoom form</param>
/// <param name="e">the events args</param>
private void frmZoom_Shown(object sender, EventArgs e)
{
pnlImage.Focus();
}
#endregion
}
#endregion
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog