18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

BUILDING APPLICATIONS WITH C++ AND CONTROL WIDGETS<br />

}<br />

The ICommand_OnCreate event is passed a<br />

handle or hook to the application that the<br />

command will work with. In this case it can be a<br />

MapControl, PageLayoutControl, or<br />

ToolbarControl. Rather than adding code to<br />

the OnCreate event to determine the type of<br />

hook that is being passed to the command, you<br />

will use the HookHelper to handle this. A<br />

command or tool needs to know how to handle<br />

the hook it gets passed, so a check is needed to<br />

determine the type of <strong>ArcGIS</strong> control that has<br />

been passed. The HookHelper is used to hold the<br />

hook and return the ActiveView regardless of the<br />

type of hook (in this case a MapControl,<br />

PageLayoutControl, or ToolbarControl).<br />

// Create the command and set who it will work with<br />

HRESULT AddDateTool::OnCreate(IDispatch* hook)<br />

{<br />

if (!hook)<br />

return E_POINTER;<br />

m_ipHookHelper->putref_Hook(hook);<br />

return S_OK;<br />

}<br />

HRESULT AddDateTool::OnClick()<br />

{<br />

return S_OK;<br />

}<br />

e. Write a function that will format the date for display on the<br />

PageLayoutControl.<br />

i. Before the class in AddDate.h, include the following header files:<br />

#include <br />

#include <br />

#include <br />

ii. Add a private function to the AddDate class in AddDate.h to take care<br />

of the formatting.<br />

OLE_HANDLE m_hCursor;<br />

char* FormatDate();<br />

iii. Implement the function at the bottom of AddDate.cpp.<br />

char* AddDateTool::FormatDate()<br />

{<br />

time_t dateInfo = time(NULL);<br />

tm* todaysDate = localtime(&dateInfo);<br />

int month = todaysDate->tm_mon + 1;<br />

int day = todaysDate->tm_mday;<br />

int year = todaysDate->tm_year + 1900;<br />

char* dateDisplay = new char[12];<br />

sprintf(dateDisplay, "%d/%d/%d\n", month, day, year);<br />

return dateDisplay;<br />

}<br />

f. Continue implementing your custom tool by stubbing out all the properties<br />

and events of the ITool interface before the FormatDate function in<br />

AddDate.cpp. Pay attention to the implementation of the OnMouseDown<br />

method, as it creates the date text element and adds it to the graphics<br />

container of the application.<br />

HRESULT AddDateTool::OnClick()<br />

{<br />

return S_OK;<br />

}<br />

Chapter 6 • <strong>Developer</strong> scenarios • 397

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!