====== Source Code DesktopEditionSample RestrictingExtent CS 101103.zip ======
====Program.cs====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace RestrictingExtent
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TestForm());
}
}
}
====TestForm.cs====
using System;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.DesktopEdition;
namespace RestrictingExtent
{
public partial class TestForm : Form
{
RectangleShape restrictedExtentForPanning = new RectangleShape(-127, 51, -65, 21);
RectangleShape restrictedExtentForZoomOut = null;
public TestForm()
{
InitializeComponent();
}
private void TestForm_Load(object sender, EventArgs e)
{
//Gets the restricted extent for zooming out based on the restrcited extent for panning
restrictedExtentForZoomOut = (RectangleShape)restrictedExtentForPanning.CloneDeep();
restrictedExtentForZoomOut.ScaleUp(80);
winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
winformsMap1.CurrentExtent = restrictedExtentForPanning;
winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.FromArgb(255, 198, 255, 255));
//Displays the World Map Kit as a background.
ThinkGeo.MapSuite.DesktopEdition.WorldMapKitWmsDesktopOverlay worldMapKitDesktopOverlay = new ThinkGeo.MapSuite.DesktopEdition.WorldMapKitWmsDesktopOverlay();
winformsMap1.Overlays.Add(worldMapKitDesktopOverlay);
//InMemoryFeatureLayer to show the restricted extent. It is just for display.
InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();
inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Transparent,
GeoColor.StandardColors.Green, 3);
inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
inMemoryFeatureLayer.Open();
inMemoryFeatureLayer.InternalFeatures.Add(new Feature(restrictedExtentForPanning));
inMemoryFeatureLayer.InternalFeatures.Add(new Feature(restrictedExtentForZoomOut));
inMemoryFeatureLayer.Close();
LayerOverlay layerOverlay = new LayerOverlay();
layerOverlay.Layers.Add(inMemoryFeatureLayer);
winformsMap1.Overlays.Add(layerOverlay);
winformsMap1.Refresh();
}
private void winformsMap1_CurrentExtentChanging(object sender, CurrentExtentChangingWinformsMapEventArgs e)
{
//While changing zoom level. Does not allow zooming out beyond the restricted extent by using the geometric
//function IsWithin. The new extent has to be within the restricted extent or the zoomin out will be canceled.
if (e.CurrentExtent.GetBoundingBox().Width != e.NewExtent.GetBoundingBox().Width)
{
if (e.NewExtent.IsWithin(restrictedExtentForZoomOut) == false)
{
e.Cancel = true;
}
}
//While panning. It checks for the difference in area of the overlap (GetIntersection) of the current extent and the
//new extent with the restricted extent for panning. That way, we can be outside the restricted extent for panning after
//zooming out and still pan to get more within the restricted extent for panning.
else
{
RectangleShape currentIntersection = restrictedExtentForPanning.GetIntersection(e.CurrentExtent);
RectangleShape newIntersection = restrictedExtentForPanning.GetIntersection(e.NewExtent);
//Gets the area. Meter is an arbitrary unit.
double currentArea = currentIntersection.GetArea(GeographyUnit.Meter, AreaUnit.SquareMeters);
double newArea = newIntersection.GetArea(GeographyUnit.Meter, AreaUnit.SquareMeters);
if (newArea < currentArea)
{
e.Cancel = true;
}
}
}
private void winformsMap1_MouseMove(object sender, MouseEventArgs e)
{
//Displays the X and Y in screen coordinates.
statusStrip1.Items["toolStripStatusLabelScreen"].Text = "X:" + e.X + " Y:" + e.Y;
//Gets the PointShape in world coordinates from screen coordinates.
PointShape pointShape = ExtentHelper.ToWorldCoordinate(winformsMap1.CurrentExtent, new ScreenPointF(e.X, e.Y), winformsMap1.Width, winformsMap1.Height);
//Displays world coordinates.
statusStrip1.Items["toolStripStatusLabelWorld"].Text = "(world) X:" + Math.Round(pointShape.X, 4) + " Y:" + Math.Round(pointShape.Y, 4);
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}