====== Source Code DesktopEditionSample DrawCustomException CS 100127.zip ======
====CustomWorldMapKitWmsDesktopOverlay.cs====
using System;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.DesktopEdition;
namespace DrawCustomException
{
// We inherit from the exisitng overlay to create a new one that helps us simulate
// going on and offline and drawing a custom exception. If you used this in production
// you would only need to override the DrawExceptionCore method and set the this.DrawExceptionMode
// The rest of the code in this class is just used to simulate going on and offline and
// showing you the difference between the default drawing and custom drawing of an exception
public class CustomWorldMapKitWmsDesktopOverlay : WorldMapKitWmsDesktopOverlay
{
private bool drawCustomException;
private bool online;
// In this public constructor we set the drawing exception mode.
public CustomWorldMapKitWmsDesktopOverlay()
: base()
{
this.DrawingExceptionMode = DrawingExceptionMode.DrawException;
drawCustomException = false;
online = true;
}
// This property is just fir us to simulate switching
// bewtween custom and standard exception drawing
public bool DrawCustomException
{
get { return drawCustomException; }
set { drawCustomException = value; }
}
// This just simulates being on and offline by throwing
// an error if you are offline
public bool Online
{
get { return online; }
set { online = value; }
}
// This code is just here to simulate being on and offline
protected override void DrawCore(GeoCanvas canvas)
{
if (online)
{
base.DrawCore(canvas);
}
else
{
throw new Exception("Unable to connect to the WMS server.");
}
}
// Here we override the DrawException method and do our own drawing of the exception.
// We use the drawCustomException only for the sample
protected override void DrawExceptionCore(GeoCanvas canvas, Exception e)
{
if (drawCustomException)
{
// We are writing directly to the canvas. Note that above we have access to the exception itself
// and we could make the image based on that.
canvas.DrawTextWithScreenCoordinate(e.Message, new GeoFont("Arial", 20), new GeoSolidBrush(new GeoColor(120, GeoColor.StandardColors.Red)), canvas.Width / 2, canvas.Height / 2, DrawingLevel.LevelOne);
}
else
{
base.DrawExceptionCore(canvas, e);
}
}
}
}
====Program.cs====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DrawCustomException
{
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.Diagnostics;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
namespace DrawCustomException
{
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
// This is a warning to let people know the sample will not run properly if the debugger is attached.
if (Debugger.IsAttached)
{
MessageBox.Show("For this sample to work properly you need to run it without the debugger attached. You can use Ctrl-F5 or in the debug menu choose 'Start Without Debugging.'");
}
}
private void TestForm_Load(object sender, EventArgs e)
{
// Set the full extent and the background color
winformsMap1.CurrentExtent = new RectangleShape(-124.89,49.36,-66.79,20.01);
winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
CustomWorldMapKitWmsDesktopOverlay worldMapKitDesktopOverlay = new CustomWorldMapKitWmsDesktopOverlay();
winformsMap1.Overlays.Add("WorldOverlay",worldMapKitDesktopOverlay);
// Draw the map image on the screen
winformsMap1.Refresh();
}
// If the user changes the status we change the online property in the overlay. Note this is just
// to simulate going on or offline. If this were not a sample this code would not be necessary
private void rbnOnline_CheckedChanged(object sender, EventArgs e)
{
((CustomWorldMapKitWmsDesktopOverlay)winformsMap1.Overlays["WorldOverlay"]).Online = rbnOnline.Checked;
winformsMap1.Refresh();
}
// If the user changes the status we change the drawing exception mode of the overlay. Note this is just
// to show different options. If this were not a sample this code would not be necessary. You would just
// set the one mode you want and leave it.
private void rbnException_CheckedChanged(object sender, EventArgs e)
{
// In this case we want to throw the exception
if (rbnThrowException.Checked)
{
((CustomWorldMapKitWmsDesktopOverlay)winformsMap1.Overlays["WorldOverlay"]).DrawingExceptionMode = DrawingExceptionMode.ThrowException;
}
// In this case we want to draw the excpetion using the deault drawing we provide in the overlay
if (rbnDrawException.Checked)
{
((CustomWorldMapKitWmsDesktopOverlay)winformsMap1.Overlays["WorldOverlay"]).DrawingExceptionMode = DrawingExceptionMode.DrawException;
((CustomWorldMapKitWmsDesktopOverlay)winformsMap1.Overlays["WorldOverlay"]).DrawCustomException = false;
}
// In this case we want to do our own custom exception drawing
if (rbnDrawCustomException.Checked)
{
((CustomWorldMapKitWmsDesktopOverlay)winformsMap1.Overlays["WorldOverlay"]).DrawingExceptionMode = DrawingExceptionMode.DrawException;
((CustomWorldMapKitWmsDesktopOverlay)winformsMap1.Overlays["WorldOverlay"]).DrawCustomException = true;
}
winformsMap1.Refresh();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
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);
}
}
}