📄 ex_a7view.cpp
字号:
// Ex_A7View.cpp : implementation of the CEx_A7View class
//
#include "stdafx.h"
#include "Ex_A7.h"
#include "Ex_A7Doc.h"
#include "Ex_A7View.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEx_A7View
IMPLEMENT_DYNCREATE(CEx_A7View, CView)
BEGIN_MESSAGE_MAP(CEx_A7View, CView)
//{{AFX_MSG_MAP(CEx_A7View)
ON_WM_MOUSEMOVE()
ON_WM_SETCURSOR()
ON_WM_LBUTTONDOWN()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEx_A7View construction/destruction
CEx_A7View::CEx_A7View()
{
m_oldBmp = NULL;
}
CEx_A7View::~CEx_A7View()
{
}
BOOL CEx_A7View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CEx_A7View drawing
void CEx_A7View::OnDraw(CDC* pDC)
{
CEx_A7Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rcClient;
GetClientRect( rcClient ); // 获取客户区大小
// 清除内存
if (m_oldBmp) m_dcMem.SelectObject( m_oldBmp ); // 恢复原来位图
m_dcMem.DeleteDC();
m_dcMem.CreateCompatibleDC( pDC ); // 用视图设备参数重建内存环境
CBitmap bmp;
bmp.CreateCompatibleBitmap( pDC, rcClient.Width(), rcClient.Height() );
m_oldBmp = m_dcMem.SelectObject( &bmp ); // 通过选入位图构造实际内存环境
bmp.DeleteObject();
m_dcMem.FillSolidRect( rcClient, RGB(0,0,0) );
int nBkMode = m_dcMem.SetBkMode( TRANSPARENT ); // 设置透明背景模式
CPen pen( PS_SOLID, 3, RGB( 0, 192, 0 ) );
CPen *oldPen = m_dcMem.SelectObject( &pen );
for ( int i = 0; i < m_strCmdArray.GetSize(); i++ )
{
CString str = (CString)m_strCmdArray[i];
// 解析命令
CString strCmd;
int nIndex = str.Find(",");
strCmd = str.Left( nIndex );
strCmd.TrimLeft(); strCmd.TrimRight();
CString strData = str.Mid( nIndex + 1 );
int x,y;
nIndex = strData.Find(",");
x = atol( strData.Left( nIndex ) );
y = atol( strData.Mid( nIndex + 1 ) );
if ( strCmd == "MOVETO" ) m_dcMem.MoveTo( x, y );
if ( strCmd == "LINETO" ) m_dcMem.LineTo( x, y );
}
m_dcMem.SelectObject( oldPen );
pDC->BitBlt( 0, 0, rcClient.Width(), rcClient.Height(), &m_dcMem, 0, 0, SRCCOPY );
m_dcMem.SetBkMode( nBkMode );
m_bCursorFirst = TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CEx_A7View printing
BOOL CEx_A7View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEx_A7View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEx_A7View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEx_A7View diagnostics
#ifdef _DEBUG
void CEx_A7View::AssertValid() const
{
CView::AssertValid();
}
void CEx_A7View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CEx_A7Doc* CEx_A7View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx_A7Doc)));
return (CEx_A7Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEx_A7View message handlers
void CEx_A7View::DrawCursor(CDC *pDC, CPoint pt)
{
CRect rcClip;
pDC->GetClipBox( rcClip ); // 当前裁剪区大小
CPen pen( PS_SOLID, 1, RGB( 128, 128, 128 ) ); // 灰色画笔
CPen *oldPen = pDC->SelectObject( &pen ); // 选入画笔
// 设置XOR光栅操作模式
int nOldROP = pDC->SetROP2( R2_XORPEN );
// 绘制水平线
pDC->MoveTo( rcClip.left, pt.y ); pDC->LineTo( rcClip.right, pt.y );
//绘制垂直线
pDC->MoveTo( pt.x, rcClip.top ); pDC->LineTo( pt.x, rcClip.bottom );
// 恢复原来的光栅模式
pDC->SetROP2( nOldROP );
// 恢复原来的画笔
pDC->SelectObject( oldPen );
}
void CEx_A7View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC = this->GetDC();
CString strPos;
if (m_bLineStart )
{
// 计算长度和角度
int dx = point.x - m_ptPrevPos.x;
int dy = point.y - m_ptPrevPos.y;
double length = sqrt( dx * dx + dy * dy );
double angle;
if (dx == 0 ) angle = 90.0;
else if ( dy == 0 ) angle = 0.0;
else angle = 180.0 * atan( (double)dy / (double)dx ) / 3.14159265;
strPos.Format( "%.1f<%.1f", length, angle );
} else
strPos.Format( "%d, %d", point.x, point.y );
if ( m_bCursorFirst )
{
m_bCursorFirst = FALSE;
m_ptCurPos = point;
DrawCursor( pDC, m_ptCurPos );
DrawInfoWnd( pDC, m_ptCurPos, strPos );
} else
{
DrawCursor( pDC, m_ptCurPos );
if (m_bLineStart ) // 绘制XOR直线
DrawXORLine( pDC, m_ptPrevPos, m_ptCurPos );
if ( m_dcMem.m_hDC )
{
pDC->BitBlt( m_ptCurPos.x, m_ptCurPos.y - 15, 80, 15,
&m_dcMem, m_ptCurPos.x, m_ptCurPos.y - 15, SRCCOPY );
}
m_ptCurPos = point;
if (m_bLineStart ) // 绘制XOR直线
DrawXORLine( pDC, m_ptPrevPos, m_ptCurPos );
DrawCursor( pDC, m_ptCurPos );
DrawInfoWnd( pDC, m_ptCurPos, strPos );
}
CView::OnMouseMove(nFlags, point);
}
BOOL CEx_A7View::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
SetCursor( NULL );
return TRUE;//CView::OnSetCursor(pWnd, nHitTest, message);
}
void CEx_A7View::DrawInfoWnd(CDC *pDC, CPoint pt, CString strInfo)
{
CRect rcWnd( pt.x, pt.y - 15, pt.x + 80, pt.y );
pDC->FillSolidRect( rcWnd, RGB( 0, 0, 128 ) );
pDC->Draw3dRect( rcWnd, RGB(255,255,255), RGB(255,255,255) );
// 定义并设置文本字体
LOGFONT lfText;
memset(&lfText, 0, sizeof(LOGFONT));
lfText.lfHeight = -12;
lfText.lfCharSet = GB2312_CHARSET;
strcpy(lfText.lfFaceName, "宋体");
CFont cf;
cf.CreateFontIndirect(&lfText);
CFont *oldFont = pDC->SelectObject( &cf );
pDC->SetBkColor( RGB( 0, 0, 128 ) );
pDC->SetTextColor( RGB( 192, 192, 0 ) );
pDC->DrawText( strInfo, rcWnd, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
pDC->SelectObject( oldFont );
cf.DeleteObject(); // 删除字体对象
}
void CEx_A7View::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
m_strCmdArray.RemoveAll(); // 清空命令
m_bLineStart = FALSE;
}
void CEx_A7View::DrawPointFlag(CDC *pDC, CPoint pt)
{
CPen *oldPen = (CPen *)pDC->SelectStockObject( WHITE_PEN ); // 选入白色系统画笔
// 绘制水平线
pDC->MoveTo( pt.x - 4, pt.y ); pDC->LineTo( pt.x + 4, pt.y );
//绘制垂直线
pDC->MoveTo( pt.x, pt.y - 4 ); pDC->LineTo( pt.x, pt.y + 4 );
// 恢复原来的画笔
pDC->SelectObject( oldPen );
}
void CEx_A7View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_ptPrevPos = point;
if ( !m_bLineStart )
{
m_bLineStart = TRUE;
// 创建并添加命令
CString strCmd;
strCmd.Format( "MOVETO,%d,%d", point.x, point.y );
m_strCmdArray.Add( strCmd );
} else
{
CString strCmd;
strCmd.Format( "LINETO,%d,%d", point.x, point.y );
m_strCmdArray.Add( strCmd );
}
DrawCursor( GetDC(), point );
DrawPointFlag( GetDC(), point );
DrawCursor( GetDC(), point );
DrawPointFlag( &m_dcMem, point );
CView::OnLButtonDown(nFlags, point);
}
void CEx_A7View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if ( nChar == VK_ESCAPE )
{
if ( m_bLineStart )
{
m_bLineStart = FALSE;
Invalidate(); // 强制更新视图
}
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CEx_A7View::DrawXORLine(CDC *pDC, CPoint pt1, CPoint pt2)
{
CPen pen( PS_SOLID, 1, RGB( 192, 192, 128 ) ); // 淡黄色画笔
CPen *oldPen = pDC->SelectObject( &pen ); // 选入画笔
int nOldROP = pDC->SetROP2( R2_XORPEN );
pDC->MoveTo( pt1 ); pDC->LineTo( pt2 );
pDC->SetROP2( nOldROP );
pDC->SelectObject( oldPen );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -