Geodesy MapControl
MapControlEvents.cpp
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 


#include "MapControlEvents.h"

void MapControlEvents::OnAfterDraw(VARIANT display, long viewDrawPhase)
{}

void MapControlEvents::OnAfterScreenDraw(long hdc)
{}

void MapControlEvents::OnBeforeScreenDraw(long hdc)
{}

void MapControlEvents::OnDoubleClick(long button, long shift, long x,
                                     long y, double mapX, double mapY)
{}

void MapControlEvents::OnExtentUpdated(VARIANT displayTransformation,
                                       VARIANT_BOOL sizeChanged,
                                       VARIANT newEnvelope)
{}

void MapControlEvents::OnFullExtentUpdated(VARIANT displayTransformation,
                                           VARIANT newEnvelope)
{}

void MapControlEvents::OnKeyDown(long keyCode, long shift)
{}

void MapControlEvents::OnKeyUp(long keyCode, long shift)
{}

void MapControlEvents::OnMapReplaced(VARIANT newMap)
{}

void MapControlEvents::OnMouseDown(long button, long shift, long x, long y,
                                   double mapX,double mapY)
{
    /*
     On a right mouse button click use the TrackPolygon method on the map control
     to define the vertices of an eventual GeoPolygon. Each right mouse button
     click adds a vertex to the polygon; double-clicking the right mouse button
     completes the polygon.
    */
    if(button == 2 )
    {
        /*
         Create the polygon from the TrackPolygon method. This polygon will be
             the foundation for a GeoPolygon object.
        */
        IGeometryPtr ipPolyGeom;
            m_ipMapControl->TrackPolygon(&ipPolyGeom);
        IPolygonPtr ipPoly = ipPolyGeom;
        IGeoPolygonPtr ipGeoPoly(CLSID_GeoPolygon);
        ipGeoPoly->put_Polygon(ipPoly);

        /*
         Set the GeoPolyline type to form the segments of the GeoPolygon. In this case
             the segments will be Geodesic lines.
        */
        ipGeoPoly->put_SpecialGeolineType(cjmtkSGTGeodesic);
        //set polygon's spatial reference
        ISpatialReferencePtr ipSpatialRef;
        ISpatialReferenceFactory2Ptr ipSpatialFactory(CLSID_SpatialReferenceEnvironment);
        ipSpatialFactory->CreateSpatialReference(esriSRGeoCS_WGS1984, &ipSpatialRef);
        ipGeoPoly->putref_BaseSpatialReference(ipSpatialRef);

        //outline color
        IRgbColorPtr ipRgb(CLSID_RgbColor);
        ipRgb->put_Red(0);
        ipRgb->put_Green(0);
        ipRgb->put_Blue(255);

        //make fill color null
        IColorPtr ipNullColor(CLSID_RgbColor);
        ipNullColor->put_NullColor(VARIANT_TRUE);

        //create the line symbol for the polygon outline
        ILineSymbolPtr ipLineSym(CLSID_SimpleLineSymbol);
        ipLineSym->put_Color(ipRgb);
        ipLineSym->put_Width(1.5);

        //create the fill symbol
        IFillSymbolPtr ipFillSym(CLSID_SimpleFillSymbol);
        ipFillSym->put_Outline(ipLineSym);
        ipFillSym->put_Color(ipNullColor);

        /*
         Create the IElement to be rendered as a GeoPolygonElement and
             set its geometry to the GeoPolygon geometry. When a GeoPolygonElement
             graphic is moved from one location to another in the map control
             the GeoPolygon geometry is automatically updated based on the
             geographic location and the graphic is updated accordingly.
        */
        IElementPtr ipTrackPolyElement(CLSID_GeoPolygonElement);
        IGeometryPtr ipGeoPolyGeom = ipGeoPoly;
        ipTrackPolyElement->put_Geometry(ipGeoPolyGeom);

        //QI to IFillShapeElement to set the symbology of the GeoPolygon graphic
        IFillShapeElementPtr ipFillShapeElement = ipTrackPolyElement;
        ipFillShapeElement->put_Symbol(ipFillSym);

        //draw the key points polygon graphic
        drawElement(ipTrackPolyElement);
    }


}

void MapControlEvents::drawElement(IElement* ipElement)
  {
    //get ActiveView from MapControl
    IActiveViewPtr ipActiveView;
    m_ipMapControl->get_ActiveView(&ipActiveView);

    //get the GraphicsContainer and add element
    IGraphicsContainerPtr ipContainer;
    ipActiveView->get_GraphicsContainer(&ipContainer);
    ipContainer->AddElement(ipElement, 0);

    //refresh view to draw the added element
    ipActiveView->Refresh();
  }

void MapControlEvents::OnMouseMove(long button, long shift, long x, long y,
                                   double mapX, double mapY)
{}

void MapControlEvents::OnMouseUp(long button, long shift, long x, long y,
                                 double mapX, double mapY)
{}

void MapControlEvents::OnOleDrop(esriControlsDropAction dropAction,
                                 VARIANT dataObjectHelper, long* effect,
                                 long button, long shift, long x, long y)
{}

void MapControlEvents::OnSelectionChanged()
{}

void MapControlEvents::OnViewRefreshed(VARIANT ActiveView, long viewDrawPhase,
                                       VARIANT layerOrElement,
                                       VARIANT envelope)
{}