⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplemapviewer.cpp

📁 VC和AO的实现图形浏览的功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	IDisplayTransformationPtr ipDisplayTransformation;
	hr = ipScreenDisplay->get_DisplayTransformation(&ipDisplayTransformation);
	if (FAILED(hr)) return hr;

	IPointPtr ipPoint;
	hr = ipDisplayTransformation->ToMapPoint(X, Y, &ipPoint);
	if (FAILED(hr)) return hr;

	if (g_currentTool == currentToolPan)
	{
		IEnvelopePtr ipEnv;
		hr = ipScreenDisplay->PanStop(&ipEnv);
		if (FAILED(hr)) return hr;

		hr = ipActiveView->put_Extent(ipEnv);
		if (FAILED(hr)) return hr;

		hr = ipActiveView->Refresh();
		if (FAILED(hr)) return hr;
	}
	else if (g_currentTool == currentToolZoomIn)
	{
		if (pDisplayFeedback)
		{
			IEnvelopePtr ipEnv;
			hr = pDisplayFeedback->Stop(&ipEnv);
			if (FAILED(hr)) return hr;

			hr = ipActiveView->put_Extent(ipEnv);
			if (FAILED(hr)) return hr;

			hr = ipActiveView->Refresh();
			if (FAILED(hr)) return hr;
		}
		else
		{
			IEnvelopePtr ipEnv;
			hr = ipActiveView->get_Extent(&ipEnv);
			if (FAILED(hr)) return hr;

			hr = ipEnv->Expand(0.5, 0.5, VARIANT_TRUE);
			if (FAILED(hr)) return hr;

			hr = ipActiveView->put_Extent(ipEnv);
			if (FAILED(hr)) return hr;

			hr = ipActiveView->Refresh();
			if (FAILED(hr)) return hr;
		}
	}
	else if (g_currentTool == currentToolZoomOut)
	{
		if (pDisplayFeedback)
		{
			IEnvelopePtr ipEnvelope;
			hr = pDisplayFeedback->Stop(&ipEnvelope);
	    pDisplayFeedback=0;
			if (FAILED(hr)) return hr;
		
			double dWidth, dHeight;
			hr = ipEnvelope->get_Width(&dWidth);
			if (FAILED(hr)) return hr;

			hr = ipEnvelope->get_Height(&dHeight);
			if (FAILED(hr)) return hr;

			if ((dHeight == 0) || (dWidth == 0))
				return S_OK;

			double dRubberWidth, dRubberHeight;
			double dRubberXMin, dRubberYMin, dRubberXMax, dRubberYMax;

			ipEnvelope->get_Width(&dRubberWidth);
			ipEnvelope->get_Height(&dRubberHeight);
			ipEnvelope->get_XMin(&dRubberXMin);
			ipEnvelope->get_YMin(&dRubberYMin);
			ipEnvelope->get_XMax(&dRubberXMax);
			ipEnvelope->get_YMax(&dRubberYMax);

			IEnvelopePtr ipExtentEnv;
			hr = ipActiveView->get_Extent(&ipExtentEnv);
			if (FAILED(hr)) return hr;

			double dExtentWidth, dExtentHeight;
			double dExtentXMin, dExtentYMin, dExtentXMax, dExtentYMax;
			ipExtentEnv->get_Width(&dExtentWidth);
			ipExtentEnv->get_Height(&dExtentHeight);
			ipExtentEnv->get_XMin(&dExtentXMin);
			ipExtentEnv->get_YMin(&dExtentYMin);
			ipExtentEnv->get_XMax(&dExtentXMax);
			ipExtentEnv->get_YMax(&dExtentYMax);

			double dNewWidth, dNewHeight;

			dNewWidth = dExtentWidth * ( dExtentWidth / dRubberWidth);
			dNewHeight = dExtentHeight * ( dExtentHeight / dRubberHeight);

			double dNewXMin, dNewYMin;

			dNewXMin = dExtentXMin - ((dRubberXMin - dExtentXMin) * (dExtentWidth / dRubberWidth));
			dNewYMin = dExtentYMin - ((dRubberYMin - dExtentYMin) * (dExtentHeight / dRubberHeight));

			IEnvelopePtr ipNewEnvelope(CLSID_Envelope);
			ipNewEnvelope->PutCoords(dNewXMin, dNewYMin, dNewXMin + dNewWidth, dNewYMin + dNewHeight);

			hr = ipActiveView->put_Extent(ipNewEnvelope);
			if (FAILED(hr)) return hr;

			hr = ipActiveView->Refresh();
			if (FAILED(hr)) return hr;
		}
		else
		{
			IEnvelopePtr ipEnv;
			hr = ipActiveView->get_Extent(&ipEnv);
			if (FAILED(hr)) return hr;

			hr = ipEnv->Expand(2.0, 2.0, VARIANT_TRUE);
			if (FAILED(hr)) return hr;

			hr = ipActiveView->put_Extent(ipEnv);
			if (FAILED(hr)) return hr;

			hr = ipActiveView->Refresh();
			if (FAILED(hr)) return hr;
		}
	}
	
	return S_OK;
}

HRESULT ToolEventMouseMove(HWND hWndPicBox, long X, long Y, IPoint* pPoint, INewEnvelopeFeedback **pDisplayFeedback)
{
	// Update the display feedback depending upon which tool is active
	IActiveViewPtr ipActiveView(ipMap);
	IScreenDisplayPtr ipScreenDisplay;
	HRESULT hr = ipActiveView->get_ScreenDisplay(&ipScreenDisplay);
	if (FAILED(hr)) return hr;

	IDisplayTransformationPtr ipDisplayTransformation;
	hr = ipScreenDisplay->get_DisplayTransformation(&ipDisplayTransformation);
	if (FAILED(hr)) return hr;

	IPointPtr ipPoint;
	hr = ipDisplayTransformation->ToMapPoint(X, Y, &ipPoint);
	if (FAILED(hr)) return hr;

	
	if (g_currentTool == currentToolPan)
	{
		hr = ipScreenDisplay->PanMoveTo(ipPoint);
		if (FAILED(hr)) return hr;
	}
	else
	{
		if (*pDisplayFeedback == 0)
		{
			IDisplayFeedbackPtr ipDisplayFeedback(CLSID_NewEnvelopeFeedback);
			
			hr = ((INewEnvelopeFeedbackPtr) ipDisplayFeedback)->putref_Display(ipScreenDisplay);
			if (FAILED(hr)) return hr;

			hr = ((INewEnvelopeFeedbackPtr) ipDisplayFeedback)->Start(pPoint);
			if (FAILED(hr)) return hr;

			*pDisplayFeedback = (INewEnvelopeFeedbackPtr) ipDisplayFeedback;
			if (*pDisplayFeedback)
				(*pDisplayFeedback)->AddRef();
		}
		else
		{
			INewEnvelopeFeedbackPtr ipFeedback(*pDisplayFeedback);
			hr = ipFeedback->MoveTo(ipPoint);
			if (FAILED(hr)) return hr;
		}
	}

	return S_OK;
}

HRESULT ZoomToFullExtent(IMap* pMap)
{
	// Get the Full extent information form the ActiveView and update the view
	// to display the data at its full extent
	IActiveViewPtr ipActiveView(pMap);

	IEnvelopePtr ipEnv;
	HRESULT hr = ipActiveView->get_FullExtent(&ipEnv);
	if (FAILED(hr)) return hr;

	hr = ipActiveView->put_Extent(ipEnv);
	if (FAILED(hr)) return hr;

	hr = ipActiveView->Refresh();
	if (FAILED(hr)) return hr;

	return S_OK;
}

// Implement the WindowProc for the Picture Box Drawing area
LRESULT CALLBACK DrawAreaProc(HWND hWndPicBox, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static IPointPtr ipPoint;
	static BOOL			 bToolInUse = false;
	static INewEnvelopeFeedback* pFeedback;

	switch (uMsg)
	{
		case WM_PAINT:
			{
				PAINTSTRUCT paintStruct;
				BeginPaint(hWndPicBox, &paintStruct);
				PaintDialog(paintStruct.hdc, ipMap, ipCancelTracker);
				EndPaint(hWndPicBox, &paintStruct);
			}
			break;
		case WM_LBUTTONDOWN:
			::SetCapture(hWndPicBox);
			ToolEventMouseDown(hWndPicBox, LOWORD(lParam), HIWORD(lParam), &ipPoint);
			bToolInUse = true;
			break;
		case WM_LBUTTONUP:
			::ReleaseCapture();
			bToolInUse = false;
			ToolEventMouseUp(hWndPicBox, LOWORD(lParam), HIWORD(lParam), pFeedback);
			ipPoint = 0;
			if (pFeedback)
			{
				pFeedback->Release();
				pFeedback = 0;
			}
			break;
		case WM_MOUSEMOVE:
			switch (g_currentTool)
			{
			case currentToolPan:
				::SetCursor(bToolInUse ? hCursorPanDrag :hCursorPan);
				break;
			case currentToolZoomIn:
				::SetCursor(bToolInUse ? hCursorZoomInMove : hCursorZoomIn);
				break;
			case currentToolZoomOut:
				::SetCursor(bToolInUse ? hCursorZoomOutMove : hCursorZoomOut);
				break;
			}
			if (bToolInUse)
				ToolEventMouseMove(hWndPicBox, LOWORD(lParam), HIWORD(lParam), ipPoint, &pFeedback);
			break;
	}

	return DefWindowProc(hWndPicBox, uMsg, wParam, lParam);
}

// Load in the cursors
void InitializeData(HINSTANCE hInstance)
{
	hCursorPan = ::LoadCursor(hInstance, MAKEINTRESOURCE(IDC_PANCURSOR));
	hCursorPanDrag = ::LoadCursor(hInstance, MAKEINTRESOURCE(IDC_PANDRAGCURSOR));
	hCursorZoomIn = ::LoadCursor(hInstance,  MAKEINTRESOURCE(IDC_ZOOMINCURSOR));
	hCursorZoomInMove = ::LoadCursor(hInstance,  MAKEINTRESOURCE(IDC_ZOOMINMOVECURSOR));
	hCursorZoomOut = ::LoadCursor(hInstance,  MAKEINTRESOURCE(IDC_ZOOMOUTCURSOR));
	hCursorZoomOutMove = ::LoadCursor(hInstance,  MAKEINTRESOURCE(IDC_ZOOMOUTMOVECURSOR));

}

// Delete all the cursors
void CleanUpData()
{
	::DeleteObject(hCursorPan);
	::DeleteObject(hCursorPanDrag);
	::DeleteObject(hCursorZoomIn);
	::DeleteObject(hCursorZoomInMove);
	::DeleteObject(hCursorZoomOut);
	::DeleteObject(hCursorZoomOutMove);
}


BOOL CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static HWND hWndPicBox;
	HRESULT hr;

	switch (uMsg)
	{
		case WM_INITDIALOG:
			hr = ipMap.CreateInstance(CLSID_Map);
			if (FAILED(hr))
			{
				MessageBox(hWnd, "Failed to Create a Map object", "Error", MB_OK | MB_ICONERROR);
			};
			ipCancelTracker.CreateInstance(CLSID_CancelTracker);

			{
				IActiveViewPtr ipActiveView(ipMap);
				hr = ipActiveView->put_ShowScrollBars(VARIANT_FALSE);
				if (FAILED(hr)) return hr;
				hWndPicBox = ::GetDlgItem(hWnd, IDC_MAPACTIVEVIEW);
				hr = ipActiveView->Activate((OLE_HANDLE) hWndPicBox);
				if (FAILED(hr)) return hr;

				InitializeData((HINSTANCE) ::GetWindowLong(hWndPicBox, GWL_HINSTANCE));

				::SetWindowLong(hWndPicBox, GWL_WNDPROC, (LPARAM) DrawAreaProc);
			}
			break;
		case WM_CLOSE:
			{
				DestroyWindow(hWnd);
				hWnd = NULL;
				PostQuitMessage(0);
			}
			break;
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
			case IDM_EXIT:
				::SendMessage(hWnd, WM_CLOSE, 0, 0);
				break;
			case IDM_ADDSHAPEFILE:
				AddShapeFiles(ipMap, hWnd);
				break;
			case IDM_ADDDATA:
				AddData(ipMap, hWnd);
				break;
			case IDC_ZOOMINBUTTON:
				::SetCursor(hCursorZoomIn);
				g_currentTool = currentToolZoomIn;
				break;
			case IDC_ZOOMOUTBUTTON:
				::SetCursor(hCursorZoomOut);
				g_currentTool = currentToolZoomOut;
				break;
			case IDC_PANBUTTON:
				::SetCursor(hCursorPan);
				g_currentTool = currentToolPan;
				break;
			case IDC_FULLEXTENTBUTTON:
				ZoomToFullExtent(ipMap);
				break;
			case IDM_REMOVEDATA:
				RemoveAllData(ipMap);
				break;
			}
	}

	return FALSE;
}

// WinMain function
// Initialize COM and create the dialog interface
// the implement the message loop
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{

	::CoInitialize(0);
	{
		HWND hWnd = ::CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAPDIALOG), 0, DialogProc);

		::ShowWindow(hWnd, nCmdShow);
		::UpdateWindow(hWnd);

		MSG msg;

		while (GetMessage(&msg, NULL, 0, 0))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	::CoUninitialize();

	return 0;
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -