📄 mt.cpp
字号:
// MT.cpp
#include "stdafx.h"
#include "mt.h"
#include "math.h"
bool MT::Open(char *fn)
{
CleanUp();
FILE *fp = NULL;
m_itemhead = new item;
item *next = NULL,*last = NULL;
// Init Header
m_itemhead->last = NULL;
m_itemhead->next = NULL;
m_itemhead->x0 = .0;
m_itemhead->y0 = .0;
m_itemhead->x1 = .0;
m_itemhead->y1 = .0;
m_itemhead->type = ACTION_NOTHING;
memset(m_itemhead->word,0,MAX_TEXT_LENGTH);
// Open Data File
fp = fopen(fn,"rb");
if (fp != NULL)
{
for(last = m_itemhead;;)
{
next = new item;
if (fread(next,RECORD_LENGTH,1,fp))
{
last->next = next;
next->last = last;
next->next = NULL;
last = next;
}
else
{
delete next;
next = NULL;
break;
}
}
fclose(fp);
}
filename = _T(fn);
return true;
}
MT::MT()
{
m_itemhead = new item;
m_itemhead->next = NULL;
m_itemhead->last = NULL;
m_itemhead->x0 = .0;
m_itemhead->y0 = .0;
m_itemhead->x1 = .0;
m_itemhead->y1 = .0;
m_itemhead->type = ACTION_NOTHING;
memset(m_itemhead->word,0,MAX_TEXT_LENGTH);
changed=false;
}
MT::~MT()
{
CleanUp();
}
bool MT::CleanUp()
{
item *p,*next;
for (p = m_itemhead;p != NULL;)
{
next = p->next;
delete p;
p = next;
}
m_itemhead = NULL;
return true;
}
bool MT::GetArrow(CPoint start, CPoint end, CPoint &pa, CPoint &pb)
{
int l2,dx,dy;
double angle;
dx = end.x - start.x;
dy = end.y - start.y;
l2 = dx * dx + dy * dy;
angle = -atan2(dy,dx);
if (angle < 0) angle = 2 * PI + angle;
pa.x = long(start.x + ARROW_LENGTH * cos(angle + ARROW_ANGLE));
pa.y = long(start.y - ARROW_LENGTH * sin(angle + ARROW_ANGLE));
pb.x = long(start.x + ARROW_LENGTH * cos(angle - ARROW_ANGLE));
pb.y = long(start.y - ARROW_LENGTH * sin(angle - ARROW_ANGLE));
return true;
}
bool MT::ReDraw(CDC *dc,long left,long top,double width,double height)
{
/* -> DEBUGING
return true;
static _left = -1, _top = -1, _width = -1, _height = -1;
if (_left > 0 && _top > 0 && _width > 0 && _height > 0 &&
_left != left && _top != top && _width != width && _height != height)
{
_left = _top = _width = _height = -1;
}
_left = left;
_top = top;
_width = width;
_height = height;
*/
item *p = NULL;
CPoint start,end,pointa,pointb;
CPen pen,*oldpen;
pen.CreatePen(PS_SOLID,1,RGB(255,0,0));
oldpen=dc->SelectObject(&pen);
dc->SetTextColor(RGB(255,0,0));
dc->SetBkMode(TRANSPARENT);
for (p = m_itemhead->next; p != NULL; p = p->next)
{
start.x = long(p->x0 * width) + left;
start.y = long(p->y0 * height) + top;
end.x = long(p->x1 * width) + left;
end.y = long(p->y1 * height) + top;
switch (p->type)
{
case ACTION_ARROW:
dc->MoveTo(start);
dc->LineTo(end);
GetArrow(start,end,pointa,pointb);
dc->MoveTo(start);
dc->LineTo(pointa);
dc->MoveTo(start);
dc->LineTo(pointb);
break;
case ACTION_CROSS:
dc->MoveTo(start.x,start.y - CROSS_LENGTH);
dc->LineTo(start.x,start.y + CROSS_LENGTH + 1);
dc->MoveTo(start.x - CROSS_LENGTH,start.y);
dc->LineTo(start.x + CROSS_LENGTH + 1,start.y);
break;
case ACTION_TEXT:
dc->SetBkMode(TRANSPARENT);
dc->TextOut(start.x,start.y,p->word);
break;
}
}
dc->SelectObject(oldpen);
DeleteObject(&pen);
return true;
}
void MT::AddNode(item *nitem)
{
item *last = NULL,*node = NULL;
for (last = m_itemhead; last->next != NULL; last = last->next);
node = new item;
memcpy(node,nitem,sizeof(item));
last->next = node;
node->last = last;
node->next = NULL;
changed =true;
/* 与其插在最后,不如插在开始。
item *node = new item;
memcpy(node,nitem,sizeof(item));
if (m_itemhead->next != NULL)
m_itemhead->next->last = node;
node->next = m_itemhead->next;
node->last = m_itemhead;
m_itemhead->next = node;
changed = true;
*/
}
void MT::DelNode(item *nitem)
{
nitem->last->next = nitem->next;
if (nitem->next != NULL)
nitem->next->last = nitem->last;
changed =true;
}
void MT::Save(char *fn)
{
if (changed == false) return;
FILE *fp;
item *p;
if (fn != NULL) filename = _T(fn);
fp = fopen(filename.GetBuffer(200),"wb");
if(fp == NULL) return;
for(p = m_itemhead; p != NULL; p = p->next)
fwrite(p,sizeof(item),1,fp);
fclose(fp);
}
void MEAS::BeginRecord()
{
item *node = new item;
node->type = TYPE_START;
AddNode(node);
delete node;
}
void MEAS::EndRecord()
{
item *node = new item;
node->type = TYPE_END;
AddNode(node);
delete node;
}
void MEAS::InsertLine(float x0,float y0,float x1,float y1)
{
// if (TYPE_START != m_itemhead->last->type &&
// TYPE_LINE != m_itemhead->last->type)
// BeginRecord();
item *node = new item;
node->type = TYPE_LINE;
node->x0 = x0;
node->x1 = x1;
node->y0 = y0;
node->y1 = y1;
AddNode(node);
delete node;
}
void MEAS::InsertArc(float x0, float y0, float x2, float y2, float x3, float y3)
{
item *node = new item;
node->type = TYPE_ARC;
node->x0 = x0;
node->y0 = y0;
node->u_double[0] = x2;
node->u_double[1] = y2;
node->u_double[2] = x3;
node->u_double[3] = y3;
AddNode(node);
delete node;
}
BOOL MEAS::SaveFile(char *argPathName)
{
item *p = NULL;
FILE *fp = NULL;
if (NULL != argPathName) m_FilePathName = argPathName;
fp = fopen(m_FilePathName,"wb");
if (NULL == fp)
return FALSE;
for (p = m_itemhead->next; NULL != p; p = p->next)
fwrite(p,sizeof(item),1,fp);
fclose(fp);
// m_Changed = FALSE;
return TRUE;
}
// 从文件中读取记录数据
BOOL MEAS::LoadFile(char *argPathName)
{
item *next = NULL,*last = NULL;
m_FilePathName = argPathName;
// 首先释放曾经分配的资源
CleanUp();
// Open Data File
FILE *fp = fopen(argPathName,"rb");
if (NULL == fp) return FALSE;
for(last = m_itemhead;;)
{
next = new item;
if (fread(next,RECORD_LENGTH,1,fp))
{
last->next = next;
next->last = last;
next->next = NULL;
m_itemhead->last = last = next;
}
else
{
delete next;
next = NULL;
break;
}
}
fclose(fp);
// m_Changed = FALSE;
return TRUE;
}
/*
构造函数进行初始化
*/
MEAS::MEAS()
{
SetScale(0,0,400,300);
m_itemhead = new item;
m_itemhead->last = m_itemhead;
m_itemhead->next = NULL;
m_itemhead->x0 = .0;
m_itemhead->y0 = .0;
m_itemhead->x1 = .0;
m_itemhead->y1 = .0;
m_itemhead->type = TYPE_NOTHING;
memset(m_itemhead->word,0,MAX_TEXT_LENGTH);
m_selitem = NULL;
m_lighton = FALSE;
// m_Changed = FALSE;
}
/*
析构函数释放资源
*/
MEAS::~MEAS()
{
CleanUp();
delete m_itemhead;
m_itemhead = NULL;
}
/*
释放分配的资源,不包括头记录
*/
void MEAS::CleanUp()
{
item *p,*next;
for (p = m_itemhead->next; NULL != p;)
{
next = p->next;
delete p;
p = next;
}
m_itemhead->next = NULL;
m_itemhead->last = m_itemhead;
// m_Changed = FALSE;
}
BOOL MEAS::AddNode(item *node)
{
item *thenode = new item;
memcpy(thenode,node,sizeof(item));
thenode->last = m_itemhead->last;
thenode->next = NULL;
m_itemhead->last->next = thenode;
m_itemhead->last = thenode;
// m_itemhead->last->next = thenode;
// thenode->last = m_itemhead->last;
// thenode->next = NULL;
// m_itemhead->last = thenode;
// m_Changed = TRUE;
SaveFile();
return TRUE;
}
BOOL MEAS::ReDraw(CDC *dc)
{
CPoint start,end,pointa,pointb;
CPen pen,*oldpen;
pen.CreatePen(PS_SOLID,1,RGB(0,255,0));
oldpen = dc->SelectObject(&pen);
for (item *p = m_itemhead->next; NULL != p; p = p->next)
{
if (TYPE_LINE == p->type)
{
start.x = long(p->x0 * m_width) + m_left;
start.y = long(p->y0 * m_height) + m_top;
end.x = long(p->x1 * m_width) + m_left;
end.y = long(p->y1 * m_height) + m_top;
dc->MoveTo(start);
dc->LineTo(end);
continue;
}
if (TYPE_ARC == p->type)
{
int x1, y1, x2, y2, x3, y3, x4, y4;
x1 = long(p->x0 * m_width) + m_left - 10;
y1 = long(p->y0 * m_height) + m_top - 10;
x2 = x1 + 20;
y2 = y1 + 20;
x3 = long(p->u_double[0] * m_width) + m_left;
y3 = long(p->u_double[1] * m_height) + m_top;
x4 = long(p->u_double[2] * m_width) + m_left;
y4 = long(p->u_double[3] * m_height) + m_top;
dc->Arc(x1,y1,x2,y2,x3,y3,x4,y4);
continue;
}
}
dc->SelectObject(oldpen);
DeleteObject(&pen);
return true;
}
BOOL MEAS::SelectNone()
{
m_selitem = NULL;
return TRUE;
}
BOOL MEAS::SelectItem(CPoint hp)
{
item *p = NULL,*first = NULL;
int dx,dy;
for (p = m_itemhead->next; NULL != p; p = p->next)
{
if (TYPE_START == p->type)
{
first = p;
continue;
}
if (TYPE_LINE != p->type) continue;
dx = long(p->x0 * m_width) + m_left - hp.x;
dy = long(p->y0 * m_height) + m_top - hp.y;
if (abs(dx) < 5 && abs(dy) < 5)
{
m_selitem = first;
return TRUE;
}
dx = long(p->x1 * m_width) + m_left - hp.x;
dy = long(p->y1 * m_height) + m_top - hp.y;
if (abs(dx) < 5 && abs(dy) < 5)
{
m_selitem = first;
return TRUE;
}
}
return FALSE;
}
BOOL MEAS::SelFlash(CDC *dc,BOOL off)
{
if (NULL == m_selitem) return FALSE;
CPoint start;
CPoint end;
CPen pen;
CPen *oldpen = NULL;
if (off)
{
pen.CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
}
else
{
if (m_lighton)
{
pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
}
else
{
pen.CreatePen(PS_SOLID,1,RGB(255,0,0));
}
m_lighton = !m_lighton;
}
oldpen = dc->SelectObject(&pen);
for (item *p = m_selitem; NULL != p && TYPE_END != p->type; p = p->next)
{
if (TYPE_LINE == p->type)
{
start.x = long(p->x0 * m_width) + m_left;
start.y = long(p->y0 * m_height) + m_top;
end.x = long(p->x1 * m_width) + m_left;
end.y = long(p->y1 * m_height) + m_top;
dc->MoveTo(start);
dc->LineTo(end);
}
}
dc->SelectObject(oldpen);
DeleteObject(&pen);
return TRUE;
}
BOOL MEAS::SetScale(float left, float top, float width, float height)
{
m_left = left;
m_top = top;
m_width = width;
m_height = height;
return TRUE;
}
BOOL MEAS::EraseSel()
{
item *next = NULL,*last = NULL;
if (NULL == m_selitem) return FALSE;
for (last = m_selitem->last;;)
{
next = last->next;
last->next = next->next;
delete next;
if (NULL == last->next) break;
if (TYPE_START == last->next->type) break;
}
if (NULL != last->next) last->next->last = last;
m_selitem = NULL;
SaveFile();
return TRUE;
}
BOOL MEAS::HasSelected()
{
return (NULL != m_selitem);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -