using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace J2i.Net.FindMe.Controls
{
public partial class MapDisplay : Control
{
delegate void SetMapDelegate(Image i);
enum RenderOperation:int
{
SourceCopy = 0xCC0020
}
Bitmap _map;
//Bitmap _backBuffer;
SolidBrush _backgroundColor = new SolidBrush(Color.White);
Brush _fontBrush = new SolidBrush(Color.Blue);
Font _defaultFont = new Font("Tahoma", 10f, FontStyle.Regular);
Point _centerDisplacement;
Point _originalCenter;
//Point _displayCenterDisplacement;
Rectangle _destinationRectangle;
Rectangle _sourceRectangle;
bool _moveMap = false;
Point _mouseStart;
Point _lowerBoundry;
Point _upperBoundry;
SetMapDelegate _setMap;
[DllImport("coredll")]
private static extern int BitBlt(IntPtr dest, int xdest, int ydest, int width, int height, IntPtr hSrc, int xsrc, int ysrc, RenderOperation operation);
public Image MapImage
{
set
{
SetMap(value);
}
}
void SetMap(Image i)
{
if (this.InvokeRequired)
{
this.Invoke(_setMap, new object[]{i});
}
else
{
if (i is Bitmap)
_map = i as Bitmap;
else
_map = new Bitmap(i);
_centerDisplacement = new Point(0, 0);
//_displayCenterDisplacement = new Point(0,0);
UpdateDestination();
UpdateDisplayBuffer();
this.Invalidate();
}
}
public Point CenterDisplacement
{
get { return _centerDisplacement; }
set
{
int x, y;
x = value.X;
y = value.Y;
if (x < _lowerBoundry.X)
x = _lowerBoundry.X;
else if (x > _upperBoundry.X)
x = _upperBoundry.X;
if (y < _lowerBoundry.Y)
y = _lowerBoundry.Y;
else if (y > _upperBoundry.Y)
y = _upperBoundry.Y;
_centerDisplacement.X = x;
_centerDisplacement.Y = y;
UpdateDisplayBuffer();
Graphics g = this.CreateGraphics();
RedrawMap(g);
g.Dispose();
}
}
private bool MoveMap
{
get { return _moveMap; }
set { _moveMap = value; }
}
//private Point DisplayCenterDisplacement
//{
// get { return _displayCenterDisplacement; }
// set { _displayCenterDisplacement = value; }
//}
public MapDisplay()
{
InitializeComponent();
_setMap = new SetMapDelegate(SetMap);
}
void UpdateDisplayBuffer()
{
if((_destinationRectangle!=null)&&(_map!=null))
{
int startX = (_map.Width / 2) + _centerDisplacement.X - (_destinationRectangle.Width / 2);
int startY = (_map.Height / 2) + _centerDisplacement.Y - (_destinationRectangle.Height / 2);
int width = _destinationRectangle.Width;
int height = _destinationRectangle.Height;
_sourceRectangle = new Rectangle(startX, startY, width, height);
// _upperBoundry = new Point(_map.Width-(_destinationRectangle.Width/2),_map.Height-(-_destinationRectangle.Height/2));
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
MoveMap = true;
_mouseStart = new Point(e.X,e.Y);
_originalCenter = CenterDisplacement;
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if(MoveMap)
{
int displacementX, displacementY;
displacementX = e.X - _mouseStart.X;
displacementY = e.Y - _mouseStart.Y;
// Point originalCenter = CenterDisplacement;
Point newCenter = new Point(_originalCenter.X - displacementX, _originalCenter.Y - displacementY);
this.Text = String.Format("({0},{1}) - ({2},{3})", CenterDisplacement.X, CenterDisplacement.Y,newCenter.X, newCenter.Y);
CenterDisplacement = newCenter;
}
//base.OnMouseMove(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
MoveMap = false;
this.Text = String.Empty;
}
private void UpdateBoundries()
{
if ((_map != null))
{
_lowerBoundry= new Point(
-_map.Width/2 + this.Width / 2,
-_map.Height/2 + this.Height / 2);
_upperBoundry = new Point(
_map.Width/2- this.Width / 2,
_map.Height/2- this.Height - 2);
}
}
void UpdateDestination()
{
_destinationRectangle = new Rectangle(0, 0, this.Width, this.Height);
UpdateBoundries();
}
protected override void OnResize(EventArgs e)
{
UpdateDestination();
UpdateBoundries();
base.OnResize(e);
}
void RedrawMap(Graphics g)
{
//
if(_map!=null)
g.DrawImage(_map, _destinationRectangle, _sourceRectangle, GraphicsUnit.Pixel);
else
g.FillRectangle(_backgroundColor, _destinationRectangle);
//g.DrawString(this.Text, _defaultFont, _fontBrush, 0, 0);
}
protected override void OnPaint(PaintEventArgs pe)
{
RedrawMap(pe.Graphics);;
}
}
}