Pan and zoom commands
PanDown.cpp
// Copyright 2011 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 "PanDown.h"

PanDown::PanDown()
{
  m_ipHookHelper.CreateInstance(CLSID_HookHelper);

  // Load the bitmap
  IRasterPicturePtr ipRastPict(CLSID_BasicRasterPicture);
  IPicturePtr ipPict;
  HRESULT hr = ipRastPict->LoadPicture(CComBSTR(L"./Res/PanDown.bmp"), &ipPict);
  if (SUCCEEDED(hr))
  {
    OLE_HANDLE hBitmap;
    hr = ipPict->get_Handle(&hBitmap);
    if (SUCCEEDED(hr))
      m_hBitmap = hBitmap;
  }
}

PanDown::~PanDown()
{
  m_ipHookHelper = 0;
  m_hBitmap = 0;

}

HRESULT PanDown::get_Enabled(VARIANT_BOOL* Enabled)
{
  if (!Enabled)
    return E_POINTER;

  IMapPtr ipMap;
  m_ipHookHelper->get_FocusMap(&ipMap);
  if (ipMap == 0)
    return S_OK;

  *Enabled = VARIANT_TRUE;
  return S_OK;
}

HRESULT PanDown::get_Checked(VARIANT_BOOL* Checked)
{
  if (!Checked)
    return E_POINTER;

  Checked = VARIANT_FALSE;
  return S_OK;
}

HRESULT PanDown::get_Name(BSTR* Name)
{
  if (!Name)
    return E_POINTER;
  
  *Name = ::AoAllocBSTR(L"Sample_Pan/Zoom_Pan Down");
  return S_OK;
}

HRESULT PanDown::get_Caption(BSTR* Caption) 
{
  if (!Caption)
    return E_POINTER;
  
  *Caption = ::AoAllocBSTR(L"Pan Down");
  return S_OK;
}

HRESULT PanDown::get_Tooltip(BSTR* Tooltip) 
{
  if (!Tooltip)
    return E_POINTER;
  
  *Tooltip = ::AoAllocBSTR(L"Pan Down");
  return S_OK;
}

HRESULT PanDown::get_Message(BSTR* Message) 
{
  if (!Message)
    return E_POINTER;
  
  *Message = ::AoAllocBSTR(L"Pan display down by the pan factor percentage");
  return S_OK;
}

HRESULT PanDown::get_Bitmap(OLE_HANDLE* bitmap) 
{
  if (!bitmap)
    return E_POINTER;
  
  if (m_hBitmap != 0)
  {
    *bitmap = m_hBitmap;
    return S_OK;
  }

  return E_FAIL;
}

HRESULT  PanDown::get_Category(BSTR* categoryName) 
{
  if (!categoryName)
    return E_POINTER;
  
  *categoryName = ::AoAllocBSTR(L"Sample_Pan/Zoom");
  return S_OK;
}

// Create the command and set who it will work with
HRESULT PanDown::OnCreate(IDispatch* hook) 
{
  if (!hook)
    return E_POINTER;
  
  m_ipHookHelper->putref_Hook(hook);
  return S_OK;
}

// When clicked, the button appears pressed
HRESULT PanDown::OnClick() 
{

  // Get the active view
  IMapPtr ipMap;
  m_ipHookHelper->get_FocusMap(&ipMap);
  IActiveViewPtr ipActiveView(ipMap);

  if (ipMap == 0)
    return S_OK;

  // Get the extent
  IEnvelopePtr ipEnv;
  ipActiveView->get_Extent(&ipEnv);

  // Create a point to pan to
  IPointPtr ipPoint(CLSID_Point);
  double dHeight;
  ipEnv->get_Height(&dHeight);
  double dXMin;
  ipEnv->get_XMin(&dXMin);
  double dXMax;
  ipEnv->get_XMax(&dXMax);
  double dYMin;
  ipEnv->get_YMin(&dYMin);
  double dYMax;
  ipEnv->get_YMax(&dYMax);
  double dPanFactor;
  GetPanFactor(&dPanFactor);
  ipPoint->put_X((dXMin + dXMax)/2);
  ipPoint->put_Y((dYMin + dYMax)/2 - (dHeight / (100 / dPanFactor)));

  // Center the envelope at the point
  ipEnv->CenterAt(ipPoint);
  // Set the new extent
  ipActiveView->put_Extent(ipEnv);
  // Refresh the active view
  ipActiveView->Refresh();

  return S_OK;
}

HRESULT PanDown::GetPanFactor(double* dPanFactor)
{
  *dPanFactor = 50;

  return S_OK;
}