MapWinGIS:SampleCode-C Sharp Net:DrawMapSubclass

This is a back-up of the WIKI.
Not all links might work
We're working on a new wiki.

Main Page | Recent changes | View source | Page history | Log in / create account |

Printable version | Disclaimers | Privacy policy

Inheriting the ActiveX Map control and exposing a new "Draw" event that uses GDI+ drawing

First, you create a completely new class that inherits from the map. This new control will override the default behavior that happens during a paint message (which is to control all its own drawing internally) and exposes a new event after the regular drawing has taken place. The OnDraw method is protected so that it can be called from subclasses of the DrawMap (which will fire the event), and it is made virtual so that it can be overridden in subclasses, so that standard behavior can be set up like a drawing class.

 //********************************************************************************************************
 // Product Name: DrawSubclassCS
 // Description:  C# Tutorial for sublcassing MapWindow map
 //********************************************************************************************************
 // The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
 // you may not use this file except in compliance with the License. You may obtain a copy of the License at 
 // http://www.mozilla.org/MPL/ 
 //
 // Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
 // ANY KIND, either expressed or implied. See the License for the specificlanguage governing rights and 
 // limitations under the License. 
 //
 // The Original Code is from MapWindow.dll version 6.0
 //
 // The Initial Developer of this Original Code is Ted Dunsford. Created 11/7/2008 1:22:13 PM
 // 
 // Contributor(s): (Open source contributors should list themselves and their modifications here). 
 //
 //********************************************************************************************************
 
 using System;
 using System.Collections.Generic;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
 
 
 namespace DrawSubclassCS
 {
 
 
     /// <summary>
     /// DrawMap inherits from the regular map, but overrides the paint methods
     /// </summary>
     public class DrawMap : AxMapWinGIS.AxMap
     {
         #region Events
 
         /// <summary>
         /// This event occurs after the map content has been drawn
         /// </summary>
         public event EventHandler<PaintEventArgs> Draw;
         
         #endregion
 
         #region Private Variables
 
         private const int WM_PAINT = 0xF;
 
         #endregion
 
         #region Constructors
 
         /// <summary>
         /// Creates a new instance of DrawMap
         /// </summary>
         public DrawMap()
         {
             
         }
 
         #endregion
 
         #region Protected Methods
 
         /// <summary>
         /// The basic method to receive the original paint action.  base.WndProc is called
         /// before the Draw event is launched in order to ensure that the map gets updated first.
         /// </summary>
         /// <param name="m">A Message argument.  The message is not important to this method.</param>
         protected override void WndProc(ref Message m)
         {
             base.WndProc(ref m);
             if (m.Msg == WM_PAINT)
             {
                 Graphics g = this.CreateGraphics();
                 OnDraw(new PaintEventArgs(g, ClientRectangle));
             }
         }
 
         /// <summary>
         /// This tests to see if there are any event handlers that have been set up to listen to this
         /// event.  If not, then do nothing.  Otherwise, evoke the handler now.
         /// </summary>
         /// <param name="pe">The PaintEventArgs that contains the information needed to draw to the form.</param>
         protected virtual void OnDraw(PaintEventArgs pe)
         {
             if (Draw != null) Draw(this, pe);
         }
 
         #endregion
 
 
     }
 }
 

And then to use this new control programatically:

 using System;
 using System.Drawing;
 using System.Windows.Forms;
 
 namespace DrawSubclassCS
 {
     public partial class Form1 : Form
     {
         internal DrawMap map;
 
         public Form1()
         {
             InitializeComponent();
             map = new DrawMap();
             this.map.Parent = this;
             this.map.Dock = DockStyle.Fill;
             this.map.Draw += new EventHandler<PaintEventArgs>(map_Draw);
         }
 
         void map_Draw(object sender, PaintEventArgs e)
         {
             e.Graphics.DrawString("test", new Font("Ariel", 30), Brushes.Red, new PointF(0, 0));
 
         }
     }
 }

Posted by Shade1974 on 11/7/2008

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:SampleCode-C_Sharp_Net:DrawMapSubclass"

This page has been accessed 1,338 times. This page was last modified on 7 November 2008, at 22:10.