ArcObjects Library Reference  

DynamicLogo

About the Dynamic logo Sample

[C#]

DynamicLogo.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using Microsoft.Win32;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS;


namespace DynamicLogo
{
  /// <summary>
  /// Command that works in ArcMap/Map/PageLayout
  /// </summary>
  [Guid("638eb76e-0b28-4538-92ba-89cebf4e1acb")]
  [ClassInterface(ClassInterfaceType.None)]
  [ProgId("DynamicLogo.DynamicLogo")]
  public sealed class DynamicLogo : BaseCommand
  {
    #region COM Registration Function(s)
    [ComRegisterFunction()]
    [ComVisible(false)]
    static void RegisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryRegistration(registerType);

      //
      // TODO: Add any COM registration code here
      //
    }

    [ComUnregisterFunction()]
    [ComVisible(false)]
    static void UnregisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryUnregistration(registerType);

      //
      // TODO: Add any COM unregistration code here
      //
    }

    #region ArcGIS Component Category Registrar generated code
    /// <summary>
    /// Required method for ArcGIS Component Category registration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryRegistration(Type registerType)
    {
      string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
      ControlsCommands.Register(regKey);
    }
    /// <summary>
    /// Required method for ArcGIS Component Category unregistration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryUnregistration(Type registerType)
    {
      string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
      ControlsCommands.Unregister(regKey);
    }

    #endregion
    #endregion

    private IHookHelper m_hookHelper = null;
    private string m_logoPath = string.Empty;
    private ISymbol m_logoSymbol = null;
    public bool m_bOnce = true;
    private IDynamicGlyph m_logoGlyph = null;
    private IDynamicGlyphFactory m_dynamicGlyphFactory = null;
    private IDynamicSymbolProperties m_dynamicSymbolProps = null;
    private IDynamicDrawScreen m_dynamicDrawScreen = null;
    private IPoint m_point;
    private bool m_bIsOn = false;
    private IActiveViewEvents_Event avEvents;


    public DynamicLogo()
    {
      //
      // TODO: Define values for the public properties
      //
      base.m_category = ".NET Samples"; //localizable text
      base.m_caption = "Show Logo";  //localizable text 
      base.m_message = "Show or hide the logo";  //localizable text
      base.m_toolTip = "Show or hide the logo";  //localizable text
      base.m_name = "DynamicLogo_ShowDynamicLogo";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

      try
      {
        string bitmapResourceName = GetType().Name + ".bmp";
        base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
      }
    }

    #region Overriden Class Methods

    /// <summary>
    /// Occurs when this command is created
    /// </summary>
    /// <param name="hook">Instance of the application</param>
    public override void OnCreate(object hook)
    {
      if (hook == null)
        return;

      try
      {
        m_hookHelper = new HookHelperClass();
        m_hookHelper.Hook = hook;
        if (m_hookHelper.ActiveView == null)
          m_hookHelper = null;
      }
      catch
      {
        m_hookHelper = null;
      }

      if (m_hookHelper == null)
        base.m_enabled = false;
      else
        base.m_enabled = true;


    }

    /// <summary>
    /// Occurs when this command is clicked
    /// </summary>
    public override void OnClick()
    {
      m_logoPath = GetLogoPath();
      IMap map = m_hookHelper.FocusMap;
      IDynamicMap dynamicMap = map as IDynamicMap;

      IActiveView activeView = map as IActiveView;
      /*IActiveViewEvents_Event */avEvents = activeView as IActiveViewEvents_Event;
      IDynamicMapEvents_Event dynamicMapEvents = dynamicMap as IDynamicMapEvents_Event;
      IScreenDisplay screenDisplay = activeView.ScreenDisplay;

      if (!m_bIsOn)
      {

        avEvents.AfterDraw += new IActiveViewEvents_AfterDrawEventHandler(avEvents_AfterDraw);
        dynamicMapEvents.AfterDynamicDraw += new IDynamicMapEvents_AfterDynamicDrawEventHandler(dynamicMapEvents_AfterDynamicDraw);

      }
      else
      {
        dynamicMapEvents.AfterDynamicDraw -= new IDynamicMapEvents_AfterDynamicDrawEventHandler(dynamicMapEvents_AfterDynamicDraw);
        avEvents.AfterDraw -= new IActiveViewEvents_AfterDrawEventHandler(avEvents_AfterDraw);
      }
      m_bIsOn = !m_bIsOn;
      screenDisplay.Invalidate(null, true, (short)esriScreenCache.esriNoScreenCache);
      screenDisplay.UpdateWindow();
    }

    public override bool Checked
    {
      get
      {
        return m_bIsOn;
      }
    }

    void dynamicMapEvents_AfterDynamicDraw(esriDynamicMapDrawPhase DynamicMapDrawPhase, IDisplay Display, IDynamicDisplay dynamicDisplay)
    {
      if (DynamicMapDrawPhase != esriDynamicMapDrawPhase.esriDMDPDynamicLayers)
        return;
      DrawDynamicLogo(dynamicDisplay);


    }

    private void DrawDynamicLogo(IDynamicDisplay dynamicDisplay)
    {
      if (m_bOnce)
      {
        //cast the DynamicDisplay into DynamicGlyphFactory
        m_dynamicGlyphFactory = dynamicDisplay.DynamicGlyphFactory;
        //cast the DynamicDisplay into DynamicSymbolProperties
        m_dynamicSymbolProps = dynamicDisplay as IDynamicSymbolProperties;

        m_dynamicDrawScreen = dynamicDisplay as IDynamicDrawScreen;

        //create the dynamic glyph for the logo
        m_logoGlyph = m_dynamicGlyphFactory.CreateDynamicGlyphFromFile(esriDynamicGlyphType.esriDGlyphMarker, m_logoPath, ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(Color.White));

        m_point = new PointClass();
        m_point.PutCoords(120, 160);
        m_bOnce = false;
      }

      m_dynamicSymbolProps.set_DynamicGlyph(esriDynamicSymbolType.esriDSymbolMarker, m_logoGlyph);
      m_dynamicSymbolProps.SetScale(esriDynamicSymbolType.esriDSymbolMarker, .435f, .435f);
      m_dynamicDrawScreen.DrawScreenMarker(m_point);
    }

    void avEvents_AfterDraw(ESRI.ArcGIS.Display.IDisplay Display, esriViewDrawPhase phase)
    {
      if (phase != esriViewDrawPhase.esriViewForeground)
        return;
      DrawLogoStandard(Display);
    }

    private void DrawLogoStandard(IDisplay Display)
    {
      tagRECT r = Display.DisplayTransformation.get_DeviceFrame();
      Display.StartDrawing(Display.hDC, (short)esriScreenCache.esriNoScreenCache);
      if (null == m_logoSymbol)
      {
        m_logoSymbol = CreateStandardLogoSymbol();
      }
      Display.SetSymbol(m_logoSymbol);
      Display.DrawPoint(Display.DisplayTransformation.ToMapPoint(120, r.bottom - 160));
      Display.FinishDrawing();
    }

    #endregion

    private string GetLogoPath()
    {
      //get the ArcGIS path from the registry
      string runtimeVersion = RuntimeManager.ActiveRuntime.Version;
      RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ESRI\ArcObjectsSDK" + runtimeVersion);
      string path = Convert.ToString(key.GetValue("InstallDir"));

      //set the of the logo
      string str = System.IO.Path.Combine(path, @"Samples\data\ESRILogo\ESRI_LOGO.bmp");
      if (!System.IO.File.Exists(str))
      {
        MessageBox.Show("Path :" + str + " does not exists!");
        return string.Empty;
      }

      return str;
    }

    private ISymbol CreateStandardLogoSymbol()
    {
      IPictureMarkerSymbol pictureMarkerSymbol = new PictureMarkerSymbolClass();
      pictureMarkerSymbol.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, m_logoPath);
      pictureMarkerSymbol.Size = 100;
      IColor whiteTransparencyColor = ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(Color.FromArgb(255, 255, 255)) as IColor;
      pictureMarkerSymbol.BitmapTransparencyColor = whiteTransparencyColor;

      return pictureMarkerSymbol as ISymbol;
    }
  }
}

[Visual Basic .NET]

DynamicLogo.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Display
Imports Microsoft.Win32
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.ADF
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS

''' <summary>
''' Command that works in ArcMap/Map/PageLayout
''' </summary>
<Guid("638eb76e-0b28-4538-92ba-89cebf4e1acb"), ClassInterface(ClassInterfaceType.None), ProgId("DynamicLogo.DynamicLogo")> _
Public NotInheritable Class DynamicLogo : Inherits BaseCommand
#Region "COM Registration Function(s)"
  <ComRegisterFunction(), ComVisible(False)> _
  Private Shared Sub RegisterFunction(ByVal registerType As Type)
    ' Required for ArcGIS Component Category Registrar support
    ArcGISCategoryRegistration(registerType)

    '
    ' TODO: Add any COM registration code here
    ''
  End Sub

  <ComUnregisterFunction(), ComVisible(False)> _
  Private Shared Sub UnregisterFunction(ByVal registerType As Type)
    ' Required for ArcGIS Component Category Registrar support
    ArcGISCategoryUnregistration(registerType)

    '
    ' TODO: Add any COM unregistration code here
    ''
  End Sub

#Region "ArcGIS Component Category Registrar generated code"
  ''' <summary>
  ''' Required method for ArcGIS Component Category registration -
  ''' Do not modify the contents of this method with the code editor.
  ''' </summary>
  Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
    Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
    ControlsCommands.Register(regKey)
  End Sub
  ''' <summary>
  ''' Required method for ArcGIS Component Category unregistration -
  ''' Do not modify the contents of this method with the code editor.
  ''' </summary>
  Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
    Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
    ControlsCommands.Unregister(regKey)
  End Sub

#End Region
#End Region

  Private m_hookHelper As IHookHelper = Nothing
  Private m_logoPath As String = String.Empty
  Private m_logoSymbol As ISymbol = Nothing
  Public m_bOnce As Boolean = True
  Private m_logoGlyph As IDynamicGlyph = Nothing
  Private m_dynamicGlyphFactory As IDynamicGlyphFactory = Nothing
  Private m_dynamicSymbolProps As IDynamicSymbolProperties = Nothing
  Private m_dynamicDrawScreen As IDynamicDrawScreen = Nothing
  Private m_point As IPoint
    Private m_bIsOn As Boolean = False
    Private avEvents As IActiveViewEvents_Event



  Public Sub New()
    '
    ' TODO: Define values for the public properties
    '
    MyBase.m_category = ".NET Samples" 'localizable text
    MyBase.m_caption = "Show Logo" 'localizable text
    MyBase.m_message = "Show or hide the logo" 'localizable text
    MyBase.m_toolTip = "Show or hide the logo" 'localizable text
    MyBase.m_name = "DynamicLogo_ShowDynamicLogo" 'unique id, non-localizable (e.g. "MyCategory_MyCommand")

    Try
      Dim bitmapResourceName As String = Me.GetType().Name & ".bmp"
      MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
    Catch ex As Exception
      System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
    End Try
  End Sub

#Region "Overridden Class Methods"

  ''' <summary>
  ''' Occurs when this command is created
  ''' </summary>
  ''' <param name="hook">Instance of the application</param>
  Public Overrides Sub OnCreate(ByVal hook As Object)
    If hook Is Nothing Then
      Return
    End If

    Try
      m_hookHelper = New HookHelperClass()
      m_hookHelper.Hook = hook
      If m_hookHelper.ActiveView Is Nothing Then
        m_hookHelper = Nothing
      End If
    Catch
      m_hookHelper = Nothing
    End Try

    If m_hookHelper Is Nothing Then
      MyBase.m_enabled = False
    Else
      MyBase.m_enabled = True
    End If


  End Sub

  ''' <summary>
  ''' Occurs when this command is clicked
  ''' </summary>
  Public Overrides Sub OnClick()
    m_logoPath = GetLogoPath()
    Dim map As IMap = m_hookHelper.FocusMap
    Dim dynamicMap As IDynamicMap = TryCast(map, IDynamicMap)

    Dim activeView As IActiveView = TryCast(map, IActiveView)
        'Dim avEvents As IActiveViewEvents_Event 
        avEvents = TryCast(activeView, IActiveViewEvents_Event)
    Dim dynamicMapEvents As IDynamicMapEvents_Event = TryCast(dynamicMap, IDynamicMapEvents_Event)
    Dim screenDisplay As IScreenDisplay = activeView.ScreenDisplay

    If (Not m_bIsOn) Then

      AddHandler avEvents.AfterDraw, AddressOf avEvents_AfterDraw
      AddHandler dynamicMapEvents.AfterDynamicDraw, AddressOf dynamicMapEvents_AfterDynamicDraw

    Else
      RemoveHandler dynamicMapEvents.AfterDynamicDraw, AddressOf dynamicMapEvents_AfterDynamicDraw
      RemoveHandler avEvents.AfterDraw, AddressOf avEvents_AfterDraw
    End If
    m_bIsOn = Not m_bIsOn
    screenDisplay.Invalidate(Nothing, True, CShort(esriScreenCache.esriNoScreenCache))
    screenDisplay.UpdateWindow()
  End Sub

  Public Overrides ReadOnly Property Checked() As Boolean
    Get
      Return m_bIsOn
    End Get
  End Property

  Private Sub dynamicMapEvents_AfterDynamicDraw(ByVal DynamicMapDrawPhase As esriDynamicMapDrawPhase, ByVal Display As IDisplay, ByVal dynamicDisplay As IDynamicDisplay)
    If DynamicMapDrawPhase <> esriDynamicMapDrawPhase.esriDMDPDynamicLayers Then
      Return
    End If
    DrawDynamicLogo(dynamicDisplay)


  End Sub

  Private Sub DrawDynamicLogo(ByVal dynamicDisplay As IDynamicDisplay)
    If m_bOnce Then
      'cast the DynamicDisplay into DynamicGlyphFactory
      m_dynamicGlyphFactory = dynamicDisplay.DynamicGlyphFactory
      'cast the DynamicDisplay into DynamicSymbolProperties
      m_dynamicSymbolProps = TryCast(dynamicDisplay, IDynamicSymbolProperties)

      m_dynamicDrawScreen = TryCast(dynamicDisplay, IDynamicDrawScreen)

      'create the dynamic glyph for the logo
      m_logoGlyph = m_dynamicGlyphFactory.CreateDynamicGlyphFromFile(esriDynamicGlyphType.esriDGlyphMarker, m_logoPath, ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(Color.White))

      m_point = New PointClass()
      m_point.PutCoords(120, 160)
      m_bOnce = False
    End If

    m_dynamicSymbolProps.DynamicGlyph(esriDynamicSymbolType.esriDSymbolMarker) = m_logoGlyph
    m_dynamicSymbolProps.SetScale(esriDynamicSymbolType.esriDSymbolMarker, 0.435F, 0.435F)
    m_dynamicDrawScreen.DrawScreenMarker(m_point)
  End Sub

  Private Sub avEvents_AfterDraw(ByVal Display As ESRI.ArcGIS.Display.IDisplay, ByVal phase As esriViewDrawPhase)
    If phase <> esriViewDrawPhase.esriViewForeground Then
      Return
    End If
    DrawLogoStandard(Display)
  End Sub

  Private Sub DrawLogoStandard(ByVal Display As IDisplay)
    Dim r As tagRECT = Display.DisplayTransformation.DeviceFrame
    Display.StartDrawing(Display.hDC, CShort(esriScreenCache.esriNoScreenCache))
    If Nothing Is m_logoSymbol Then
      m_logoSymbol = CreateStandardLogoSymbol()
    End If
    Display.SetSymbol(m_logoSymbol)
    Display.DrawPoint(Display.DisplayTransformation.ToMapPoint(120, r.bottom - 160))
    Display.FinishDrawing()
  End Sub

#End Region

  Private Function GetLogoPath() As String
    'get the ArcGIS path from the registry
    Dim runtimeVersion As String = RuntimeManager.ActiveRuntime.Version
    Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\ESRI\ArcObjectsSDK" + runtimeVersion)
    Dim path As String = Convert.ToString(key.GetValue("InstallDir"))

    'set the of the logo
    Dim str As String = System.IO.Path.Combine(path, "Samples\data\ESRILogo\ESRI_LOGO.bmp")
    If (Not System.IO.File.Exists(str)) Then
      MessageBox.Show("Path :" & str & " does not exists!")
      Return String.Empty
    End If

    Return str
  End Function

  Private Function CreateStandardLogoSymbol() As ISymbol
    Dim pictureMarkerSymbol As IPictureMarkerSymbol = New PictureMarkerSymbolClass()
    pictureMarkerSymbol.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, m_logoPath)
    pictureMarkerSymbol.Size = 100
    Dim whiteTransparencyColor As IColor = TryCast(ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(Color.FromArgb(255, 255, 255)), IColor)
    pictureMarkerSymbol.BitmapTransparencyColor = whiteTransparencyColor

    Return TryCast(pictureMarkerSymbol, ISymbol)
  End Function
End Class