|
// Resort Companion Mobile Edition
// Copyright (c) 2004 Wolf Logan
using System;
using System.Drawing;
using System.Windows.Forms;
namespace org.CircleCross.ResortCompanion.Mobile {
/// <summary>
/// A scrolling map control. Derived from a PictureBox, this control adds scrolling capabilities,
/// both interactive and programmatic.
/// </summary>
public class ScrollMap: PictureBox {
protected Point origin = new Point(0, 0);
public Point Origin {
get {
return this.origin;
}
set {
// clip the specified Origin to prevent scrolling off the edge of the image
if(value.X > (this.Image.Width - this.Width)) {
this.origin.X = (this.Image.Width - this.Width);
} else if(value.X < 0) {
this.origin.X = 0;
} else {
this.origin.X = value.X;
}
if(value.Y > (this.Image.Height - this.Height)) {
this.origin.Y = (this.Image.Height - this.Height);
} else if(value.Y < 0) {
this.origin.Y = 0;
} else {
this.origin.Y = value.Y;
}
}
}
public ScrollMap(): base() {
}
/// <summary>
/// OnPaint handler. Paints the visible portion of the image, based on the current origin.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e) {
Rectangle src = new Rectangle(this.origin.X, this.origin.Y, this.ClientRectangle.Width, this.ClientRectangle.Height);
e.Graphics.DrawImage(this.Image, this.ClientRectangle, src, GraphicsUnit.Pixel);
}
/// <summary>
/// the initial point selected when the scrolling begins
/// </summary>
protected Point initialMouse = new Point(0, 0);
/// <summary>
/// the original origin before scrolling begins
/// </summary>
protected Point oldOrigin = new Point(0, 0);
/// <summary>
/// flag indicates whether we're currently scrolling
/// </summary>
protected bool isScrolling = false;
/// <summary>
/// OnMouseDown handler. Store the current Origin, and the current mouse position.
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDown(MouseEventArgs e) {
this.initialMouse.X = e.X;
this.initialMouse.Y = e.Y;
this.oldOrigin = this.Origin;
this.isScrolling = true;
base.OnMouseDown (e);
}
/// <summary>
/// OnMouseMove handler. Subtract the current mouse position from the initial position,
/// then move the Origin the same distance from the old Origin.
/// </summary>
/// <param name="e"></param>
protected override void OnMouseMove(MouseEventArgs e) {
if(this.isScrolling){
Point delta = this.initialMouse;
delta.Offset(-e.X, -e.Y);
Point temp = this.oldOrigin;
temp.Offset(delta.X, delta.Y);
this.Origin = temp;
Refresh();
}
base.OnMouseMove (e);
}
/// <summary>
/// OnMouseUp handler. Clean up the scrolling flag when we're done.
/// </summary>
/// <param name="e"></param>
protected override void OnMouseUp(MouseEventArgs e) {
this.isScrolling = false;
base.OnMouseUp (e);
}
}
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.