// ImgDefect.cpp : 备泅 颇老涝聪促.
|
//
|
|
#include "stdafx.h"
|
#include "ImgDefect.h"
|
#include "math.h"
|
|
|
// CImgDefect
|
|
IMPLEMENT_DYNAMIC(CImgDefect, CWnd)
|
|
CImgDefect::CImgDefect():m_ptOrg(0,0),m_nCam(0),m_nSide(0),m_nMode(0),m_bMeasure(FALSE),m_nTurn(0),m_nTextPos(0)
|
{
|
m_dPx = 10;
|
m_dPy = 10;
|
|
m_dRatioX = 1.0;
|
m_dRatioY = 1.0;
|
|
}
|
|
CImgDefect::~CImgDefect()
|
{
|
}
|
|
|
BEGIN_MESSAGE_MAP(CImgDefect, CWnd)
|
ON_WM_CREATE()
|
ON_WM_DESTROY()
|
ON_WM_PAINT()
|
ON_WM_MOUSEMOVE()
|
ON_WM_LBUTTONDOWN()
|
END_MESSAGE_MAP()
|
|
|
|
// CImgDefect 皋矫瘤 贸府扁涝聪促.
|
|
|
BOOL CImgDefect::Create(RECT const & rect, CWnd * pParentWnd /*= 0*/)
|
{
|
|
#ifdef _DEBUG
|
ASSERT(pParentWnd); // Exception
|
#endif
|
|
return CWnd::Create(NULL, NULL, WS_VISIBLE | WS_CHILD, rect, pParentWnd, 0);
|
}
|
|
int CImgDefect::OnCreate(LPCREATESTRUCT lpCreateStruct)
|
{
|
if (CWnd::OnCreate(lpCreateStruct) == -1)
|
return -1;
|
|
GetClientRect(&m_WndRect);
|
|
return 0;
|
}
|
|
void CImgDefect::OnDestroy()
|
{
|
CWnd::OnDestroy();
|
if(!m_img.IsNull())
|
m_img.Destroy();
|
}
|
|
void CImgDefect::OnPaint()
|
{
|
CPaintDC dc(this); // device context for painting
|
// TODO: 咯扁俊 皋矫瘤 贸府扁 内靛甫 眠啊钦聪促.
|
// 弊府扁 皋矫瘤俊 措秦辑绰 CWnd::OnPaint()阑(甫) 龋免窍瘤 付绞矫坷.
|
|
CDC *pDC = GetDC();
|
|
CDC memDC;
|
CBitmap Bitmap ,*pOldBitmap;
|
|
memDC.CreateCompatibleDC(pDC);
|
Bitmap.CreateCompatibleBitmap(pDC,abs(m_WndRect.Width()),abs(m_WndRect.Height()));
|
|
pOldBitmap = (CBitmap*)memDC.SelectObject(&Bitmap);
|
|
|
if(!m_img.IsNull())
|
{
|
|
CRect rc;
|
rc= m_WndRect;
|
|
if(m_nMode == 1)
|
{
|
if(!m_img.IsNull())
|
{
|
int nStartX = 0;
|
int nStartY = 0;
|
int nWidth = m_img.GetWidth()-1;
|
int nHeight = m_img.GetHeight()-1;
|
memDC.SetStretchBltMode(COLORONCOLOR);
|
|
|
if(m_nTurn & 1)
|
{
|
nStartX = nWidth;
|
nWidth = - nWidth;
|
}
|
|
if(m_nTurn & 2)
|
{
|
nStartY = nHeight;
|
nHeight = - nHeight;
|
}
|
|
m_img.StretchBlt(memDC.GetSafeHdc(),0,0,m_WndRect.Width(),m_WndRect.Height(),nStartX,nStartY,nWidth,nHeight);
|
|
if(m_bMeasure)
|
DrawManualMeasure(memDC);
|
}
|
}
|
else // 角 Size Mode
|
{
|
rc.right = m_img.GetWidth();
|
rc.bottom = m_img.GetHeight();
|
memDC.SetStretchBltMode(COLORONCOLOR);
|
|
m_img.Draw(memDC.GetSafeHdc(),-m_ptOrg.x,-m_ptOrg.y);
|
if(m_bMeasure)
|
DrawManualMeasure(memDC);
|
}
|
|
|
DrawTextInfo(memDC);
|
pDC->BitBlt(0,0,m_WndRect.Width(),m_WndRect.Height(),&memDC,0,0,SRCCOPY);
|
}
|
memDC.SelectObject(pOldBitmap);
|
memDC.DeleteDC();
|
ReleaseDC(pDC);
|
}
|
|
|
void CImgDefect::SetDefectImage(int nSide,CString strPath,double dPixel_X,double dPixel_Y)
|
{
|
m_nSide = nSide;
|
|
if(!m_img.IsNull())
|
m_img.Destroy();
|
|
if(!FAILED(m_img.Load(strPath)))
|
{
|
Invalidate(TRUE);
|
|
m_dPx = dPixel_X;
|
m_dPy = dPixel_Y;
|
}
|
}
|
|
int CImgDefect::GetImgWidth()
|
{
|
if(!m_img.IsNull())
|
return m_img.GetWidth();
|
else
|
return 0;
|
}
|
|
int CImgDefect::GetImgHeight()
|
{
|
if(!m_img.IsNull())
|
return m_img.GetHeight();
|
else
|
return 0;
|
}
|
|
|
void CImgDefect::OnMouseMove(UINT nFlags, CPoint point)
|
{
|
if(!m_img.IsNull())
|
{
|
m_ptMouseCurrent = point;
|
m_ptMouseCurrent.x = (m_img.GetWidth() < point.x) ? m_img.GetWidth() : point.x;
|
m_ptMouseCurrent.y = (m_img.GetHeight() < point.y) ? m_img.GetHeight() : point.y;
|
|
if(m_ManualMeausre.bStart == TRUE || m_ManualMeausre.bModifyEnd == TRUE)
|
{
|
m_ManualMeausre.ptMeasureEnd.x = m_ptMouseCurrent.x + m_ptOrg.x;
|
m_ManualMeausre.ptMeasureEnd.y = m_ptMouseCurrent.y + m_ptOrg.y;
|
}
|
else if(m_ManualMeausre.bModifyStart == TRUE)
|
{
|
m_ManualMeausre.ptMeasureStart.x = m_ptMouseCurrent.x + m_ptOrg.x;
|
m_ManualMeausre.ptMeasureStart.y = m_ptMouseCurrent.y + m_ptOrg.y;
|
}
|
Invalidate(FALSE);
|
}
|
CWnd::OnMouseMove(nFlags, point);
|
}
|
|
void CImgDefect::OnLButtonDown(UINT nFlags, CPoint point)
|
{
|
if(!m_img.IsNull())
|
{
|
if(m_ManualMeausre.bStart == FALSE)
|
{
|
if( m_ManualMeausre.ptMeasureStart.x > point.x + m_ptOrg.x - 5
|
&& m_ManualMeausre.ptMeasureStart.x < point.x + m_ptOrg.x + 5
|
&& m_ManualMeausre.ptMeasureStart.y > point.y + m_ptOrg.y - 5
|
&& m_ManualMeausre.ptMeasureStart.y < point.y + m_ptOrg.y + 5 )
|
{
|
if(m_ManualMeausre.bModifyStart)
|
m_ManualMeausre.bModifyStart = FALSE;
|
else
|
m_ManualMeausre.bModifyStart = TRUE;
|
|
}
|
else if(m_ManualMeausre.ptMeasureEnd.x > point.x + m_ptOrg.x - 5
|
&& m_ManualMeausre.ptMeasureEnd.x < point.x + m_ptOrg.x + 5
|
&& m_ManualMeausre.ptMeasureEnd.y > point.y + m_ptOrg.y - 5
|
&& m_ManualMeausre.ptMeasureEnd.y < point.y + m_ptOrg.y + 5 )
|
{
|
if(m_ManualMeausre.bModifyEnd)
|
m_ManualMeausre.bModifyEnd = FALSE;
|
else
|
m_ManualMeausre.bModifyEnd = TRUE;
|
}
|
else
|
{
|
m_ManualMeausre.bModifyStart = FALSE;
|
m_ManualMeausre.bModifyEnd = FALSE;
|
m_ManualMeausre.bStart = TRUE;
|
|
|
|
|
m_ManualMeausre.ptMeasureStart.x= point.x + m_ptOrg.x;
|
m_ManualMeausre.ptMeasureStart.y= point.y + m_ptOrg.y;
|
}
|
}
|
else
|
{
|
m_ManualMeausre.bStart = FALSE;
|
}
|
Invalidate(FALSE);
|
}
|
|
|
CWnd::OnLButtonDown(nFlags, point);
|
}
|
|
|
void CImgDefect::DrawManualMeasure(CDC &dc)
|
{
|
|
CPen penX(PS_SOLID, 1, RGB(0,255,255));
|
CPen penY(PS_SOLID, 1, RGB(255,255,0));
|
CPen *pOldPen;
|
CBrush *pOldBruh;
|
|
|
CBrush BrushRed;
|
|
BrushRed.CreateSolidBrush(RGB(0xff,0x00,0x00));
|
|
//dc.SetROP2(R2_XORPEN);
|
pOldPen = (CPen*)dc.SelectObject(&penX);
|
pOldBruh = (CBrush*)dc.SelectObject(BrushRed);
|
|
|
CPoint ptStart;
|
CPoint ptEnd;
|
|
ptStart.x = (m_ManualMeausre.ptMeasureStart.x - m_ptOrg.x);
|
ptStart.y = (m_ManualMeausre.ptMeasureStart.y - m_ptOrg.y);
|
|
ptEnd.x = (m_ManualMeausre.ptMeasureEnd.x - m_ptOrg.x);
|
ptEnd.y = (m_ManualMeausre.ptMeasureEnd.y - m_ptOrg.y);
|
|
dc.MoveTo(0 , ptStart.y);
|
dc.LineTo(m_img.GetWidth() , ptStart.y);
|
dc.MoveTo(ptStart.x , 0);
|
dc.LineTo(ptStart.x , m_img.GetHeight());
|
|
dc.MoveTo(0 , ptEnd.y);
|
dc.LineTo(m_img.GetWidth() , ptEnd.y);
|
dc.MoveTo(ptEnd.x , 0);
|
dc.LineTo(ptEnd.x , m_img.GetHeight());
|
|
|
(CPen*)dc.SelectObject(&penY);
|
dc.MoveTo(ptStart.x , ptStart.y);
|
dc.LineTo(ptEnd.x , ptEnd.y);
|
|
dc.MoveTo(ptStart.x - 5,
|
ptStart.y - 5);
|
|
dc.LineTo(ptStart.x + 5,
|
ptStart.y + 5);
|
|
dc.MoveTo(ptStart.x + 5,
|
ptStart.y - 5);
|
|
dc.LineTo(ptStart.x - 5,
|
ptStart.y + 5);
|
|
dc.MoveTo(ptEnd.x - 5,
|
ptEnd.y - 5);
|
|
dc.LineTo(ptEnd.x + 5,
|
ptEnd.y + 5);
|
|
dc.MoveTo(ptEnd.x + 5,
|
ptEnd.y - 5);
|
|
dc.LineTo(ptEnd.x - 5,
|
ptEnd.y + 5);
|
|
|
|
CString strTmp;
|
int nTmp;
|
CRect rc;
|
double dPosX,dPosY;
|
|
rc.top = m_ManualMeausre.ptMeasureStart.y;
|
rc.bottom = m_ManualMeausre.ptMeasureStart.y + 20;
|
rc.left = m_ManualMeausre.ptMeasureStart.x;
|
rc.right = m_ManualMeausre.ptMeasureStart.x + 20;
|
|
if(m_ManualMeausre.bModifyStart)
|
m_ManualMeausre.nTextPos = 0;
|
else if(m_ManualMeausre.bModifyEnd || m_ManualMeausre.bStart)
|
m_ManualMeausre.nTextPos = 1;
|
else;
|
|
|
CPoint ptText;
|
const int TextsizeX = 150;
|
const int TextsizeY = 50;
|
|
if(m_ManualMeausre.nTextPos == 0)
|
{
|
if(ptStart.x > m_WndRect.Width()/2)
|
ptText.x = ptStart.x - TextsizeX;
|
else
|
ptText.x = ptStart.x + 5;
|
|
if(ptStart.y > m_img.GetHeight()/2)
|
ptText.y =ptStart.y - TextsizeY;
|
else
|
ptText.y = ptStart.y + 5;
|
|
}
|
else
|
{
|
if(ptEnd.x > m_WndRect.Width()/2)
|
ptText.x = ptEnd.x - TextsizeX;
|
else
|
ptText.x = ptEnd.x + 5;
|
|
if(ptEnd.y > m_img.GetHeight()/2)
|
ptText.y = ptEnd.y - TextsizeY;
|
else
|
ptText.y = ptEnd.y + 5;
|
}
|
|
|
dc.SetBkMode(TRANSPARENT);
|
dc.SetTextColor(RGB(255,0,0));
|
|
nTmp = abs(m_ManualMeausre.ptMeasureStart.x - m_ManualMeausre.ptMeasureEnd.x);
|
dPosX = m_dRatioX * nTmp/1000.0 * m_dPx;
|
strTmp.Format(_T("X : %4.3f mm [%d pixel]"), dPosX , nTmp);
|
dc.TextOut( ptText.x + 5, ptText.y,strTmp);
|
|
|
dc.SetTextColor( RGB(250, 150, 0));
|
nTmp = abs(m_ManualMeausre.ptMeasureStart.y - m_ManualMeausre.ptMeasureEnd.y);
|
|
|
dPosY = m_dRatioY * nTmp/1000.0 * m_dPy;
|
strTmp.Format(_T("Y : %4.3f mm [%d pixel]"),dPosY, nTmp);
|
dc.TextOut( ptText.x + 5, ptText.y + 15 ,strTmp);
|
|
|
dc.SetTextColor(RGB(255,0,0));
|
dPosX = sqrtf(float(dPosX *dPosX + dPosY*dPosY));
|
strTmp.Format(_T("D : %4.3f mm "),dPosX);
|
dc.TextOut( ptText.x + 5, ptText.y + 30 ,strTmp);
|
|
|
//dc.SelectObject(pOldBruh);
|
dc.SelectObject(pOldPen);
|
// dc.SetROP2(R2_COPYPEN);
|
|
BrushRed.DeleteObject();
|
|
}
|
|
void CImgDefect::SetImgTurn(int nMode)
|
{
|
if(nMode == 1)
|
{
|
if(m_nTurn & 1)
|
m_nTurn &= 0xfe;
|
else
|
m_nTurn |= 1;
|
}
|
else
|
{
|
if(m_nTurn & 2)
|
m_nTurn &= 0xfd;
|
else
|
m_nTurn |= 2;
|
|
}
|
Invalidate(TRUE);
|
|
|
}
|
|
void CImgDefect::DrawTextInfo(CDC &dc)
|
{
|
int nAlign = 0;
|
CRect rc = m_WndRect;
|
switch(m_nTextPos)
|
{
|
case 0:
|
return;
|
break;
|
|
case 1:
|
rc.bottom = 40;
|
nAlign = DT_LEFT | DT_TOP;
|
break;
|
|
case 2:
|
nAlign = DT_RIGHT | DT_TOP;
|
rc.bottom = 40;
|
break;
|
|
case 3:
|
nAlign = DT_LEFT | DT_BOTTOM;
|
rc.top = rc.bottom - 40;
|
break;
|
|
case 4:
|
nAlign = DT_RIGHT | DT_BOTTOM;
|
rc.top = rc.bottom - 40;
|
break;
|
}
|
dc.SetBkMode(TRANSPARENT);
|
dc.SetTextColor(RGB(180, 180, 0));
|
|
CFont TextFont;
|
TextFont.CreateFont(30 , 0, 0, 0, 300 , FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
|
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN,_T("Arial"));
|
|
CFont *pOldFont = (CFont*) dc.SelectObject(&TextFont);
|
dc.DrawText(m_strTextInfo,rc,nAlign);
|
|
|
dc.SelectObject( pOldFont );
|
TextFont.DeleteObject();
|
|
}
|