// Variable to toggle if user is currently selecting an area.
private bool m_bZoomToRectSelect = false;
// Set of points defining the selected area.
private ImGearPoint[] igPointsZoomToRect;
// Color of selection box.
private Color m_SelectorColor = Color.White;
// Method to return appropriate height for a specific width
// based upon the size of a scrollable view.
private int GetHeight(IImGearScrollableView igScrollableView, int Width)
{
return Width * igScrollableView.ClientRectangle.Height / igScrollableView.ClientRectangle.Width;
}
private void menuZoomToRect_Click(object sender, EventArgs e)
{
// Menu item used to toggle if Zoom to Rectangle is on or not.
menuZoomToRect.Checked = !menuZoomToRect.Checked;
}
private void imGearPageView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (menuZoomToRect.Checked)
{
// Enter Zoom to Rectangle selection mode.
m_bZoomToRectSelect = true;
// Create a new pair of points to define the rectangle,
// setting the first point to the current position.
igPointsZoomToRect = new ImGearPoint[2];
igPointsZoomToRect[0].X = e.X;
igPointsZoomToRect[0].Y = e.Y;
// Register method to draw the selection rectangle.
imGearPageView.RegisterAfterDraw(
new ImGearPageView.AfterDraw(DrawSelector));
}
}
private void imGearPageView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (m_bZoomToRectSelect)
{
// Leave Zoom to Rectangle selection mode.
imGearPageView.RegisterAfterDraw(null);
m_bZoomToRectSelect = false;
// Record the second point of the zoom rectangle,
// normalizing it to the view's aspect ratio.
igPointsZoomToRect[1].X = e.X;
igPointsZoomToRect[1].Y = igPointsZoomToRect[0].Y +
GetHeight(imGearPageView, Math.Abs(igPointsZoomToRect[1].X - igPointsZoomToRect[0].X) + 1) - 1;
// Create a rectangle based upon the 2 points.
ImGearRectangle igRectangle = new ImGearRectangle(igPointsZoomToRect[0], igPointsZoomToRect[1]);
// Cancel the zoom if it would be for a 0x0 or 1x1 rectangle.
if (igRectangle.Width <= 1 || igRectangle.Height <= 1)
return;
// Zoom to the selected rectangle
imGearPageView.Display.ZoomToRectangle(imGearPageView, igRectangle);
imGearPageView.Invalidate();
// Disable Zoom to Rectangle.
menuZoomToRect.Checked = false;
}
}
private void imGearPageView1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (m_bZoomToRectSelect)
{
// Record the second point of the zoom rectangle,
// normalizing it to the view's aspect ratio.
igPointsZoomToRect[1].X = e.X;
igPointsZoomToRect[1].Y = igPointsZoomToRect[0].Y +
GetHeight(imGearPageView, Math.Abs(igPointsZoomToRect[1].X - igPointsZoomToRect[0].X) + 1) - 1;
// Cause a redraw which will update the zoom selection rectangle.
imGearPageView.Invalidate();
}
}
private void DrawSelector(System.Drawing.Graphics gr)
{
if (m_bZoomToRectSelect)
{
// Create a new pen to draw dotted lines.
Pen pen = new Pen(m_SelectorColor);
pen.DashStyle = DashStyle.Dot;
// Define the currently selected zoom rectangle.
ImGearRectangle igRectangleZoom = new ImGearRectangle(igPointsZoomToRect[0], igPointsZoomToRect[1]);
// Draw the selection box.
gr.DrawRectangle(pen, igRectangleZoom.Left, igRectangleZoom.Top,
igRectangleZoom.Width, igRectangleZoom.Height);
}
}