📄 drawview.cpp
字号:
// DrawView.cpp : implementation of the CDrawView class
//
#include "stdafx.h"
#include "Draw.h"
#include "DrawDoc.h"
#include "DrawView.h"
#include "MainFrm.h"
#include "LineStyle.h"
#include "LineWid.h"
#include "LineColor.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawView
IMPLEMENT_DYNCREATE(CDrawView, CView)
BEGIN_MESSAGE_MAP(CDrawView, CView)
//{{AFX_MSG_MAP(CDrawView)
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
ON_UPDATE_COMMAND_UI(ID_DRAW_LINE, OnUpdateDrawLine)
ON_COMMAND(ID_DRAW_POINT, OnDrawPoint)
ON_UPDATE_COMMAND_UI(ID_DRAW_POINT, OnUpdateDrawPoint)
ON_COMMAND(ID_DRAW_RECTANGLE, OnDrawRectangle)
ON_UPDATE_COMMAND_UI(ID_DRAW_RECTANGLE, OnUpdateDrawRectangle)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_SETCURSOR()
ON_COMMAND(ID_DRAG, OnDrag)
ON_UPDATE_COMMAND_UI(ID_DRAG, OnUpdateDrag)
ON_COMMAND(ID_SET_LINETYPE, OnSetLinetype)
ON_COMMAND(ID_SET_LINEWIDTH, OnSetLinewidth)
ON_COMMAND(ID_SET_COLOR, OnSetColor)
ON_COMMAND(ID_MOVE, OnMove)
ON_UPDATE_COMMAND_UI(ID_MOVE, OnUpdateMove)
ON_MESSAGE(WM_MYMESSAGE,OnMyMessage)
ON_UPDATE_COMMAND_UI(ID_SET_LINEWIDTH, OnUpdateSetLinewidth)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CDrawView construction/destruction
CDrawView::CDrawView()
{
// TODO: add construction code here
step=0;
start.x=0;
start.y=0;
end.x=0;
end.y=0;
m_point=FALSE;
m_line=FALSE;
m_rectangle=FALSE;
drag=FALSE;
move=FALSE;
m_bLBtnDown=FALSE;
}
CDrawView::~CDrawView()
{
}
BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView drawing
void CDrawView::OnDraw(CDC* pDC)
{
CDrawDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CPen newpen,*oldpen;
newpen.CreatePen(pDoc->linestyle,pDoc->linewidth,pDoc->linecolor);
oldpen=pDC->SelectObject(&newpen);
CBrush newbrush,*oldbrush;
newbrush.CreateHatchBrush(pDoc->brushstyle,pDoc->linecolor);
oldbrush=pDC->SelectObject(&newbrush);
if (m_point)
{
PostMessage(WM_MYMESSAGE,0,0);
pDC->TextOut(25,48,"程序正在画点");
}
if (m_line)
{
pDC->TextOut(25,48,"程序正在画线");
}
if (m_rectangle)
{
pDC->TextOut(25,48,"程序正在画矩形");
}
for(int i=0;i<pDoc->pointnum;i++)
{
int x = pDoc->points[i][0];
int y = pDoc->points[i][1];
pDC->Ellipse(x-2, y-2, x+2, y+2);
}
for( i=0;i<pDoc->linenum;i++)
{
int x1 = pDoc->line[i][0];
int y1 = pDoc->line[i][1];
int x2 = pDoc->line[i][2];
int y2 = pDoc->line[i][3];
pDC->MoveTo(x1, y1);
pDC->LineTo(x2, y2);
}
for( i=0;i<pDoc->recnum;i++)
{
int x1 = pDoc->rec[i][0];
int y1 = pDoc->rec[i][1];
int x2 = pDoc->rec[i][2];
int y2 = pDoc->rec[i][3];
pDC->Rectangle(x1,y1,x2,y2);
}
if (move==TRUE)
{
pDC->Rectangle(rect0);
pDoc->rec[selectindex][0]=rect0.left;
pDoc->rec[selectindex][1]=rect0.top;
pDoc->rec[selectindex][2]=rect0.right;
pDoc->rec[selectindex][3]=rect0.bottom;
pDoc->rec[selectindex][4]=1;
pDoc->SetModifiedFlag();
}
pDC->SelectObject(oldbrush);
pDC->SelectObject(oldpen);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView printing
BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView diagnostics
#ifdef _DEBUG
void CDrawView::AssertValid() const
{
CView::AssertValid();
}
void CDrawView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawDoc* CDrawView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
return (CDrawDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawView message handlers
void CDrawView::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CMenu menu;
menu.LoadMenu(IDR_MENU1);
CMenu *popmenu=menu.GetSubMenu(0);
popmenu->TrackPopupMenu(TPM_RIGHTBUTTON,point.x,point.y,this);
}
void CDrawView::OnMyMessage()
{
CClientDC dc(this);
dc.TextOut(25,25,"触发了自定义消息WM_MYMESSAGE");
}
void CDrawView::OnDrawLine()
{
// TODO: Add your command handler code here
m_point=FALSE;
m_line=TRUE;
m_rectangle=FALSE;
drag=FALSE;
move=FALSE;
}
void CDrawView::OnUpdateDrawLine(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_line);
}
void CDrawView::OnDrawPoint()
{
// TODO: Add your command handler code here
m_point=TRUE;
m_line=FALSE;
m_rectangle=FALSE;
drag=FALSE;
move=FALSE;
}
void CDrawView::OnUpdateDrawPoint(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_point);
}
void CDrawView::OnDrawRectangle()
{
// TODO: Add your command handler code here
m_point=FALSE;
m_line=FALSE;
m_rectangle=TRUE;
drag=FALSE;
move=FALSE;
}
void CDrawView::OnUpdateDrawRectangle(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_rectangle);
}
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDrawDoc* pDoc = GetDocument();
if (move)
{
int x1=pDoc->rec[selectindex][0];
int y1=pDoc->rec[selectindex][1];
int x2=pDoc->rec[selectindex][2];
int y2=pDoc->rec[selectindex][3];
CRect rect(x1,y1,x2,y2);
if(rect.PtInRect(point))
{
m_bLBtnDown=TRUE;
movepoint=point;
}
if (step==0)
{
step++;
}
else
{
step=0;
move=FALSE;
}
}
CClientDC dc(this);//获取设备环境
CPen newpen(pDoc->linestyle, pDoc->linewidth, pDoc->linecolor);
CPen * poldpen = dc.SelectObject(&newpen);//将画笔选入设备环境
CBrush *br=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *oldBr=dc.SelectObject(br);
if(m_point)//如果程序处于画点状态
{
dc.Ellipse(point.x-2, point.y-2, point.x+2, point.y+2);
int i = pDoc->pointnum;
pDoc->points[i][0] = point.x; //记录点的坐标值
pDoc->points[i][1] = point.y;
pDoc->pointnum++; //点数加1
pDoc->SetModifiedFlag(); //设置文档“脏”标志
pDoc->UpdateAllViews(this); // 重画所有视图
}
if(m_line)//如果程序处于画线状态
{
if(step == 0)//如果是线段起点,则记录起点坐标
{
start.x=point.x;
start.y=point.y;
step ++;//操作步数加1
SetCapture();//捕捉鼠标
}
else//如果是线段终点,则在视图区中画出线段
{
dc.MoveTo(start);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -