📄 enginecontrolsdlg.cpp
字号:
#include "stdafx.h"
#include "EngineControlsDlg.h"
// Type information for the event handlers
_ATL_FUNC_INFO OnAfterDrawInfo = {CC_STDCALL, VT_I4, 2, {VT_VARIANT, VT_I4}};
_ATL_FUNC_INFO OnMouseDownInfo = {CC_STDCALL, VT_I4, 6, {VT_I4, VT_I4, VT_I4, VT_I4, VT_R8, VT_R8}};
_ATL_FUNC_INFO OnPageLayoutReplacedInfo = {CC_STDCALL, VT_I4, 1, {VT_VARIANT}};
_ATL_FUNC_INFO OnEndLabelEditInfo = {CC_STDCALL, VT_I4, 4, {VT_I4, VT_I4, VT_BSTR, VT_BOOL}};
/////////////////////////////////////////////////////////////////////////////
// Message Handlers
LRESULT CEngineControlsDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// Get the Control's interfaces
GetDlgControl(IDC_TOOLBARCONTROL, IID_IToolbarControl, (void **) &m_ipToolbarControl);
GetDlgControl(IDC_TOCCONTROL, IID_ITOCControl, (void **) &m_ipTOCControl);
GetDlgControl(IDC_MAPCONTROL, IID_IMapControl2, (void **) &m_ipMapControl);
GetDlgControl(IDC_PAGELAYOUTCONTROL, IID_IPageLayoutControl, (void **) &m_ipPageLayoutControl);
// Connect to the controls
AtlAdviseSinkMap(this, true);
// Create the commands for the toolbar
CreateToolbarItems();
// Create the customize dialog for the ToolbarControl
CreateCustomizeDialog();
// Create symbol used on the MapConrol
CreateOverviewSymbol();
// Set label editing to manual
m_ipTOCControl->put_LabelEdit(esriTOCControlManual);
// Check and load a pre-authored map document into the PageLayoutControl using relative paths
CComBSTR bstrFileName;
GetCurrentDir(bstrFileName);
bstrFileName += CComBSTR("../../../../../../../Data/ArcGIS_Engine_Developer_Guide/Gulf of St. Lawrence.mxd");
VARIANT_BOOL bResult;
m_ipPageLayoutControl->CheckMxFile(bstrFileName, &bResult);
if (VARIANT_TRUE == bResult)
m_ipPageLayoutControl->LoadMxFile(bstrFileName);
// Set buddy controls
m_ipTOCControl->SetBuddyControl(m_ipPageLayoutControl);
m_ipToolbarControl->SetBuddyControl(m_ipPageLayoutControl);
// Suppress drawing while resizing
m_ipMapControl->SuppressResizeDrawing(VARIANT_FALSE, (long)m_hWnd);
m_ipPageLayoutControl->SuppressResizeDrawing(VARIANT_FALSE, (long)m_hWnd);
return TRUE;
}
LRESULT CEngineControlsDlg::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
int dWidth, dHeight, dMargin;
// Get the dimensions of the client area of the main window
RECT rcClient;
GetClientRect(&rcClient);
int winHeight = rcClient.bottom - rcClient.top;
int winWidth = rcClient.right - rcClient.left;
// Set the margin size
RECT rcTOC;
::GetWindowRect(GetDlgItem(IDC_TOCCONTROL), &rcTOC);
POINT pTOC = { rcTOC.left, rcTOC.top };
ScreenToClient(&pTOC);
dMargin = pTOC.x;
// Resize the PageLayoutControl
RECT rcPageLayout;
::GetWindowRect(GetDlgItem(IDC_PAGELAYOUTCONTROL), &rcPageLayout);
POINT pPL = { rcPageLayout.left, rcPageLayout.top };
ScreenToClient(&pPL);
dWidth = winWidth - (rcTOC.right - rcTOC.left) - (dMargin * 2);
if (dWidth <= 0) dWidth = rcPageLayout.right - rcPageLayout.left;
dHeight = winHeight - pTOC.y - dMargin;
if (dHeight <= 0) dHeight = rcPageLayout.bottom - rcPageLayout.top;
::MoveWindow(GetDlgItem(IDC_PAGELAYOUTCONTROL), pPL.x, pPL.y, dWidth, dHeight, TRUE);
// Resize the MapControl
RECT rcMap;
::GetWindowRect(GetDlgItem(IDC_MAPCONTROL), &rcMap);
POINT pMap = { rcMap.left, rcMap.top };
ScreenToClient(&pMap);
dHeight = winHeight - pMap.y - dMargin;
if (dHeight <= 0) dHeight = rcMap.bottom - rcMap.top;
dWidth = rcMap.right - rcMap.left;
::MoveWindow(GetDlgItem(IDC_MAPCONTROL), pMap.x, pMap.y, dWidth, dHeight, TRUE);
return 0;
}
LRESULT CEngineControlsDlg::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// Stop listening to events
if (m_dwCustDlgCookie)
{
AtlUnadvise(m_ipCustomizeDialog, IID_ICustomizeDialogEvents, m_dwCustDlgCookie);
m_dwCustDlgCookie = 0;
}
if (m_dwDisplayCookie)
{
AtlUnadvise(m_ipCustomizeDialog, IID_IDisplayEvents, m_dwDisplayCookie);
m_dwDisplayCookie = 0;
}
AtlAdviseSinkMap(this, false);
return 0;
}
LRESULT CEngineControlsDlg::OnClose(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
EndDialog(0);
return 0;
}
LRESULT CEngineControlsDlg::OnCheckedCustomize(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// Show or hide the customize dialog
if (BST_CHECKED == IsDlgButtonChecked(IDC_CHECK_CUSTOMIZE))
m_ipCustomizeDialog->StartDialog((long)GetDlgItem(IDC_TOOLBARCONTROL));
else
m_ipCustomizeDialog->CloseDialog();
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// Event Handlers
HRESULT CEngineControlsDlg::OnAfterDraw(VARIANT display, long viewDrawPhase)
{
if (0 == m_ipEnvelope)
return S_OK;
// If the foreground phase has drawn
esriViewDrawPhase phase = (esriViewDrawPhase)viewDrawPhase;
if (esriViewForeground == phase)
{
VARIANT varSymbol;
varSymbol.vt = VT_UNKNOWN;
varSymbol.punkVal = m_ipFillSymbol;
// Draw the shape on the MapControl
m_ipMapControl->DrawShape(m_ipEnvelope, &varSymbol);
}
return S_OK;
}
HRESULT CEngineControlsDlg::OnMouseDown(long button, long shift, long x, long y, double pageX, double pageY)
{
// Popup the ToolbarMenu
if (2 == button)
m_ipToolbarMenu->PopupMenu(x, y, (long)GetDlgItem(IDC_PAGELAYOUTCONTROL));
return S_OK;
}
HRESULT CEngineControlsDlg::OnPageLayoutReplaced(VARIANT newMap)
{
// Get the IActiveView of the focus map in the PageLayoutControl
IActiveViewPtr ipActiveView;
m_ipPageLayoutControl->get_ActiveView(&ipActiveView);
IMapPtr ipMap;
ipActiveView->get_FocusMap(&ipMap);
IActiveViewPtr ipFocusActiveView = ipMap;
// Start listening for ITranformEvents; they will call 'this' object methods directly.
// If we're already listening from a previous document make sure to stop first.
IScreenDisplayPtr ipScreenDisplay;
ipFocusActiveView->get_ScreenDisplay(&ipScreenDisplay);
IDisplayTransformationPtr ipDisplayTransform;
ipScreenDisplay->get_DisplayTransformation(&ipDisplayTransform);
if (m_dwDisplayCookie)
{
AtlUnadvise(m_ipCustomizeDialog, IID_IDisplayEvents, m_dwDisplayCookie);
m_dwDisplayCookie = 0;
}
AtlAdvise(ipDisplayTransform, GetUnknown(), IID_ITransformEvents, &m_dwDisplayCookie);
// Get the extent of the focus map
ipFocusActiveView->get_Extent(&m_ipEnvelope);
// Load the same pre-authored map document into the MapControl
CComBSTR bstrFileName;
m_ipPageLayoutControl->get_DocumentFilename(&bstrFileName);
m_ipMapControl->LoadMxFile(bstrFileName);
// Set the extent of the MapControl to the full extent of the data
IEnvelopePtr ipFullExtent;
m_ipMapControl->get_FullExtent(&ipFullExtent);
m_ipMapControl->put_Extent(ipFullExtent);
return S_OK;
}
HRESULT CEngineControlsDlg::OnEndLabelEdit(long x, long y, BSTR newLabel, VARIANT_BOOL *CanEdit)
{
// If the new label is an empty string then prevent the edit
if (wcslen(newLabel) == 0)
*CanEdit = VARIANT_FALSE;
return S_OK;
}
// ICustomizeDialogEvents
STDMETHODIMP CEngineControlsDlg::OnStartDialog()
{
m_ipToolbarControl->put_Customize(VARIANT_TRUE);
return S_OK;
}
STDMETHODIMP CEngineControlsDlg::OnCloseDialog()
{
m_ipToolbarControl->put_Customize(VARIANT_FALSE);
CheckDlgButton(IDC_CHECK_CUSTOMIZE, BST_UNCHECKED);
return S_OK;
}
// ITransformEvents
STDMETHODIMP CEngineControlsDlg::BoundsUpdated(IDisplayTransformation *sender)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP CEngineControlsDlg::VisibleBoundsUpdated(IDisplayTransformation *sender, VARIANT_BOOL sizeChanged)
{
// Set the extent to the new visible extent
sender->get_VisibleBounds(&m_ipEnvelope);
// Refresh the MapControl's foreground phase
m_ipMapControl->Refresh(esriViewForeground);
return S_OK;
}
STDMETHODIMP CEngineControlsDlg::DeviceFrameUpdated(IDisplayTransformation *sender, VARIANT_BOOL sizeChanged)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP CEngineControlsDlg::ResolutionUpdated(IDisplayTransformation *sender)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP CEngineControlsDlg::RotationUpdated(IDisplayTransformation *sender)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP CEngineControlsDlg::UnitsUpdated(IDisplayTransformation *sender)
{
// Not implemented
return E_NOTIMPL;
}
/////////////////////////////////////////////////////////////////////////////
// Helper functions
void CEngineControlsDlg::CreateToolbarItems()
{
CComBSTR bstrProgID;
CComVariant varProgID;
varProgID.vt = VT_BSTR;
// Add generic commands
bstrProgID = _T("esriControlCommands.ControlsOpenDocCommand");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_FALSE, 0, esriCommandStyleIconOnly, 0);
// Add PageLayout navigation commands
bstrProgID = _T("esriControlCommands.ControlsPageZoomInTool");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_TRUE, 0, esriCommandStyleIconOnly, 0);
bstrProgID = _T("esriControlCommands.ControlsPageZoomOutTool");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_FALSE, 0, esriCommandStyleIconOnly, 0);
bstrProgID = _T("esriControlCommands.ControlsPagePanTool");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_FALSE, 0, esriCommandStyleIconOnly, 0);
bstrProgID = _T("esriControlCommands.ControlsPageZoomWholePageCommand");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_FALSE, 0, esriCommandStyleIconOnly, 0);
varProgID.bstrVal = _T("esriControlCommands.ControlsPageZoomPageToLastExtentBackCommand");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_FALSE, 0, esriCommandStyleIconOnly, 0);
bstrProgID = _T("esriControlCommands.ControlsPageZoomPageToLastExtentForwardCommand");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_FALSE, 0, esriCommandStyleIconOnly, 0);
// Add Map navigation commands
bstrProgID = _T("esriControlCommands.ControlsMapZoomInTool");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_TRUE, 0, esriCommandStyleIconOnly, 0);
bstrProgID = _T("esriControlCommands.ControlsMapZoomOutTool");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_FALSE, 0, esriCommandStyleIconOnly, 0);
bstrProgID = _T("esriControlCommands.ControlsMapPanTool");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_FALSE, 0, esriCommandStyleIconOnly, 0);
bstrProgID = _T("esriControlCommands.ControlsMapFullExtentCommand");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_FALSE, 0, esriCommandStyleIconOnly, 0);
// Add custom date tool
bstrProgID = _T("ControlCommands.AddDate");
varProgID.bstrVal = bstrProgID;
m_ipToolbarControl->AddItem(varProgID, -1, -1, VARIANT_TRUE, 0, esriCommandStyleIconAndText, 0);
// Create a new ToolbarMenu
m_ipToolbarMenu.CreateInstance(CLSID_ToolbarMenu);
// Share the ToolbarControl's command pool
ICommandPoolPtr ipCommandPool;
m_ipToolbarControl->get_CommandPool(&ipCommandPool);
m_ipToolbarMenu->putref_CommandPool(ipCommandPool);
// Add commands to the ToolbarMenu
bstrProgID = _T("esriControlCommands.ControlsPageZoomInFixedCommand");
varProgID.bstrVal = bstrProgID;
m_ipToolbarMenu->AddItem(varProgID, -1, -1, VARIANT_FALSE, esriCommandStyleIconAndText, 0);
bstrProgID = _T("esriControlCommands.ControlsPageZoomOutFixedCommand");
varProgID.bstrVal = bstrProgID;
m_ipToolbarMenu->AddItem(varProgID, -1, -1, VARIANT_FALSE, esriCommandStyleIconAndText, 0);
bstrProgID = _T("esriControlCommands.ControlsPageZoomWholePageCommand");
varProgID.bstrVal = bstrProgID;
m_ipToolbarMenu->AddItem(varProgID, -1, -1, VARIANT_FALSE, esriCommandStyleIconAndText, 0);
bstrProgID = _T("esriControlCommands.ControlsPageZoomPageToLastExtentBackCommand");
varProgID.bstrVal = bstrProgID;
m_ipToolbarMenu->AddItem(varProgID, -1, -1, VARIANT_TRUE, esriCommandStyleIconAndText, 0);
bstrProgID = _T("esriControlCommands.ControlsPageZoomPageToLastExtentForwardCommand");
varProgID.bstrVal = bstrProgID;
m_ipToolbarMenu->AddItem(varProgID, -1, -1, VARIANT_FALSE, esriCommandStyleIconAndText, 0);
// Set the hook to the PageLayoutControl
m_ipToolbarMenu->SetHook(m_ipPageLayoutControl);
}
void CEngineControlsDlg::CreateCustomizeDialog()
{
HRESULT hr = m_ipCustomizeDialog.CreateInstance(CLSID_CustomizeDialog);
if (FAILED(hr)) return;
// Set the title
CComBSTR bstrTitle;
bstrTitle.LoadString(IDS_CUSTOMIZETITLE);
m_ipCustomizeDialog->put_DialogTitle(bstrTitle);
// Show the 'Add from File' button
m_ipCustomizeDialog->put_ShowAddFromFile(VARIANT_TRUE);
// Set the ToolbarControl that new items will be added to
m_ipCustomizeDialog->SetDoubleClickDestination(m_ipToolbarControl);
// Start listening to customize dialog; it will call 'this' object methods directly
AtlAdvise(m_ipCustomizeDialog, GetUnknown(), IID_ICustomizeDialogEvents, &m_dwCustDlgCookie);
}
void CEngineControlsDlg::CreateOverviewSymbol()
{
// Get the IRGBColor interface
IRgbColorPtr ipColor(CLSID_RgbColor);
// Set the color properties
ipColor->put_RGB(RGB(255, 0, 0));
// Get the ILine symbol interface
ILineSymbolPtr ipOutline(CLSID_SimpleLineSymbol);
// Set the line symbol properties
ipOutline->put_Width(1.5);
ipOutline->put_Color(ipColor);
// Get the IFillSymbol interface
m_ipFillSymbol.CreateInstance(CLSID_SimpleFillSymbol);
// Set the fill symbol properties
m_ipFillSymbol->put_Outline(ipOutline);
m_ipFillSymbol->put_Style(esriSFSHollow);
}
void CEngineControlsDlg::GetCurrentDir(CComBSTR &bstrDirName)
{
TCHAR modName[256];
GetModuleFileName(_Module.m_hInst, modName, 256);
TCHAR drive[_MAX_DRIVE], dir[_MAX_DIR];
_tsplitpath(modName, drive, dir, 0, 0);
bstrDirName = drive;
bstrDirName += CComBSTR(dir);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -