📄 mt.cpp
字号:
CMeta::SelFlash(INT iControl)
{
if (NULL == m_pSelItem) return FALSE;
if (!IsWindow(m_hWnd)) return FALSE;
HGDIOBJ hOldPen = NULL;
HGDIOBJ hNewPen = NULL;
HDC hDC = ::GetDC(m_hWnd);
switch (iControl)
{
case MT_SELECT_OFF:
m_bLightOn = FALSE;
break;
case MT_SELECT_ON:
m_bLightOn = TRUE;
break;
case MT_SELECT_TURN:
default:
m_bLightOn = !m_bLightOn;
break;
}
hNewPen = ::CreatePen(PS_SOLID, 1, (m_bLightOn) ? (m_InfoHeader.FlashLightColor) : (m_InfoHeader.NormalColor));
::SetTextColor(hDC, (m_bLightOn) ? (m_InfoHeader.FlashLightColor) : (m_InfoHeader.NormalColor));
hOldPen = ::SelectObject(hDC, hNewPen);
ITEM *p = NULL;
for (p = m_pSelItem; NULL != p && TYPE_END != p->type; p = p->next)
DoRecord(hDC, p, R2_COPYPEN);
::SelectObject(hDC, hOldPen);
::DeleteObject(hNewPen);
::ReleaseDC(m_hWnd, hDC);
return TRUE;
}
// 从文件中读取记录数据
BOOL
CMeta::LoadFile(CHAR *argPathName)
{
ITEM *next = NULL;
ITEM *last = NULL;
if (NULL != argPathName) m_sFilePathName = argPathName;
if (m_sFilePathName.IsEmpty()) return FALSE;
// 首先释放曾经分配的资源
CleanUp();
// Open Data File
FILE *fp = fopen(m_sFilePathName, "rb");
if (NULL == fp) return FALSE;
// 读取文件头
if (1 != fread(&m_InfoHeader, sizeof(INFOHEADER), 1, fp))
{
fclose(fp);
return FALSE;
}
if (0x544d != m_InfoHeader.MTTag || // MT
_COMPATIBLE_VERSION_ > m_InfoHeader.iVersion)
{
fclose(fp);
SetNewHeader();
CleanUp();
return FALSE;
}
for (last = &m_tItemHeader;;)
{
next = new ITEM;
if (1 == fread(next, sizeof(ITEM), 1, fp))
{
last->next = next;
next->prior = last;
next->next = NULL;
m_tItemHeader.prior = last = next;
}
else
{
delete next;
next = NULL;
break;
}
}
fclose(fp);
if (NULL == FindRecord(TYPE_STANDARDLENGTH))
{
POINT pa;
POINT pb;
pa.x = 100;
pa.y = 100;
pb.x = 200;
pb.y = 100;
SetStandLength(pa, pb, 5.0, "厘米");
}
return TRUE;
}
BOOL
CMeta::ReDraw(HDC hDC)
{
/*
if (FALSE == m_InfoHeader.SourceDCInited ||
FALSE == m_InfoHeader.SourcePICInited ||
FALSE == m_InfoHeader.TargetDCInited ||
FALSE == m_InfoHeader.TargetPICInited) return FALSE;
*/
HGDIOBJ hOldPen = NULL;
HGDIOBJ hNewPen = NULL;
ITEM *p = NULL;
// SetActive();
hNewPen = ::CreatePen(PS_SOLID, 1, m_InfoHeader.NormalColor);
hOldPen = ::SelectObject(hDC, hNewPen);
::SetTextColor(hDC, m_InfoHeader.NormalColor);
for (p = m_tItemHeader.next; NULL != p; p = p->next)
DoRecord(hDC, p, R2_COPYPEN );
::SelectObject(hDC, hOldPen);
::DeleteObject(hNewPen);
return TRUE;
}
VOID
CMeta::InsertArc(int x0, int y0, int x2, int y2, int x3, int 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;
AppendNode(node);
delete node;
}
BOOL
CMeta::EndRecord()
{
if (FALSE == m_bRecording) return FALSE;
m_bRecording = FALSE;
ITEM *node = new ITEM;
node->type = TYPE_END;
strcpy(node->word, "");
AppendNode(node);
delete node;
return (TRUE);
}
/*
Inited OK.
*/
BOOL
CMeta::DoRecord(HDC hDC, CMeta::ITEM *node, INT fnDrawMode)
{
if (m_bHideMark) return (TRUE);
if (NULL == hDC) return (FALSE);
if (NULL == node) return (FALSE);
CPoint start;
CPoint end;
ITEM p;
memcpy(&p, node, sizeof(ITEM));
if (FALSE != m_bIsZoom)
{
CRect rcTP, rcSP, rcTD, rcSD;
rcTP = m_InfoHeader.TargetPICRect;
rcSP = m_InfoHeader.SourcePICRect;
rcTD = m_InfoHeader.TargetDCRect;
rcSD = m_InfoHeader.SourceDCRect;
p.x0 = rcTP.left + (node->x0 - rcSP.left) * rcTP.Width() / rcSP.Width();
p.y0 = rcTP.top + (node->y0 - rcSP.top) * rcTP.Height() / rcSP.Height();
p.x1 = rcTP.left + (node->x1 - rcSP.left) * rcTP.Width() / rcSP.Width();
p.y1 = rcTP.top + (node->y1 - rcSP.top) * rcTP.Height() / rcSP.Height();
}
::SetROP2(hDC, fnDrawMode);
::SetBkMode(hDC, TRANSPARENT);
if (TYPE_TEXT == p.type)
{
INT cx = p.x0;
INT cy = p.y0;
if (0 == strlen(p.word))
{
int dx = (p.x1 > 0) ? p.x1 : 20;
int dy = (p.y1 > 0) ? p.y1 : 10;
cx -= dx / 2;
cy -= dy / 2;
::MoveToEx(hDC, cx, cy, NULL);
::LineTo(hDC, cx + dx, cy);
::LineTo(hDC, cx + dx, cy + dy);
::LineTo(hDC, cx, cy + dy);
::LineTo(hDC, cx, cy);
}
else
{
SIZE size;
::GetTextExtentPoint32(hDC, p.word, strlen(p.word), &size);
cx -= size.cx / 2;
cy -= size.cy / 2;
::TextOut(hDC, cx, cy, LPCTSTR(p.word), strlen(p.word));
}
return TRUE;
}
if (TYPE_CROSS == p.type)
{
end.x = p.x0;
end.y = p.y0;
::MoveToEx(hDC, end.x - CROSS_LENGTH, end.y, NULL);
::LineTo(hDC, end.x + CROSS_LENGTH + 1, end.y);
::MoveToEx(hDC, end.x, end.y - CROSS_LENGTH, NULL);
::LineTo(hDC, end.x, end.y + CROSS_LENGTH + 1);
return TRUE;
}
if (TYPE_BIGPOINT == p.type)
{
end.x = p.x0;
end.y = p.y0;
::Ellipse(hDC, end.x - 3, end.y - 3, end.x + 3, end.y + 3);
return TRUE;
}
if (TYPE_RECT == p.type)
{
start.x = p.x0;
start.y = p.y0;
end.x = p.x1;
end.y = p.y1;
::MoveToEx(hDC, start.x, start.y, NULL);
::LineTo(hDC, end.x, start.y);
::LineTo(hDC, end.x, end.y);
::LineTo(hDC, start.x, end.y);
::LineTo(hDC, start.x, start.y);
if (NULL != p.word)
{
SIZE size;
::GetTextExtentPoint32(hDC, p.word, strlen(p.word), &size);
int x, y;
x = (start.x + end.x - size.cx) / 2;
y = (start.y + end.y - size.cy) / 2;
::TextOut(hDC, x, y, p.word, strlen(p.word));
}
return TRUE;
}
if (TYPE_LINE == p.type)
{
start.x = p.x0;
start.y = p.y0;
end.x = p.x1;
end.y = p.y1;
::MoveToEx(hDC, start.x, start.y, NULL);
::LineTo(hDC, end.x, end.y);
return TRUE;
}
if (TYPE_ARROW == p.type)
{
CPoint ArrowPointA;
CPoint ArrowPointB;
start.x = p.x0;
start.y = p.y0;
end.x = p.x1;
end.y = p.y1;
GetArrow(start, end, ArrowPointA, ArrowPointB);
::MoveToEx(hDC, end.x, end.y, NULL);
::LineTo(hDC, start.x, start.y);
::MoveToEx(hDC, ArrowPointA.x, ArrowPointA.y, NULL);
::LineTo(hDC, start.x, start.y);
::MoveToEx(hDC, ArrowPointB.x, ArrowPointB.y, NULL);
::LineTo(hDC, start.x, start.y);
return TRUE;
}
if (TYPE_ARC == p.type)
{
int x1, y1, x2, y2, x3, y3, x4, y4;
x1 = p.x0 - 10;
y1 = p.y0 - 10;
x2 = x1 + 20;
y2 = y1 + 20;
x3 = p.u_int[0];
y3 = p.u_int[1];
x4 = p.u_int[2];
y4 = p.u_int[3];
::Arc(hDC, x1, y1, x2, y2, x3, y3, x4, y4);
return TRUE;
}
return FALSE;
}
BOOL
CMeta::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
CMeta::MouseDown(UINT nFlags, CPoint point)
{
if (FALSE != m_bMouseDown) return FALSE;
m_bMouseDown = TRUE;
// 获取设备上下文
m_hDC = ::GetDC(m_hWnd);
// 创建画笔
m_hCurrentPen = ::CreatePen(PS_SOLID, 1, m_InfoHeader.NormalColor);
m_hOldPen = ::SelectObject(m_hDC, m_hCurrentPen);
// 设置鼠标按下标志和鼠标捕获
// ::SetCapture(hCaptureWnd);
// 开始点与结束点设置初始值
m_tMouseStart = point;
m_tMouseEnd = point;
// 根据当前不同的测量状态做分支处理
// 删除测量标记
if (ACTION_DELETE == m_iCurrentAction)
{
SelectNone();
if (TRUE == SelectItem(point))
{
SelFlash(MT_SELECT_ON);
// m_fa.iControl = MT_CONTROL_PLAY;
}
return TRUE;
}
if (ACTION_CHAOSAREA == m_iCurrentAction ||
ACTION_LINEDIST == m_iCurrentAction ||
ACTION_CHAOSAREA2 == m_iCurrentAction ||
ACTION_LINEDIST2 == m_iCurrentAction ||
ACTION_ANGLE == m_iCurrentAction ||
ACTION_CHAOSANGLE == m_iCurrentAction ||
ACTION_CHAOSANGLE2 == m_iCurrentAction)
{
if (ACTION_CHAOSAREA == m_iCurrentAction ||
ACTION_LINEDIST == m_iCurrentAction ||
ACTION_ANGLE == m_iCurrentAction ||
ACTION_CHAOSANGLE == m_iCurrentAction ||
ACTION_CHAOSANGLE2 == m_iCurrentAction)
{
if (ACTION_CHAOSANGLE2 == m_iCurrentAction)
{
m_tMouseNewStart = m_tMouseStart;
}
else
{
m_iCurrentPoint = 0;
m_tMouseNewStart = point;
}
m_pt[m_iCurrentPoint++] = point;
m_fArea = 0.0;
m_iLength = 0.0;
m_tTempItem.type = TYPE_LINE;
}
m_tMouseStart = m_tMouseNewStart;
m_tTempItem.x0 = m_tMouseStart.x;
m_tTempItem.y0 = m_tMouseStart.y;
m_tTempItem.x1 = m_tMouseEnd.x;
m_tTempItem.y1 = m_tMouseEnd.y;
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
// 测量
if (ACTION_ANGLE2 == m_iCurrentAction)
{
m_tMouseStart = m_tMouseNewStart;
m_tMouseEnd = point;
m_tTempItem.x0 = m_tMouseStart.x;
m_tTempItem.y0 = m_tMouseStart.y;
m_tTempItem.x1 = m_tMouseEnd.x;
m_tTempItem.y1 = m_tMouseEnd.y;
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
if (ACTION_RECTAREA == m_iCurrentAction)
{
m_tTempItem.type = TYPE_RECT;
m_tTempItem.x0 = m_tMouseStart.x;
m_tTempItem.y0 = m_tMouseStart.y;
m_tTempItem.x1 = m_tMouseEnd.x;
m_tTempItem.y1 = m_tMouseEnd.y;
strcpy(m_tTempItem.word, "");
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
if (ACTION_SETSTANDARDLENGTH == m_iCurrentAction)
{
m_tTempItem.type = TYPE_LINE;
m_tTempItem.x0 = m_tMouseStart.x;
m_tTempItem.y0 = m_tMouseStart.y;
m_tTempItem.x1 = m_tMouseEnd.x;
m_tTempItem.y1 = m_tMouseEnd.y;
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
if (ACTION_ARROW == m_iCurrentAction)
{
m_tTempItem.type = TYPE_ARROW;
m_tTempItem.x0 = m_tMouseStart.x;
m_tTempItem.y0 = m_tMouseStart.y;
m_tTempItem.x1 = m_tMouseEnd.x;
m_tTempItem.y1 = m_tMouseEnd.y;
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
if (ACTION_TEXT == m_iCurrentAction)
{
m_tTempItem.type = TYPE_TEXT;
m_tTempItem.x0 = m_tMouseEnd.x;
m_tTempItem.y0 = m_tMouseEnd.y;
SIZE size;
::GetTextExtentPoint32(m_hDC, m_sText, strlen(m_sText), &size);
m_tTempItem.x1 = size.cx;
m_tTempItem.y1 = size.cy;
strcpy(m_tTempItem.word, "");
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
if (ACTION_CROSS == m_iCurrentAction)
{
m_tTempItem.type = TYPE_CROSS;
m_tTempItem.x0 = m_tMouseEnd.x;
m_tTempItem.y0 = m_tMouseEnd.y;
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
return FALSE;
}
BOOL
CMeta::MouseMove(UINT nFlags, CPoint point)
{
if (FALSE == m_bMouseDown) return FALSE;
#ifdef _DEBUG
CHAR buf[10];
CString sCaption;
sCaption = "(";
sCaption += itoa(point.x, buf, 10);
sCaption += ",";
sCaption += itoa(point.y, buf, 10);
sCaption += ")";
::AfxGetMainWnd()->SetWindowText(sCaption);
#endif
if (ACTION_CHAOSAREA == m_iCurrentAction || // 不规则面积
ACTION_CHAOSAREA2 == m_iCurrentAction ||
ACTION_CHAOSANGLE == m_iCurrentAction || //
ACTION_CHAOSANGLE2 == m_iCurrentAction ||
ACTION_ANGLE2 == m_iCurrentAction ||
ACTION_ANGLE == m_iCurrentAction ||
ACTION_LINEDIST2 == m_iCurrentAction ||
ACTION_LINEDIST == m_iCurrentAction ||
ACTION_SETSTANDARDLENGTH == m_iCurrentAction)
{
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
if (ACTION_CHAOSAREA2 == m_iCurrentAction &&
FALSE != (nFlags & MK_CONTROL) &&
m_iCurrentPoint >= 3)
{
m_tMouseEnd = m_pt[0];
}
else
{
m_tMouseEnd = point;
}
m_tTempItem.x1 = m_tMouseEnd.x;
m_tTempItem.y1 = m_tMouseEnd.y;
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
// 矩形面积
if (ACTION_RECTAREA == m_iCurrentAction)
{
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
m_tMouseEnd = point;
if (nFlags & MK_CONTROL) // 正方形
{
int dx = abs(m_tMouseEnd.x - m_tMouseStart.x);
int dy = abs(m_tMouseEnd.y - m_tMouseStart.y);
if (dx > dy)
{
if (m_tMouseEnd.x > m_tMouseStart.x)
m_tMouseEnd.x = m_tMouseStart.x + dy;
else
m_tMouseEnd.x = m_tMouseStart.x - dy;
}
else
{
if (m_tMouseEnd.y > m_tMouseStart.y)
m_tMouseEnd.y = m_tMouseStart.y + dx;
else
m_tMouseEnd.y = m_tMouseStart.y - dx;
}
}
m_tTempItem.x1 = m_tMouseEnd.x;
m_tTempItem.y1 = m_tMouseEnd.y;
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
// 箭头
if (ACTION_ARROW == m_iCurrentAction)
{
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
m_tMouseEnd = point;
m_tTempItem.x1 = m_tMouseEnd.x;
m_tTempItem.y1 = m_tMouseEnd.y;
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
// 十字叉
if (ACTION_CROSS == m_iCurrentAction)
{
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
m_tMouseEnd = point;
m_tTempItem.x0 = m_tMouseEnd.x;
m_tTempItem.y0 = m_tMouseEnd.y;
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
// 文字
if (ACTION_TEXT == m_iCurrentAction)
{
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
m_tMouseEnd = point;
m_tTempItem.x0 = m_tMouseEnd.x;
m_tTempItem.y0 = m_tMouseEnd.y;
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
return TRUE;
}
return FALSE;
}
BOOL
CMeta::MouseUp(UINT nFlags, CPoint point, CWnd *pInfoWnd)
{
if (FALSE == m_bMouseDown) return FALSE;
m_bMouseDown = FALSE;
::SetTextColor(m_hDC, m_InfoHeader.NormalColor);
// ::ReleaseCapture();
if (ACTION_CHAOSAREA2 == m_iCurrentAction)
{
if (FALSE != (nFlags & MK_CONTROL)) // End
{
if (m_iCurrentPoint >= 3)
{
DoRecord(m_hDC, &m_tTempItem, R2_XORPEN);
m_tTempItem.x1 = m_pt[0].x;
m_tTempItem.y1 = m_pt[0].y;
DoRecord(m_hDC, &m_tTempItem, R2_COPYPEN);
// -> Record
int i;
BeginRecord(TYPE_CHAOSAREA);
int cx = 0;
int cy = 0;
m_tTempItem.type = TYPE_LINE;
for (i = 0; i < m_iCurrentPoint; i++)
{
m_tTempItem.x0 = m_pt[i].x;
m_tTempItem.y0 = m_pt[i].y;
if (i == m_iCurrentPoint - 1)
{
m_tTempItem.x1 = m_pt[0].x;
m_tTempItem.y1 = m_pt[0].y;
}
else
{
m_tTempItem.x1 = m_pt[i + 1].x;
m_tTempItem.y1 = m_pt[i + 1].y;
}
AppendNode(&m_tTempItem);
cx += m_pt[i].x;
cy += m_pt[i].y;
}
FLOAT area = m_fArea;
char buf[200];
area *= (m_InfoHeader.fD2 * m_InfoHeader.fD2) /
(m_InfoHeader.fD1 * m_InfoHeader.fD1);
sprintf(buf, "%5.2f 平方%s", area, m_InfoHeader.sUnit);
cx /= m_iCurrentPoint;
cy /= m_iCurrentPoint;
m_tTempItem.type = TYPE_TEXT;
m_tTempItem.x0 = cx;
m_tTempItem.y0 = cy;
strcpy(m_tTempItem.word, buf);
DoRecord(m_hDC, &m_tTempItem, R2_COPYPEN);
AppendNode(&m_tTempItem);
EndRecord();
// <- Record
if (NULL != pInfoWnd)
{
pInfoWnd->SetWindowText("");
}
m_iCurrentAction = (nFlags & MK_SHIFT)?ACTION_LINEDIST:ACTION_NOTHING;
}
else
{
m_pt[m_iCurrentPoint].x = point.x;
m_pt[m_iCurrentPoint].y = point.y;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -