翻译稿

发布时间 : 星期六 文章翻译稿更新完毕开始阅读

Getting Started with MapObjects Version 2.x in Visual C++ 33

Add a CheckBox to your search toolbar

A CheckBox will be used to control whether or not real-time data collection is simulated.

1. Open the CG_IDD_SEARCH (search toolbar) dialog using the resource editor.

2. Add a CheckBox control to the form. Specify it?s ID as IDC_EVENTCHECK. Give it the following

caption, “Collect Data”.

3. Add the following public method to CMainFrame. It allows the view to find out whether or not

data collection is enabled:

BOOL CMainFrame::IsDataCollectionEnabled() {

CButton* pCheck = (CButton*)m_wndSearch.GetDlgItem(IDC_EVENTCHECK); ASSERT_VALID(pCheck);

return (pCheck->GetCheck() == 1); }

Add a timer to the view window

Set a timer in the view window to trigger the movement of the events.

1. Add the following code to the start of the CEasyMapView::OnInitialUpdate methd:

void CEasyMapView::OnInitialUpdate() { CFormView::OnInitialUpdate(); // // Start the window timer // VERIFY(SetTimer(1, 1000, 0)); . . .

// 1 second timer

2. Use Class Wizard to add a DestroyWindow handler to CEasyMapView.

3. Implement CEasyMapView::DestroyWindow as follows:

BOOL CEasyMapView::DestroyWindow() { KillTimer(1); return CFormView::DestroyWindow();

}

4. Use Class Wizard to add a WM_TIMER hander to CEasyMapView.

5. Implement CEasyMapView::OnTimer as follows:

void CEasyMapView::OnTimer(UINT nIDEvent) { // // Find out if we're simulating real-time data collection // CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd(); ASSERT_VALID(pMainFrame); if (!pMainFrame->IsDataCollectionEnabled()) return; // // Offset all of the events by a random amount // CMoRectangle extent(m_map.GetExtent()); double maxDist = extent.GetWidth() / 20.0;

Getting Started with MapObjects Version 2.x in Visual C++

34

CMoTrackingLayer tLayer(m_map.GetTrackingLayer()); int eventCount = tLayer.GetEventCount();

CMoGeoEvent event;

for (int i = 0; i < eventCount; i++) {

event = tLayer.GetEvent(i);

double xOffset = maxDist * ((double)rand()/(double)RAND_MAX - 0.5); double yOffset = maxDist * ((double)rand()/(double)RAND_MAX - 0.5); event.Move(xOffset, yOffset); }

}

Test your changes

1. 2. 3. 4.

Build and run the application. Zoom into an area.

Click the event tool, then click in the map to add events.

Click the Data collection check box. Notice that the events start moving randomly on top of the map.

5. Click the check box again to stop the events.

Changing the GeoEvent symbol

We shall now make use of the CMoFont class that is defined in the MapHelper.cpp and MapHelper.h files. Be sure to include these two files into your project directory, if you haven?t done so already. The can be found in “…Samples\\MFC\\Common”.

1. In your Stdafx.h file, make sure that you add the following line:

#include “maphelper.h”

2. Click on the FileView tab in your Development Studio Workspace window. Highlight the root of

the project tree (EasyMap files) and right click to bring up the context menu. Select “Add files to project” and add the MapHelper.cpp and MapHelper.h files.

3. In CEasyMapView::OnInitialUpdate() append the following code:

// //

// Set the tracking layer symbol //

CMoTrackingLayer tLayer(m_map.GetTrackingLayer()); tLayer.SetSymbolCount(1);

CMoFont fnt;

fnt.InitializeFont(); // similar to CreateDispatch

// now define the font face

fnt.SetName(TEXT(\

CMoSymbol sym(tLayer.GetSymbol(0)); sym.SetColor(moBlack);

sym.SetStyle(moTrueTypeMarker);

sym.SetFont(fnt.GetFontDispatch()); sym.SetSize(18);

sym.SetCharacterIndex(81);

fnt.ReleaseFont();

4. Rebuild and execute to test your application. You should find that instead of the default GeoEvent

symbol you will have a small aircraft.

Getting Started with MapObjects Version 2.x in Visual C++ 35

Changing the mouse icon

Finally, to give the user additional visual feedback as to what tool they currently have selected, we can make use if the MapObjects mouse pointers. Using the Class Wizard, add a MouseMove event handler for IDC_MAP1. Call it OnMouseMoveMap1. Add the following code:

void CEasyMapView::OnMouseMoveMap1(short Button, short Shift, long X, long Y) {

switch (m_curTool) { case ID_MAP_ZOOMIN: m_map.SetMousePointer(moZoomIn); break; case ID_MAP_ZOOMOUT: m_map.SetMousePointer(moZoomOut); break;

case ID_MAP_PAN: m_map.SetMousePointer(moPan); break;

case ID_MAP_QUERY: m_map.SetMousePointer(moIdentify); break;

case ID_MAP_ADDEVENT: m_map.SetMousePointer(moDefault); break; } }

Getting Started with MapObjects Version 2.x in Visual C++ 36

Step Ten: Advanced Topics - Printing and Print Preview

For many applications, it?s essential that a map be printed. In this section, we?ll look at how to render maps to the printer and specifically how to draw them into a specified rectangle on the printer page. In addition, we?ll use the Print Preview support provided by MFC to allow our printed results to be previewed.

Add Helper Routines

If you have not already done so, add the MapHelper.cpp and MapHelper.h files to your project. The following routines make it easy to add print preview support to your application, and are included in the MapHelper files. The main function, FrameMap, is listed last. If you do not wish to add the Maphelper files, add these functions directly to EasyMapView.cpp.

//

// Perform precise integer division. Cast the result to the type of // integer that you need. //

#define DIV(a, b) (((a)*(b) < 0) ? (((double)(a) / (double)(b)) - 0.5) \\

: (((double)(a) / (double)(b)) + 0.5)) #define MULT(a, b) (((a)*(b) < 0) ? (((double)(a) * (double)(b)) - 0.5) \\

: (((double)(a) * (double)(b)) + 0.5)) #define MULDIV(a, b, c) (((a)*(b)*(c) < 0) ? \\

(((double)(a) * (double)(b) / (double)(c)) - 0.5) \\

: (((double)(a) * (double)(b) / (double)(c)) + 0.5)) double GetAspectRatio(CMap1& map) {

CMoRectangle r(map.GetExtent());

return (double)r.GetWidth() / (double)r.GetHeight(); }

static void S_AdjustWidthToRatio(CRect& r, const double ratio, const UINT align = DT_CENTER) {

int newWidth = (int)MULT(r.Height(), ratio); int offset;

if (align & DT_CENTER) offset = (int)DIV((double)r.Width() - newWidth, 2); else if (align & DT_RIGHT) offset = r.Width() - newWidth; else offset = 0; r.left += offset;

r.right = r.left + newWidth; }

static void S_AdjustHeightToRatio(CRect& r, const double ratio, const UINT align = DT_VCENTER) {

int newHeight = (int)DIV(r.Width(), ratio); int offset;

if (align & DT_VCENTER) offset = (int)DIV((double)r.Height() - newHeight, 2); else if (align & DT_BOTTOM) offset = r.Height() - newHeight; else offset = 0; r.top += offset;

r.bottom = r.top + newHeight; } //

// Adjust the rectangle to match the aspect ratio specified // Use the following values or'd together for alignment:

联系合同范文客服:xxxxx#qq.com(#替换为@)