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

📄 fgscaledraw.cpp

📁 用Visual c++来绘制地理学信息系统gis中的比例尺符号
💻 CPP
字号:
// FGScaleDraw.cpp: implementation of the CFGScaleDraw class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FGScale.h"
#include "FGScaleDraw.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFGScaleDraw::CFGScaleDraw()
{
	m_nMinX = m_nMinY = m_nMaxX = m_nMaxY = 0;
	m_ScaleType = ST_SINGLELINE;
}

CFGScaleDraw::~CFGScaleDraw()
{

}

void CFGScaleDraw::SetScale(float scale)
{
	m_fScale = scale;
}

void CFGScaleDraw::SetRect(CRect rect)
{
	m_nMinX = rect.left;
	m_nMaxX = rect.right;
	m_nMinY = rect.top;
	m_nMaxY = rect.bottom;
}

void CFGScaleDraw::SetRect(int minx, int miny, int maxx, int maxy)
{
	m_nMinX = minx;
	m_nMaxX = maxx;
	m_nMinY = miny;
	m_nMaxY = maxy;
}

void CFGScaleDraw::DrawScale(CDC* pdc)
{
	if(m_ScaleType == ST_SINGLELINE)
	{
		pdc->SetMapMode(MM_LOMETRIC);		//逻辑单位0.1毫米,x向右递增,y上递增

		CPoint p[2];
		p[0].x = m_nMinX;
		p[0].y = m_nMinY;
		p[1].x = m_nMaxX;
		p[1].y = m_nMaxY;
		pdc->DPtoLP(p, 2);
		/*pdc->MoveTo(p[0].x, p[0].y);
		pdc->LineTo(p[1].x, p[0].y);
		pdc->LineTo(p[1].x, p[1].y);
		pdc->LineTo(p[0].x, p[1].y);
		pdc->LineTo(p[0].x, p[0].y);*/

		int midy = (int)((p[0].y + p[1].y) * 0.5f);
		int midx[7];
		int dx = p[1].x - p[0].x;
		int dy = p[0].y - p[1].y;
		for(int i = 1; i < 8; i++)
		{
			midx[i - 1] = p[0].x + i * (int)(dx / 8.0f);
		}

		CPen tmppen(PS_SOLID, 10, RGB(255, 0, 0));
		CPen* poldpen = pdc->SelectObject(&tmppen);
		pdc->MoveTo(p[0].x, midy);
		pdc->LineTo(p[0].x, p[0].y);
		pdc->TextOut(p[0].x, p[0].y + 50, "0");

		pdc->MoveTo(p[0].x, midy);
		pdc->LineTo(p[1].x, midy);
		pdc->LineTo(p[1].x, p[0].y);
		CString mes;
		mes.Format("%.0f", m_fScale * dx * 0.0001f);
		pdc->TextOut(p[1].x - 30, p[0].y + 50, mes);

		CPen tmppen2(PS_SOLID, 5, RGB(255, 0, 0));
		pdc->SelectObject(&tmppen2);
		for(i = 0; i < 7; i++)
		{
			if(i != 3 && i != 1)
			{
				pdc->MoveTo(midx[i], midy);
				pdc->LineTo(midx[i], midy + (int)(dy * 0.25f));
			}
			else if(i == 1)
			{
				pdc->MoveTo(midx[i], midy);
				pdc->LineTo(midx[i], midy + (int)(dy * 0.25f));
				mes.Format("%.0f", m_fScale * dx * 0.25f * 0.0001f);
				pdc->TextOut(midx[i] - 30, p[0].y + 50, mes);
			}
			else if(i == 3)
			{
				pdc->MoveTo(midx[i], midy);
				pdc->LineTo(midx[i], p[0].y);
				mes.Format("%.0f", m_fScale * dx * 0.5f * 0.0001f);
				pdc->TextOut(midx[i] - 30, p[0].y + 50, mes);
			}
		}
		pdc->SelectObject(poldpen);
		
		pdc->SetMapMode(MM_TEXT);
	}//if(m_ScaleType == ST_SINGLELINE
	if(m_ScaleType == ST_SINGLELINE2)
	{
		pdc->SetMapMode(MM_LOMETRIC);		//逻辑单位0.1毫米,x向右递增,y上递增

		CPoint p[2];
		p[0].x = m_nMinX;
		p[0].y = m_nMinY;
		p[1].x = m_nMaxX;
		p[1].y = m_nMaxY;
		pdc->DPtoLP(p, 2);
		/*pdc->MoveTo(p[0].x, p[0].y);
		pdc->LineTo(p[1].x, p[0].y);
		pdc->LineTo(p[1].x, p[1].y);
		pdc->LineTo(p[0].x, p[1].y);
		pdc->LineTo(p[0].x, p[0].y);*/

		int midy = (int)((p[0].y + p[1].y) * 0.5f);
		int midx[7];
		int dx = p[1].x - p[0].x;
		int dy = p[0].y - p[1].y;
		for(int i = 1; i < 8; i++)
		{
			midx[i - 1] = p[0].x + i * (int)(dx / 8.0f);
		}

		CPen tmppen(PS_SOLID, 10, RGB(255, 0, 0));
		CPen* poldpen = pdc->SelectObject(&tmppen);
		pdc->MoveTo(p[0].x, p[1].y);
		pdc->LineTo(p[0].x, p[0].y);
		pdc->TextOut(p[0].x, p[0].y + 50, "0");

		pdc->MoveTo(p[0].x, midy);
		pdc->LineTo(p[1].x, midy);
		pdc->MoveTo(p[1].x, p[1].y);
		pdc->LineTo(p[1].x, p[0].y);
		CString mes;
		mes.Format("%.0f", m_fScale * dx * 0.0001f);
		pdc->TextOut(p[1].x - 30, p[0].y + 50, mes);

		CPen tmppen2(PS_SOLID, 5, RGB(255, 0, 0));
		pdc->SelectObject(&tmppen2);
		for(i = 0; i < 7; i++)
		{
			if(i != 3 && i != 1)
			{
				pdc->MoveTo(midx[i], midy - (int)(dy * 0.25f));
				pdc->LineTo(midx[i], midy + (int)(dy * 0.25f));
			}
			else if(i == 1)
			{
				pdc->MoveTo(midx[i], midy - (int)(dy * 0.25f));
				pdc->LineTo(midx[i], midy + (int)(dy * 0.25f));
				mes.Format("%.0f", m_fScale * dx * 0.25f * 0.0001f);
				pdc->TextOut(midx[i] - 30, p[0].y + 50, mes);
			}
			else if(i == 3)
			{
				pdc->MoveTo(midx[i], midy - (int)(dy * 0.25f));
				pdc->LineTo(midx[i], p[0].y);
				mes.Format("%.0f", m_fScale * dx * 0.5f * 0.0001f);
				pdc->TextOut(midx[i] - 30, p[0].y + 50, mes);
			}
		}
		pdc->SelectObject(poldpen);
		
		pdc->SetMapMode(MM_TEXT);
	}//if(m_ScaleType == ST_SINGLELINE2
	if(m_ScaleType == ST_SINGLELINE3)
	{
		pdc->SetMapMode(MM_LOMETRIC);		//逻辑单位0.1毫米,x向右递增,y上递增

		CPoint p[2];
		p[0].x = m_nMinX;
		p[0].y = m_nMinY;
		p[1].x = m_nMaxX;
		p[1].y = m_nMaxY;
		pdc->DPtoLP(p, 2);
		/*pdc->MoveTo(p[0].x, p[0].y);
		pdc->LineTo(p[1].x, p[0].y);
		pdc->LineTo(p[1].x, p[1].y);
		pdc->LineTo(p[0].x, p[1].y);
		pdc->LineTo(p[0].x, p[0].y);*/

		int midy = (int)((p[0].y + p[1].y) * 0.5f);
		int midx[7];
		int dx = p[1].x - p[0].x;
		int dy = p[0].y - p[1].y;
		for(int i = 1; i < 8; i++)
		{
			midx[i - 1] = p[0].x + i * (int)(dx / 8.0f);
		}

		CPen tmppen(PS_SOLID, 10, RGB(255, 0, 0));
		CPen* poldpen = pdc->SelectObject(&tmppen);
		pdc->MoveTo(p[0].x, midy);
		pdc->LineTo(p[0].x, p[1].y);
		pdc->TextOut(p[0].x, p[0].y + 50, "0");

		pdc->MoveTo(p[0].x, midy);
		pdc->LineTo(p[1].x, midy);
		pdc->LineTo(p[1].x, p[1].y);
		CString mes;
		mes.Format("%.0f", m_fScale * dx * 0.0001f);
		pdc->TextOut(p[1].x - 30, p[0].y + 50, mes);

		CPen tmppen2(PS_SOLID, 5, RGB(255, 0, 0));
		pdc->SelectObject(&tmppen2);
		for(i = 0; i < 7; i++)
		{
			if(i != 3 && i != 1)
			{
				pdc->MoveTo(midx[i], midy);
				pdc->LineTo(midx[i], midy - (int)(dy * 0.25f));
			}
			else if(i == 1)
			{
				pdc->MoveTo(midx[i], midy);
				pdc->LineTo(midx[i], midy - (int)(dy * 0.25f));
				mes.Format("%.0f", m_fScale * dx * 0.25f * 0.0001f);
				pdc->TextOut(midx[i] - 30, p[0].y + 50, mes);
			}
			else if(i == 3)
			{
				pdc->MoveTo(midx[i], midy);
				pdc->LineTo(midx[i], p[1].y);
				mes.Format("%.0f", m_fScale * dx * 0.5f * 0.0001f);
				pdc->TextOut(midx[i] - 30, p[0].y + 50, mes);
			}
		}
		pdc->SelectObject(poldpen);
		
		pdc->SetMapMode(MM_TEXT);
	}//if(m_ScaleType == ST_SINGLELINE3
	if(m_ScaleType == ST_FILLRECT)
	{
		pdc->SetMapMode(MM_LOMETRIC);		//逻辑单位0.1毫米,x向右递增,y上递增

		CPoint p[2];
		p[0].x = m_nMinX;
		p[0].y = m_nMinY;
		p[1].x = m_nMaxX;
		p[1].y = m_nMaxY;
		pdc->DPtoLP(p, 2);
		/*pdc->MoveTo(p[0].x, p[0].y);
		pdc->LineTo(p[1].x, p[0].y);
		pdc->LineTo(p[1].x, p[1].y);
		pdc->LineTo(p[0].x, p[1].y);
		pdc->LineTo(p[0].x, p[0].y);*/

		int midy = (int)((p[0].y + p[1].y) * 0.5f);
		int midx[15];
		int dx = p[1].x - p[0].x;
		int dy = p[0].y - p[1].y;
		for(int i = 1; i < 16; i++)
		{
			midx[i - 1] = p[0].x + i * (int)(dx / 16.0f);
		}

		pdc->MoveTo(p[0].x, midy + (int)(dy * 0.25));
		pdc->LineTo(p[1].x, midy + (int)(dy * 0.25));
		pdc->LineTo(p[1].x, midy - (int)(dy * 0.25));
		pdc->LineTo(p[0].x, midy - (int)(dy * 0.25));
		pdc->LineTo(p[0].x, midy + (int)(dy * 0.25));
		pdc->TextOut(p[0].x, p[0].y + 50, "0");
		CString mes;
		mes.Format("%.0f", m_fScale * dx * 0.0001f);
		pdc->TextOut(p[1].x - 30, p[0].y + 50, mes);
		mes.Format("%.0f", m_fScale * dx * 0.25f * 0.0001f);
		pdc->TextOut(midx[1] - 30, p[0].y + 50, mes);
		mes.Format("%.0f", m_fScale * dx * 0.5f * 0.0001f);
		pdc->TextOut(midx[7] - 30, p[0].y + 50, mes);

		CBrush tmpbrush(RGB(0, 0, 0));
		//CBrush* poldbrush = pdc->SelectObject(&tmpbrush);
		CRect tmprect;
		tmprect.left = p[0].x;
		tmprect.right = midx[0];
		tmprect.bottom = midy - (int)(dy * 0.25);
		tmprect.top = midy + (int)(dy * 0.25);
		pdc->FillRect(&tmprect, &tmpbrush);
		tmprect.left = midx[1];
		tmprect.right = midx[2];
		pdc->FillRect(&tmprect, &tmpbrush);
		tmprect.left = midx[3];
		tmprect.right = midx[7];
		pdc->FillRect(&tmprect, &tmpbrush);
		tmprect.left = midx[11];
		tmprect.right = p[1].x;
		pdc->FillRect(&tmprect, &tmpbrush);
		//pdc->SelectObject(poldbrush);
		
		pdc->SetMapMode(MM_TEXT);
	}//if(m_ScaleType == ST_FILLRECT
	if(m_ScaleType == ST_STEPPEDLINE)
	{
		pdc->SetMapMode(MM_LOMETRIC);		//逻辑单位0.1毫米,x向右递增,y上递增

		CPoint p[2];
		p[0].x = m_nMinX;
		p[0].y = m_nMinY;
		p[1].x = m_nMaxX;
		p[1].y = m_nMaxY;
		pdc->DPtoLP(p, 2);
		/*pdc->MoveTo(p[0].x, p[0].y);
		pdc->LineTo(p[1].x, p[0].y);
		pdc->LineTo(p[1].x, p[1].y);
		pdc->LineTo(p[0].x, p[1].y);
		pdc->LineTo(p[0].x, p[0].y);*/

		int midy = (int)((p[0].y + p[1].y) * 0.5f);
		int midx[15];
		int dx = p[1].x - p[0].x;
		int dy = p[0].y - p[1].y;
		for(int i = 1; i < 16; i++)
		{
			midx[i - 1] = p[0].x + i * (int)(dx / 16.0f);
		}

		pdc->TextOut(p[0].x, p[1].y, "0");
		CString mes;
		mes.Format("%.0f", m_fScale * dx * 0.0001f);
		pdc->TextOut(p[1].x - 30, p[1].y, mes);
		mes.Format("%.0f", m_fScale * dx * 0.25f * 0.0001f);
		pdc->TextOut(midx[1] - 30, p[1].y, mes);
		mes.Format("%.0f", m_fScale * dx * 0.5f * 0.0001f);
		pdc->TextOut(midx[7] - 30, p[1].y, mes);

		int y1, y2;
		y1 = midy - (int)(dy * 0.25);
		y2 = midy + (int)(dy * 0.25);
		pdc->MoveTo(p[0].x, y1);
		pdc->LineTo(p[0].x, y2);
		pdc->LineTo(midx[0], y2);
		pdc->LineTo(midx[0], y1);
		pdc->LineTo(midx[1], y1);
		pdc->LineTo(midx[1], y2);
		pdc->LineTo(midx[2], y2);
		pdc->LineTo(midx[2], y1);
		pdc->LineTo(midx[3], y1);
		pdc->LineTo(midx[3], y2);
		pdc->LineTo(midx[7], y2);
		pdc->LineTo(midx[7], y1);
		pdc->LineTo(midx[11], y1);
		pdc->LineTo(midx[11], y2);
		pdc->LineTo(p[1].x, y2);
		pdc->LineTo(p[1].x, y1);
		
		pdc->SetMapMode(MM_TEXT);
	}//if(m_ScaleType == ST_STEPPEDLINE
	if(m_ScaleType == ST_LINERECT)
	{
		pdc->SetMapMode(MM_LOMETRIC);		//逻辑单位0.1毫米,x向右递增,y上递增

		CPoint p[2];
		p[0].x = m_nMinX;
		p[0].y = m_nMinY;
		p[1].x = m_nMaxX;
		p[1].y = m_nMaxY;
		pdc->DPtoLP(p, 2);
		/*pdc->MoveTo(p[0].x, p[0].y);
		pdc->LineTo(p[1].x, p[0].y);
		pdc->LineTo(p[1].x, p[1].y);
		pdc->LineTo(p[0].x, p[1].y);
		pdc->LineTo(p[0].x, p[0].y);*/

		int midy = (int)((p[0].y + p[1].y) * 0.5f);
		int midx[15];
		int dx = p[1].x - p[0].x;
		int dy = p[0].y - p[1].y;
		for(int i = 1; i < 16; i++)
		{
			midx[i - 1] = p[0].x + i * (int)(dx / 16.0f);
		}

		pdc->MoveTo(p[0].x, midy + (int)(dy * 0.25));
		pdc->LineTo(p[1].x, midy + (int)(dy * 0.25));
		pdc->LineTo(p[1].x, midy - (int)(dy * 0.25));
		pdc->LineTo(p[0].x, midy - (int)(dy * 0.25));
		pdc->LineTo(p[0].x, midy + (int)(dy * 0.25));
		pdc->TextOut(p[0].x, p[0].y + 50, "0");
		CString mes;
		mes.Format("%.0f", m_fScale * dx * 0.0001f);
		pdc->TextOut(p[1].x - 30, p[0].y + 50, mes);
		mes.Format("%.0f", m_fScale * dx * 0.25f * 0.0001f);
		pdc->TextOut(midx[1] - 30, p[0].y + 50, mes);
		mes.Format("%.0f", m_fScale * dx * 0.5f * 0.0001f);
		pdc->TextOut(midx[7] - 30, p[0].y + 50, mes);

		CRect tmprect;
		tmprect.left = p[0].x;
		tmprect.right = midx[0];
		tmprect.bottom = midy - (int)(dy * 0.25);
		tmprect.top = midy + (int)(dy * 0.25);
		pdc->MoveTo(tmprect.left, tmprect.bottom);
		pdc->LineTo(tmprect.left, tmprect.top);
		pdc->MoveTo(tmprect.right, tmprect.bottom);
		pdc->LineTo(tmprect.right, tmprect.top);
		pdc->MoveTo(tmprect.left, midy);
		pdc->LineTo(tmprect.right, midy);
		tmprect.left = midx[1];
		tmprect.right = midx[2];
		pdc->MoveTo(tmprect.left, tmprect.bottom);
		pdc->LineTo(tmprect.left, tmprect.top);
		pdc->MoveTo(tmprect.right, tmprect.bottom);
		pdc->LineTo(tmprect.right, tmprect.top);
		pdc->MoveTo(tmprect.left, midy);
		pdc->LineTo(tmprect.right, midy);
		tmprect.left = midx[3];
		tmprect.right = midx[7];
		pdc->MoveTo(tmprect.left, tmprect.bottom);
		pdc->LineTo(tmprect.left, tmprect.top);
		pdc->MoveTo(tmprect.right, tmprect.bottom);
		pdc->LineTo(tmprect.right, tmprect.top);
		pdc->MoveTo(tmprect.left, midy);
		pdc->LineTo(tmprect.right, midy);
		tmprect.left = midx[11];
		tmprect.right = p[1].x;
		pdc->MoveTo(tmprect.left, tmprect.bottom);
		pdc->LineTo(tmprect.left, tmprect.top);
		pdc->MoveTo(tmprect.right, tmprect.bottom);
		pdc->LineTo(tmprect.right, tmprect.top);
		pdc->MoveTo(tmprect.left, midy);
		pdc->LineTo(tmprect.right, midy);
		
		pdc->SetMapMode(MM_TEXT);
	}//if(m_ScaleType == LINERECT
	if(m_ScaleType == ST_FILLRECT2)
	{
		pdc->SetMapMode(MM_LOMETRIC);		//逻辑单位0.1毫米,x向右递增,y上递增

		CPoint p[2];
		p[0].x = m_nMinX;
		p[0].y = m_nMinY;
		p[1].x = m_nMaxX;
		p[1].y = m_nMaxY;
		pdc->DPtoLP(p, 2);
		/*pdc->MoveTo(p[0].x, p[0].y);
		pdc->LineTo(p[1].x, p[0].y);
		pdc->LineTo(p[1].x, p[1].y);
		pdc->LineTo(p[0].x, p[1].y);
		pdc->LineTo(p[0].x, p[0].y);*/

		int midy = (int)((p[0].y + p[1].y) * 0.5f);
		int midx[15];
		int dx = p[1].x - p[0].x;
		int dy = p[0].y - p[1].y;
		for(int i = 1; i < 16; i++)
		{
			midx[i - 1] = p[0].x + i * (int)(dx / 16.0f);
		}

		pdc->MoveTo(p[0].x, midy + (int)(dy * 0.25));
		pdc->LineTo(p[1].x, midy + (int)(dy * 0.25));
		pdc->LineTo(p[1].x, midy - (int)(dy * 0.25));
		pdc->LineTo(p[0].x, midy - (int)(dy * 0.25));
		pdc->LineTo(p[0].x, midy + (int)(dy * 0.25));
		pdc->TextOut(p[0].x, p[0].y + 50, "0");
		CString mes;
		mes.Format("%.0f", m_fScale * dx * 0.0001f);
		pdc->TextOut(p[1].x - 30, p[0].y + 50, mes);
		mes.Format("%.0f", m_fScale * dx * 0.25f * 0.0001f);
		pdc->TextOut(midx[1] - 30, p[0].y + 50, mes);
		mes.Format("%.0f", m_fScale * dx * 0.5f * 0.0001f);
		pdc->TextOut(midx[7] - 30, p[0].y + 50, mes);

		CBrush tmpbrush(RGB(0, 0, 0));
		//CBrush* poldbrush = pdc->SelectObject(&tmpbrush);
		CRect tmprect;
		CRect tmprect2;
		tmprect.left = p[0].x;
		tmprect.right = midx[0];
		tmprect.bottom = midy;
		tmprect.top = midy + (int)(dy * 0.25);
		pdc->FillRect(&tmprect, &tmpbrush);
		tmprect2.left = midx[0];
		tmprect2.right = midx[1];
		tmprect2.bottom = midy - (int)(dy * 0.25);
		tmprect2.top = midy;
		pdc->FillRect(&tmprect2, &tmpbrush);
		tmprect.left = midx[1];
		tmprect.right = midx[2];
		pdc->FillRect(&tmprect, &tmpbrush);
		tmprect2.left = midx[2];
		tmprect2.right = midx[3];
		pdc->FillRect(&tmprect2, &tmpbrush);
		tmprect.left = midx[3];
		tmprect.right = midx[7];
		pdc->FillRect(&tmprect, &tmpbrush);
		tmprect2.left = midx[7];
		tmprect2.right = midx[11];
		pdc->FillRect(&tmprect2, &tmpbrush);
		tmprect.left = midx[11];
		tmprect.right = p[1].x;
		pdc->FillRect(&tmprect, &tmpbrush);
		//pdc->SelectObject(poldbrush);
		
		pdc->SetMapMode(MM_TEXT);
	}//if(m_ScaleType == ST_FILLRECT2
}

void CFGScaleDraw::SetScaleType(int type)
{
	m_ScaleType = (enum FGScaleType)type;
}

⌨️ 快捷键说明

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