📄 ex07_2view.cpp
字号:
// Ex07_2View.cpp : implementation of the CEx07_2View class
//
#include "stdafx.h"
#include "Ex07_2.h"
#include "Ex07_2Doc.h"
#include "Ex07_2View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View
IMPLEMENT_DYNCREATE(CEx07_2View, CScrollView)
BEGIN_MESSAGE_MAP(CEx07_2View, CScrollView)
//{{AFX_MSG_MAP(CEx07_2View)
ON_COMMAND(ID_MDRAW_LINE, OnMdrawLine)
ON_COMMAND(ID_MDRAW_RECTANGLE, OnMdrawRectangle)
ON_COMMAND(ID_MDRAW_ELLIPSE, OnMdrawEllipse)
ON_COMMAND(ID_MDRAW_ANY, OnMdrawAny)
ON_UPDATE_COMMAND_UI(ID_MDRAW_LINE, OnUpdateMdrawLine)
ON_UPDATE_COMMAND_UI(ID_MDRAW_RECTANGLE, OnUpdateMdrawRectangle)
ON_UPDATE_COMMAND_UI(ID_MDRAW_ELLIPSE, OnUpdateMdrawEllipse)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_CHAR()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View construction/destruction
CEx07_2View::CEx07_2View()
{
// TODO: add construction code here
m_DrawType=1;
m_bMouseDown=false;
m_hCursor=AfxGetApp()->LoadStandardCursor(IDC_CROSS);
m_nLine=0;
}
CEx07_2View::~CEx07_2View()
{
}
BOOL CEx07_2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View drawing
void CEx07_2View::OnDraw(CDC* pDC)
{
CEx07_2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDoc->DrawLine(pDC);
pDoc->DrawText(pDC);
}
void CEx07_2View::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View printing
BOOL CEx07_2View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEx07_2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEx07_2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View diagnostics
#ifdef _DEBUG
void CEx07_2View::AssertValid() const
{
CScrollView::AssertValid();
}
void CEx07_2View::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CEx07_2Doc* CEx07_2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx07_2Doc)));
return (CEx07_2Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View message handlers
void CEx07_2View::OnMdrawLine()
{
// TODO: Add your command handler code here
m_DrawType=1;
}
void CEx07_2View::OnMdrawRectangle()
{
// TODO: Add your command handler code here
m_DrawType=2;
}
void CEx07_2View::OnMdrawEllipse()
{
// TODO: Add your command handler code here
m_DrawType=3;
}
void CEx07_2View::OnMdrawAny()
{
// TODO: Add your command handler code here
m_DrawType=4;
}
void CEx07_2View::OnUpdateMdrawLine(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_DrawType==1);
}
void CEx07_2View::OnUpdateMdrawRectangle(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_DrawType==2);
}
void CEx07_2View::OnUpdateMdrawEllipse(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_DrawType==3);
}
///鼠标消息函数
void CEx07_2View::OnLButtonDown(UINT nFlags, CPoint point) //鼠标按下
{
// TODO: Add your message handler code here and/or call default
m_bMouseDown=true;
m_StartPt=point;//存放画线的起始位置
m_OldPt=point;//存放光标的当前位置
SetCapture();//捕获鼠标
CRect rect;
GetClientRect(&rect);//获取视图窗口客户区的坐标
ClientToScreen(&rect);//转换客户区坐标为屏幕坐标
ClipCursor(&rect);//将光标限定在视图窗口客户区内
CScrollView::OnLButtonDown(nFlags, point);
}
void CEx07_2View::OnMouseMove(UINT nFlags, CPoint point) //鼠标移动
{
// TODO: Add your message handler code here and/or call default
SetCursor(m_hCursor);//设置鼠标形状为十字形
if(m_bMouseDown)
{
CClientDC dc(this);
switch(m_DrawType)
{
case 1:
dc.SetROP2(R2_NOT);
//以下两行代码擦除从起点(鼠标按下点)到赏赐鼠标移动到的位置之间的临时线
dc.MoveTo(m_StartPt);
dc.LineTo(m_OldPt);
// //以下两行代码玛从起点到鼠标当前位置画线
dc.MoveTo(m_StartPt);
dc.LineTo(point);
break;
case 2:
dc.SetROP2(R2_NOTXORPEN );
dc.Rectangle(m_StartPt.x,m_StartPt.y,m_OldPt.x,m_OldPt.y);
dc.Rectangle(m_StartPt.x,m_StartPt.y,point.x,point.y);
break;
case 3:
dc.SetROP2(R2_NOTXORPEN );
dc.Ellipse(m_StartPt.x,m_StartPt.y,m_OldPt.x,m_OldPt.y);
dc.Ellipse(m_StartPt.x,m_StartPt.y,point.x,point.y);
break;
}
m_OldPt=point;//鼠标当前位置为下一次移动的旧位置
}
CScrollView::OnMouseMove(nFlags, point);
}
void CEx07_2View::OnLButtonUp(UINT nFlags, CPoint point) //释放鼠标
{
// TODO: Add your message handler code here and/or call default
if(m_bMouseDown)
{
m_bMouseDown=false;
ReleaseCapture();
ClipCursor(NULL);
CClientDC dc(this);
switch(m_DrawType)
{
case 1:
dc.SetROP2(R2_NOT);
dc.MoveTo(m_StartPt);
dc.LineTo(m_OldPt);
dc.SetROP2(R2_COPYPEN);
dc.MoveTo(m_StartPt);
dc.LineTo(point);
break;
case 2:
dc.SetROP2(R2_NOT);
dc.Rectangle(m_StartPt.x,m_StartPt.y,m_OldPt.x,m_OldPt.y);
dc.SetROP2(R2_COPYPEN);
dc.Rectangle(m_StartPt.x,m_StartPt.y,point.x,point.y);
break;
case 3:
dc.SetROP2(R2_NOT);
dc.Ellipse(m_StartPt.x,m_StartPt.y,m_OldPt.x,m_OldPt.y);
dc.SetROP2(R2_COPYPEN);
dc.Ellipse(m_StartPt.x,m_StartPt.y,point.x,point.y);
break;
}
//把信息添加到文档类数据成员m_LineList中
CEx07_2Doc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
CLine*pLine=new CLine(m_StartPt,point,m_DrawType);
pDoc->m_LineList.AddTail((void*)pLine);
}
CScrollView::OnLButtonUp(nFlags, point);
}
void CEx07_2View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CEx07_2Doc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
if(nChar==VK_RETURN)//回车
{
pDoc->m_strList.AddTail(m_strShow);
m_strShow.Empty();
pDoc->m_strLastLine.Empty();
m_nLine++;
}
else if(m_strShow.GetLength()<64)
{
pDoc->m_strLastLine=m_strShow;
m_strShow+=nChar;
}
CClientDC dc(this);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
int nLineHeight=tm.tmHeight+tm.tmExternalLeading;
dc.TextOut(0,m_nLine*nLineHeight,m_strShow);
CScrollView::OnChar(nChar, nRepCnt, nFlags);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -