?? target.cpp
字號:
// Target.cpp : implementation file
//
#include "stdafx.h"
#include "new ruler.h"
#include "Target.h"
#include "Math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Target
CTarget::CTarget()
{
m_pPenAux = NULL;
m_colorStart = RGB(255, 0, 0);
m_colorAux = RGB(0, 0, 0);
m_colorFont = RGB(0, 0, 0);
}
CTarget::~CTarget()
{
if (m_pPenAux)
delete m_pPenAux;
}
BEGIN_MESSAGE_MAP(CTarget, CWnd)
//{{AFX_MSG_MAP(CTarget)
ON_WM_RBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTarget message handlers
void CTarget::CreateTarget(LPCTSTR lpTitle, CWnd* pWnd)
{
//取得屏幕寬度
m_cxScreen = ::GetSystemMetrics(SM_CXSCREEN);
m_cyScreen = ::GetSystemMetrics(SM_CYSCREEN);
//創(chuàng)建透明窗口
CreateEx(WS_EX_TOPMOST,
AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
"Target", WS_POPUP, 0, 0, m_cxScreen, m_cyScreen, NULL, NULL, NULL );
//得到當(dāng)前窗口的DC,以便繪制輔助線、坐標(biāo)等
CDC * pDC;
pDC = GetDC();
//V1.06后pDC不保存,改為需要繪圖時再取DC指針,否則會非法操作
//表示目前尚未點擊
m_bSecond = FALSE;
//記錄父窗口的指針,以便測量結(jié)束進行通知
m_pWndParent = pWnd;
//窗口內(nèi)存DC保存當(dāng)前屏幕,輔助線移動時重繪窗口
m_MemDC.CreateCompatibleDC(NULL);
CBitmap Bitmap;
Bitmap.CreateCompatibleBitmap(pDC, m_cxScreen, m_cyScreen);
m_MemDC.SelectObject(&Bitmap);
m_MemDC.BitBlt(0, 0, m_cxScreen, m_cyScreen, pDC, 0, 0, SRCCOPY);
Bitmap.DeleteObject();
//創(chuàng)建輔助線的畫筆
m_pPenAux = new CPen(PS_SOLID, 1, m_colorAux);
}
void CTarget::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// CDC * pDC;
// pDC = GetDC();
m_MemDC.DeleteDC();
CWnd::OnRButtonUp(nFlags, point);
// pDC->DeleteDC();
::DeleteObject(m_pPenAux->m_hObject);
delete m_pPenAux;
m_pPenAux = NULL;
DestroyWindow();
m_pWndParent->PostMessage(WM_CANCELCLICK);
}
void CTarget::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC * pDC;
pDC = GetDC();
if(m_bSecond == FALSE)
{
m_StartPos = point;
m_bSecond = TRUE;
pDC->BitBlt(0, 0, m_cxScreen, m_cyScreen, &m_MemDC, 0, 0, SRCCOPY);
CPen pen(PS_SOLID, 2, m_colorStart);
CPen * pOldPen;
pOldPen = pDC->SelectObject(&pen);
pDC->MoveTo(point.x - 10, point.y);
pDC->LineTo(point.x + 10, point.y);
pDC->MoveTo(point.x, point.y - 10);
pDC->LineTo(point.x, point.y + 10);
pDC->SelectObject(pOldPen);
::DeleteObject(pen.m_hObject);
m_MemDC.BitBlt(0, 0, m_cxScreen, m_cyScreen, pDC, 0, 0, SRCCOPY);
ReleaseDC(pDC); // Modified by Wang Hongying Apr. 21, 2002
}
else
{
m_EndPos = point;
Calculate();
// pDC->DeleteDC(); // Modified by Wang Hongying Apr. 21, 2002
ReleaseDC(pDC); // Modified by Wang Hongying Apr. 21, 2002
m_MemDC.DeleteDC();
::DeleteObject(m_pPenAux->m_hObject);
delete m_pPenAux;
m_pPenAux = NULL;
DestroyWindow();
m_pWndParent->PostMessage(WM_ENDCLICK);
}
CWnd::OnLButtonDown(nFlags, point);
}
void CTarget::Calculate()
{
double x, y;
x = m_EndPos.x - m_StartPos.x;
y = m_EndPos.y - m_StartPos.y;
//計算長度(像素)
m_iLen_p = (int)sqrt(x * x + y * y);
//計算面積(像素)
m_AreaRectangle_Pixel = abs(int(x * y));
CDC * pDC;
pDC = GetDC();
pDC->SetMapMode(MM_LOMETRIC);
POINT tmpStart = m_StartPos;
POINT tmpEnd = m_EndPos;
pDC->DPtoLP(&tmpStart);
pDC->DPtoLP(&tmpEnd);
x = tmpEnd.x - tmpStart.x;
y = tmpEnd.y - tmpStart.y;
//計算長度(0.1毫米)
m_dLen_m = sqrt(x * x + y * y);
//計算面積(0.01平方毫米)
m_AreaRectangle_MM = fabs(x*y);
pDC->SetMapMode(MM_LOENGLISH);
tmpStart = m_StartPos;
tmpEnd = m_EndPos;
pDC->DPtoLP(&tmpStart);
pDC->DPtoLP(&tmpEnd);
x = tmpEnd.x - tmpStart.x;
y = tmpEnd.y - tmpStart.y;
//計算長度(0.01inch)
m_dLen_i = sqrt(x * x + y * y);
//計算面積(0.0001平方inch)
m_AreaRectangle_Inch = fabs(x*y);
ReleaseDC(pDC);
}
void CTarget::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC;
pDC = GetDC();
pDC->SetMapMode(MM_TEXT);
pDC->SelectObject(m_pPenAux);
pDC->BitBlt(0, 0, m_cxScreen, m_cyScreen, &m_MemDC, 0, 0, SRCCOPY);
pDC->MoveTo(0, point.y);
pDC->LineTo(m_cxScreen, point.y);
pDC->MoveTo(point.x, 0);
pDC->LineTo(point.x, m_cyScreen);
//顯示當(dāng)前坐標(biāo)
pDC->SetTextColor(m_colorFont);
CString strCord;
//如果已點擊過一次,再顯示距離
if (m_bSecond)
{
double x, y;
x = point.x - m_StartPos.x;
y = point.y - m_StartPos.y;
int iDistance = (int)sqrt(x * x + y * y);
strCord.Format("當(dāng)前:%d,%d 距離:%d",point.x,point.y,iDistance);
}
else
strCord.Format("當(dāng)前:%d,%d",point.x,point.y);
//輸出
CSize sizeCordWidth = pDC->GetOutputTextExtent(strCord);
int iOffsetX = 0, iOffsetY = 0;
//移到邊界時,調(diào)整顯示位置
if((m_cxScreen - point.x) <= (sizeCordWidth.cx + 25))
iOffsetX = -(sizeCordWidth.cx + 40);
if ((m_cyScreen - point.y) <= (sizeCordWidth.cy + 25))
iOffsetY = -(sizeCordWidth.cy + 40);
pDC->TextOut(point.x + 20 + iOffsetX, point.y + 20 + iOffsetY, strCord);
if (m_bSecond)
{
pDC->MoveTo(m_StartPos.x, m_StartPos.y);
pDC->LineTo(point.x, point.y);
}
ReleaseDC(pDC);
CWnd::OnMouseMove(nFlags, point);
}
RECT CTarget::GetPoints()
{
//以RECT結(jié)構(gòu)返回兩點(起點、終點)坐標(biāo)
//左上角為起點,右下角為終點
////////////////////////////////////////
//rect.left 起點橫坐標(biāo)
//rect.top 起點縱坐標(biāo)
//rect.right 終點橫坐標(biāo)
//rect.bottom 終點縱坐標(biāo)
////////////////////////////////////////
RECT rect;
rect.left = m_StartPos.x;
rect.top = m_StartPos.y;
rect.right = m_EndPos.x;
rect.bottom = m_EndPos.y;
return rect;
}
int CTarget::GetLength_Pixel()
{
//返回以像素為單位的長度
return m_iLen_p;
}
double CTarget::GetLength_MM()
{
//返回以毫米為單位的長度
return m_dLen_m / 10;
}
double CTarget::GetLength_Inch()
{
//返回以英寸為單位的長度
return m_dLen_i / 100;
}
void CTarget::SetColor(COLORREF Aux, COLORREF Start, COLORREF Font)
{
//設(shè)置參照物的顏色
m_colorAux = Aux; //輔助線
m_colorFont = Font; //坐標(biāo)字
m_colorStart = Start; //起點標(biāo)志
}
double CTarget::GetArea_Rectangle_MM()
{
/////////////////////////////////////////
//返回兩點間圍成矩形的面積
//單位:平方毫米
/////////////////////////////////////////
return m_AreaRectangle_MM / 100;
}
double CTarget::GetArea_Rectangle_Inch()
{
/////////////////////////////////////////
//返回兩點間圍成矩形的面積
//單位:平方inch
/////////////////////////////////////////
return m_AreaRectangle_Inch / 10000;
}
int CTarget::GetArea_Rectangle_Pixel()
{
/////////////////////////////////////////
//返回兩點間圍成矩形的面積
//單位:像素數(shù)
/////////////////////////////////////////
return m_AreaRectangle_Pixel;
}
int CTarget::GetArea_Circle_Pixel()
{
/////////////////////////////////////////
//返回以起點為圓心,長度為半徑的圓形面積
//單位:像素數(shù)
/////////////////////////////////////////
return int(2 * PI * m_iLen_p * m_iLen_p);
}
double CTarget::GetArea_Circle_MM()
{
/////////////////////////////////////////
//返回以起點為圓心,長度為半徑的圓形面積
//單位:平方毫米
/////////////////////////////////////////
return 2 * PI * m_dLen_m * m_dLen_m /100;
}
double CTarget::GetArea_Circle_Inch()
{
/////////////////////////////////////////
//返回以起點為圓心,長度為半徑的圓形面積
//單位:平方Inch
/////////////////////////////////////////
return 2 * PI * m_dLen_i * m_dLen_i /10000;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -