AddDate.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 "AddDate.h" AddDateTool::AddDateTool() { m_ipHookHelper.CreateInstance(CLSID_HookHelper); // Load the cursor ISystemMouseCursorPtr ipSysMouseCur(CLSID_SystemMouseCursor); ipSysMouseCur->LoadFromFile(CComBSTR(L"./Res/date.cur")); OLE_HANDLE hTmp; HRESULT hr = ipSysMouseCur->get_Cursor(&hTmp); if (SUCCEEDED(hr)) { m_hCursor = hTmp; } // Load the bitmap IRasterPicturePtr ipRastPict(CLSID_BasicRasterPicture); IPicturePtr ipPict; hr = ipRastPict->LoadPicture(CComBSTR(L"./Res/date.bmp"), &ipPict); if (SUCCEEDED(hr)) { OLE_HANDLE hBitmap; hr = ipPict->get_Handle(&hBitmap); if (SUCCEEDED(hr)) m_hBitmap = hBitmap; } } AddDateTool::~AddDateTool() { m_ipHookHelper = 0; m_hBitmap = 0; m_hCursor = 0; } HRESULT AddDateTool::get_Enabled(VARIANT_BOOL* Enabled) { if (!Enabled) return E_POINTER; *Enabled = VARIANT_TRUE; return S_OK; } HRESULT AddDateTool::get_Checked(VARIANT_BOOL* Checked) { if (!Checked) return E_POINTER; return S_OK; } HRESULT AddDateTool::get_Name(BSTR* Name) { if (!Name) return E_POINTER; *Name = ::AoAllocBSTR(L"CustomCommands_AddDate"); return S_OK; } HRESULT AddDateTool::get_Caption(BSTR* Caption) { if (!Caption) return E_POINTER; *Caption = ::AoAllocBSTR(L"Add Date"); return S_OK; } HRESULT AddDateTool::get_Tooltip(BSTR* Tooltip) { if (!Tooltip) return E_POINTER; *Tooltip = ::AoAllocBSTR(L"Add date"); return S_OK; } HRESULT AddDateTool::get_Message(BSTR* Message) { if (!Message) return E_POINTER; *Message = ::AoAllocBSTR(L"Adds a date element to the page layout"); return S_OK; } HRESULT AddDateTool::get_Bitmap(OLE_HANDLE* bitmap) { if (!bitmap) return E_POINTER; if (m_hBitmap != 0) { *bitmap = m_hBitmap; return S_OK; } return E_FAIL; } HRESULT AddDateTool::get_Category(BSTR* categoryName) { if (!categoryName) return E_POINTER; *categoryName = ::AoAllocBSTR(L"CustomCommands"); return S_OK; } // Create the command and set who it will work with HRESULT AddDateTool::OnCreate(IDispatch* hook) { if (!hook) return E_POINTER; m_ipHookHelper->putref_Hook(hook); return S_OK; } HRESULT AddDateTool::OnClick() { return S_OK; } HRESULT AddDateTool::get_Cursor(OLE_HANDLE* cursorName) { if (cursorName == NULL) return E_POINTER; if (m_hCursor != 0) { *cursorName = m_hCursor; return S_OK; } return E_FAIL; } // Add the date to the page layout where the mouse is HRESULT AddDateTool::OnMouseDown(LONG Button, LONG Shift, LONG X, LONG Y) { if (Button == 1) { // Format the date & create a text element char* dateDisplay = FormatDate(); ITextElementPtr ipDateTextElem(CLSID_TextElement); ipDateTextElem->put_Text(CComBSTR(dateDisplay)); delete[] dateDisplay; ITextSymbolPtr ipDateTextSymb(CLSID_TextSymbol); // Add it to the text element ipDateTextElem->put_Symbol(ipDateTextSymb); // Get point in map display coordinates IActiveViewPtr ipActiveView; m_ipHookHelper->get_ActiveView(&ipActiveView); IScreenDisplayPtr ipScreenDisplay; ipActiveView->get_ScreenDisplay(&ipScreenDisplay); IDisplayTransformationPtr ipDisplayTrans; ipScreenDisplay->get_DisplayTransformation(&ipDisplayTrans); IPointPtr ipPoint; ipDisplayTrans->ToMapPoint(X, Y, &ipPoint); // Set the element's geometry ((IElementPtr) ipDateTextElem)->put_Geometry(ipPoint); // Add element to the page layout's graphics container IGraphicsContainerPtr ipGraphicsContainer; ipActiveView->get_GraphicsContainer(&ipGraphicsContainer); ipGraphicsContainer->AddElement((IElementPtr) ipDateTextElem, 0); ipActiveView->PartialRefresh(esriViewGraphics, NULL, NULL); } return S_OK; } HRESULT AddDateTool::OnMouseMove(LONG Button, LONG Shift, LONG X, LONG Y) { return E_NOTIMPL; } HRESULT AddDateTool::OnMouseUp(LONG Button, LONG Shift, LONG X, LONG Y) { return E_NOTIMPL; } HRESULT AddDateTool::OnDblClick() { return E_NOTIMPL; } HRESULT AddDateTool::OnKeyDown(LONG keyCode, LONG Shift) { return E_NOTIMPL; } HRESULT AddDateTool::OnKeyUp(LONG keyCode, LONG Shift) { return E_NOTIMPL; } HRESULT AddDateTool::OnContextMenu(LONG X, LONG Y, VARIANT_BOOL* handled) { return E_NOTIMPL; } HRESULT AddDateTool::Refresh(OLE_HANDLE ole) { return E_NOTIMPL; } HRESULT AddDateTool::Deactivate(VARIANT_BOOL* complete) { if (!complete) return E_POINTER; *complete = VARIANT_TRUE; return S_OK; } char* AddDateTool::FormatDate() { time_t dateInfo = time(NULL); tm* todaysDate = localtime(&dateInfo); int month = todaysDate->tm_mon + 1; int day = todaysDate->tm_mday; int year = todaysDate->tm_year + 1900; char* dateDisplay = new char[12]; sprintf(dateDisplay, "%d/%d/%d\n", month, day, year); return dateDisplay; }