You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

484 lines
14 KiB
C++

#include "StdAfx.h"
#include "Ruler.h"
#include "GlobalDrawMgr.h"
#include "GlobalFunction.h"
#include "Propertie.h"
#include "PropertieMgr.h"
#include "WorkAreaMgr.h"
#define RULER_WIDETH (22) //标尺的宽度--(单位:像素)随view比例变化
#define RULER_GAP (1) //标尺的间隔--(单位:毫米)
#define RULER_NUM_WIDTH (6) //标尺数字的宽度--(单位:像素)随view比例变化
#define RULER_NUM_HEIGHT (10) //标尺数字的高度--(单位:像素)随view比例变化
CRuler *gRuler = new CRuler;
CRuler::CRuler(void)
{
m_bDrawRuler = true;//是否绘制标尺
m_MousePointTmp.x = -1;//记录鼠标的临时位置
m_MousePointTmp.y = -1;
}
CRuler::~CRuler(void)
{
}
CMFCPropertyGridProperty *CRuler::CreatGridProperty()
{
CString PropertyName;//属性名称
CString Description;//描述
CString Path = _T("LogMgr");;//存储路径
CString Name;
//-------------------------------------------------------------------------------//
PropertyName = _T("标尺");
CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(PropertyName);
//-------------------------------------------------------------------------------//
{
//添加属性变量映射
Name = _T("m_bDrawRuler");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_bDrawRuler);
pPropertie->SetType(_PROP_TYPE_BOOL);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("显示标尺");
Description = _T("是否显示辅助标尺(最小单位: mm)");
CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_bDrawRuler, Description);
pGroup->AddSubItem(p1);
gDevicePropertieMgr.Insert(p1, pPropertie);
}
//-------------------------------------------------------------------------------//
return pGroup;
}
void CRuler::OnPropertyChanged()
{
//获取当前视类指针
CLaiPuLaserView *pView = GetCurViewPtr();
if(pView)
{
pView->RefreshView();
}
}
void CRuler::Draw(CDC* pDC)
{
DrawRuler(pDC);
}
//绘制标尺
void CRuler::DrawRuler(CDC* pDC)
{
if(!m_bDrawRuler)
return;
//获取客户区范围
CRect rect;
GetCurViewPtr()->GetClientRect(&rect);//获取窗口客户区的坐标
pDC->DPtoLP(rect);//转换为逻辑坐标
//绘制
int RulerWideth = GetCurViewPtr()->GetCurrScaleVal(RULER_WIDETH);
//标尺线的长度
int ShortLen = RulerWideth/4;
int ShortLen2 = RulerWideth/6;
int MidLen = RulerWideth/2;
int LongLen;
double zoomScale = GetCurViewPtr()->GetCurZoomScale();
if(zoomScale>VIEW_SCALE9)
{
LongLen = RulerWideth/2;
}
else//缩的太小的时候变短点
{
LongLen = RulerWideth/3;
}
CPen pen;
pen.CreatePen(PS_SOLID,0.5,RGB_BLACK);
CBrush brush ;
brush.CreateSolidBrush(RGB_GRAY) ;
CPen* pOldPen = (CPen*)pDC->SelectObject(&pen);
CBrush* pOldBrush = (CBrush*)pDC->SelectObject(&brush);
int RulerX,RulerY;
RulerX = rect.left+RulerWideth;
RulerY = rect.top+RulerWideth;
//水平标尺(背景)
CRect DrawRectH(0,rect.top,SCROLL_SIZE_CX,RulerY);
pDC->Rectangle(DrawRectH);
//垂直标尺(背景)
CRect DrawRectV(rect.left,0,RulerX,SCROLL_SIZE_CY);
pDC->Rectangle(DrawRectV);
int count;//计数
int StartX = LOGIC_CENTER_X;
int StartY = LOGIC_CENTER_Y;
int gap;//间隔
gap = gDraw->Double2Int(1);//每10 毫米
pDC->SetBkMode(TRANSPARENT);//背景透明
CString FontTypeStr("华文中宋");
double TextHeight = GetCurViewPtr()->GetCurrScaleVal(RULER_NUM_HEIGHT);//高度
double TextWidth = GetCurViewPtr()->GetCurrScaleVal(RULER_NUM_WIDTH);//宽度
//绘制水平方向数字--------------------------------------------------------
//设置字体------------------------------------------------------
SetCurFont(pDC,TextHeight,TextWidth,0,FALSE,FontTypeStr);
DbSize size = gWorkAreaMgr->GetWorkArea().GetSize();
int WorkSpaceWeidth = size.w;
int WorkSpaceHeight = size.h;
double Scale1 = VIEW_SCALE1;
double Scale2 = VIEW_SCALE1;
double Scale3 = VIEW_SCALE1;
#if 1//正方向
count = 0;//计数清零
for(int i=0;i<=(WorkSpaceWeidth);i += gap)
{
//在工作空间范围才显示
if(zoomScale>Scale1)//全部显示
{
DrawRulerNum(pDC,count*10,StartX+i,RulerY-RulerWideth,TextWidth,TRUE);
}
else if(zoomScale>Scale2)//每20 毫米显示
{
if(count%2 == 0)
{
DrawRulerNum(pDC,count*10,StartX+i,RulerY-RulerWideth,TextWidth,TRUE);
}
}
else if(zoomScale>Scale3)//每50 毫米显示
{
if(count%5 == 0)
{
DrawRulerNum(pDC,count*10,StartX+i,RulerY-RulerWideth,TextWidth,TRUE);
}
}
else//每200 毫米显示
{
if(count%20 == 0)
{
DrawRulerNum(pDC,count*10,StartX+i,RulerY-RulerWideth,TextWidth,TRUE);
}
}
count++;
}
#endif
#if 1//负方向
count = 0;//计数清零
for(int i=0;i<=(WorkSpaceWeidth);i += gap)
{
if(count !=0)
{
//在工作空间范围才显示
if(zoomScale>Scale1)//全部显示
{
DrawRulerNum(pDC,count*10,StartX-i,RulerY-RulerWideth,TextWidth,TRUE);
}
else if(zoomScale>Scale2)//每20 毫米显示
{
if(count%2 == 0)
{
DrawRulerNum(pDC,count*10,StartX-i,RulerY-RulerWideth,TextWidth,TRUE);
}
}
else if(zoomScale>Scale3)//每50 毫米显示
{
if(count%5 == 0)
{
DrawRulerNum(pDC,count*10,StartX-i,RulerY-RulerWideth,TextWidth,TRUE);
}
}
else//每200 毫米显示
{
if(count%20 == 0)
{
DrawRulerNum(pDC,count*10,StartX-i,RulerY-RulerWideth,TextWidth,TRUE);
}
}
}
count++;
}
#endif
//绘制垂直方向数字--------------------------------------------------------
//设置字体------------------------------------------------------
SetCurFont(pDC,TextHeight,TextWidth,90,FALSE,FontTypeStr);
count = 0;//计数清零
//正方向
for(int i=0;i<=(WorkSpaceWeidth);i += gap)
{
//在工作空间范围才显示
if(zoomScale>Scale1)//全部显示
{
DrawRulerNum(pDC,count*10,RulerX-RulerWideth,StartY-i,TextWidth,FALSE);
}
else if(zoomScale>Scale2)//每20 毫米显示
{
if(count%2 == 0)
{
DrawRulerNum(pDC,count*10,RulerX-RulerWideth,StartY-i,TextWidth,FALSE);
}
}
else if(zoomScale>Scale3)//每50 毫米显示
{
if(count%5 == 0)
{
DrawRulerNum(pDC,count*10,RulerX-RulerWideth,StartY-i,TextWidth,FALSE);
}
}
else//每200 毫米显示
{
if(count%20 == 0)
{
DrawRulerNum(pDC,count*10,RulerX-RulerWideth,StartY-i,TextWidth,FALSE);
}
}
count++;
}
//负方向
count = 0;//计数清零
for(int i=0;i<=(WorkSpaceWeidth);i += gap)
{
if(count != 0)
{
//在工作空间范围才显示
if(zoomScale>Scale1)//全部显示
{
DrawRulerNum(pDC,count*10,RulerX-RulerWideth,StartY+i,TextWidth,FALSE);
}
else if(zoomScale>Scale2)//每20 毫米显示
{
if(count%2 == 0)
{
DrawRulerNum(pDC,count*10,RulerX-RulerWideth,StartY+i,TextWidth,FALSE);
}
}
else if(zoomScale>Scale3)//每50 毫米显示
{
if(count%5 == 0)
{
DrawRulerNum(pDC,count*10,RulerX-RulerWideth,StartY+i,TextWidth,FALSE);
}
}
else//每200 毫米显示
{
if(count%20 == 0)
{
DrawRulerNum(pDC,count*10,RulerX-RulerWideth,StartY+i,TextWidth,FALSE);
}
}
}
count++;
}
//水平标尺的横线
pDC->MoveTo(0,RulerY);
pDC->LineTo(SCROLL_SIZE_CX,RulerY);
//垂直标尺的竖线
pDC->MoveTo(RulerX,0);
pDC->LineTo(RulerX,SCROLL_SIZE_CY);
//画线----------------------------------------------------------
count = 0;//计数清零
gap = gDraw->Double2Int(RULER_GAP);
for(int i=0;i<(SCROLL_SIZE_CX/2);i += gap)
{
if(count%10 == 0)//每10 毫米画最长的
{
if(zoomScale>VIEW_SCALE3)//小于这个比例不再显示
{
//水平标尺
pDC->MoveTo(StartX+i,RulerY);
pDC->LineTo(StartX+i,RulerY-LongLen);
pDC->MoveTo(StartX-i,RulerY);
pDC->LineTo(StartX-i,RulerY-LongLen);
//垂直标尺
pDC->MoveTo(RulerX,StartY+i);
pDC->LineTo(RulerX-LongLen,StartY+i);
pDC->MoveTo(RulerX,StartY-i);
pDC->LineTo(RulerX-LongLen,StartY-i);
}
}
else if(count%5 == 0)//每5 毫米画中等的
{
if(zoomScale>VIEW_SCALE7)//小于这个比例不再显示
{
//水平标尺
pDC->MoveTo(StartX+i,RulerY);
pDC->LineTo(StartX+i,RulerY-MidLen);
pDC->MoveTo(StartX-i,RulerY);
pDC->LineTo(StartX-i,RulerY-MidLen);
//垂直标尺
pDC->MoveTo(RulerX,StartY+i);
pDC->LineTo(RulerX-MidLen,StartY+i);
pDC->MoveTo(RulerX,StartY-i);
pDC->LineTo(RulerX-MidLen,StartY-i);
}
}
else//每1 毫米
{
if(zoomScale>VIEW_SCALE12)//小于这个比例不再显示
{
//水平标尺
pDC->MoveTo(StartX+i,RulerY);
pDC->LineTo(StartX+i,RulerY-ShortLen);
pDC->MoveTo(StartX-i,RulerY);
pDC->LineTo(StartX-i,RulerY-ShortLen);
//垂直标尺
pDC->MoveTo(RulerX,StartY+i);
pDC->LineTo(RulerX-ShortLen,StartY+i);
pDC->MoveTo(RulerX,StartY-i);
pDC->LineTo(RulerX-ShortLen,StartY-i);
}
}
count++;
}
if(zoomScale>VIEW_SCALE20)//放大到这个比例才显示0.1 毫米间隔
{
gap /= 10;//间隔为0.1 毫米
//只在工作空间的范围显示
int max = max((int)WorkSpaceHeight,(int)WorkSpaceWeidth);
for(int i=0;i<(max);i += gap)
{
//水平标尺
pDC->MoveTo(StartX+i,RulerY);
pDC->LineTo(StartX+i,RulerY-ShortLen2);
pDC->MoveTo(StartX-i,RulerY);
pDC->LineTo(StartX-i,RulerY-ShortLen2);
//垂直标尺
pDC->MoveTo(RulerX,StartY+i);
pDC->LineTo(RulerX-ShortLen2,StartY+i);
pDC->MoveTo(RulerX,StartY-i);
pDC->LineTo(RulerX-ShortLen2,StartY-i);
}
}
//清除交叉的部分
CRect DrawRect(rect.left,rect.top,rect.left+RulerWideth,rect.top+RulerWideth);
pDC->Rectangle(DrawRect);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
m_MousePointTmp.x = -1;
m_MousePointTmp.y = -1;
}
//绘制标尺的数字,bIsX == TRUE为X 方向
void CRuler::DrawRulerNum(CDC* pDC,int Num,int CurX,int CurY,double TextWidth,BOOL bIsX)
{
//计算Num 有几位
int NumTmp = Num;
int cnt = 0;
while(NumTmp>0)
{
NumTmp /=10;
cnt++;
}
if(cnt==0)//只有一位时
{
cnt = 1;
}
if(bIsX)
{
CurX -= (cnt*TextWidth)/2;//水平方向往左边偏移一点
}
else
{
CurY += (cnt*TextWidth)/2;//垂直方向往下边偏移一点
}
CString TextString;
TextString.Format("%d",Num);
const char* pStr=(const char*)TextString;
int StrLen = strlen(pStr);
while(StrLen>0)
{
unsigned char char1;
char Char[3];
char1 = *pStr;
strncpy(Char,pStr,1);
Char[1]='\0';
pStr += 1;
StrLen -= 1;
pDC->TextOut(CurX,CurY,Char);
//计算下一个的起始点
if(bIsX)
{
CurX += TextWidth;
}
else
{
CurY -= TextWidth;
}
}
}
//设置当前字体
CFont* CRuler::SetCurFont(CDC *pDC,double TextHeight,double TextWidth,double RotateAngle,bool bIsItalic,CString FontTypeStr)
{
//使用当前图形的字体
CFont font,*pOldFont;
font.CreateFont (
TextHeight, //高
0,//TextWidth, //宽
RotateAngle*10, //角度
0,
FW_NORMAL,
bIsItalic, //斜体
0,
0,
DEFAULT_CHARSET, //字符集
OUT_CHARACTER_PRECIS,
CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH |FF_DONTCARE,
FontTypeStr);//字体
pOldFont = pDC->SelectObject(&font);
return pOldFont;
}
//绘制鼠标的位置
void CRuler::DrawMousePos(CDC* pDC,CPoint point)
{
if(!m_bDrawRuler)
return;
double zoomScale = GetCurViewPtr()->GetCurZoomScale();
int RulerWideth = RULER_WIDETH/zoomScale;
int CheckGap = RulerWideth/2;//检测偏移量
//获取客户区范围
CRect rect;
GetCurViewPtr()->GetClientRect(&rect);//获取窗口客户区的坐标
pDC->DPtoLP(rect);//转换为逻辑坐标
//清除先前留下的,重绘标尺区域
if(m_MousePointTmp.x == -1)
{
DrawRuler(pDC);
}
int old_rop = pDC->SetROP2(R2_XORPEN);
CPen pen;
pen.CreatePen(PS_SOLID,0.5,RGB_GRAY);
CPen* pOldPen = (CPen*)pDC->SelectObject(&pen);
int RulerX,RulerY;
RulerX = rect.left+RulerWideth;
RulerY = rect.top+RulerWideth;
RulerX -= 1;//误差
RulerY -= 1;//误差
//水平
pDC->MoveTo(m_MousePointTmp.x,RulerY);
pDC->LineTo(m_MousePointTmp.x,RulerY-RulerWideth);
pDC->MoveTo(RulerX,m_MousePointTmp.y);
pDC->LineTo(RulerX-RulerWideth,m_MousePointTmp.y);
//垂直
pDC->MoveTo(point.x,RulerY);
pDC->LineTo(point.x,RulerY-RulerWideth);
pDC->MoveTo(RulerX,point.y);
pDC->LineTo(RulerX-RulerWideth,point.y);
pDC->SelectObject(pOldPen);
pDC->SetROP2(old_rop);
}