📄 myworkview.cpp
字号:
// MyWorkView.cpp : implementation of the CMyWorkView class
//
#include "stdafx.h"
#include "MyWork.h"
#include "MyWorkDoc.h"
#include "MyWorkView.h"
#include "SettingDlg.h"
#include "MainFrm.h"
#include "Text.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyWorkView
IMPLEMENT_DYNCREATE(CMyWorkView, CView)
BEGIN_MESSAGE_MAP(CMyWorkView, CView)
//{{AFX_MSG_MAP(CMyWorkView)
ON_COMMAND(IDM_DOT, OnDot)
ON_COMMAND(IDM_LINE, OnLine)
ON_COMMAND(IDM_RECTANGLE, OnRectangle)
ON_COMMAND(IDM_ELLIPSE, OnEllipse)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_COMMAND(IDM_SETTING, OnSetting)
ON_COMMAND(IDM_COLOR, OnColor)
ON_COMMAND(IDM_RECT, OnRect)
ON_COMMAND(IDM_SMALLRECT, OnSmallrect)
ON_COMMAND(IDM_SQUARE, OnSquare)
ON_COMMAND(IDM_FILE_WRITE, OnFileWrite)
ON_COMMAND(IDM_FILE_READ, OnFileRead)
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_COMMAND(IDM_DRAW_POLYGON, OnDrawPolygon)
ON_COMMAND(IDM_TEXT, OnText)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMyWorkView construction/destruction
CMyWorkView::CMyWorkView()
{
// TODO: add construction code here
m_nDrawType=0;
m_Drag=0;
m_Ploygon=new CPoint[POINTSNUM];
m_numPloy=0;
m_ptOrigin=0;
m_nLineWidth=0;
m_nLineStyle=0;
m_clr=RGB(255,0,0);
}
CMyWorkView::~CMyWorkView()
{
delete [] m_Ploygon;
}
BOOL CMyWorkView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyWorkView drawing
void CMyWorkView::OnDraw(CDC* pDC)
{
CMyWorkDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CClientDC dc(this);
for(int k=0;k<pDoc->m_nTriaNum;k++)
{
int x=pDoc->m_Tria[k] [0];
int y=pDoc->m_Tria[k] [1];
CPoint array[3];
array[0]=CPoint(x,y-20);
array[1]=CPoint(x+17.32,y+10);
array[2]=CPoint(x-17.32,y+10);
CPen pen(m_nLineStyle,m_nLineWidth,pDoc->m_Tria[k][2]);
CPen * poldpen=dc.SelectObject(&pen);
CBrush Brush(pDoc->m_Tria[k][3]);
CBrush *poldbrush=dc.SelectObject(&Brush);
dc.Polygon(array,3);
}
for(int i=0;i<pDoc->m_nPointNum;i++)
{
int x=pDoc->m_Point[i] [0];
int y=pDoc->m_Point[i] [1];
CPen pen(m_nLineStyle,m_nLineWidth,pDoc->m_Point[i][2]);
CPen * poldpen=dc.SelectObject(&pen);
CBrush Brush(pDoc->m_Point[i][3]);
CBrush *poldbrush=dc.SelectObject(&Brush);
dc.Ellipse(x-14.14,y-14.14,x+14.14,y+14.14);
}
for(int j=0;j<pDoc->m_nRectNum;j++)
{
int x=pDoc->m_Rect[j] [0];
int y=pDoc->m_Rect[j] [1];
CPen pen(m_nLineStyle,m_nLineWidth,pDoc->m_Rect[j][2]);
CPen * poldpen=dc.SelectObject(&pen);
CBrush Brush(pDoc->m_Rect[j][3]);
CBrush *poldbrush=dc.SelectObject(&Brush);
dc.Rectangle(x-12,y-12,x+12,y+12);
}
for(int m=0;m<pDoc->m_PixelNum;m++)
{
int x=pDoc->m_Pixel[m] [0];
int y=pDoc->m_Pixel[m] [1];
CPen pen(m_nLineStyle,m_nLineWidth,pDoc->m_Pixel[m][2]);
CPen * poldpen=dc.SelectObject(&pen);
dc.SetPixel(CPoint(x,y),pDoc->m_Pixel[m][2]);
}
for(int n=0;n<pDoc->m_nLineNum;n++)
{
int x1=pDoc->m_Line[n] [0];
int y1=pDoc->m_Line[n] [1];
int x2=pDoc->m_Line[n] [2];
int y2=pDoc->m_Line[n] [3];
CPen pen(pDoc->m_Line[n] [5],pDoc->m_Line[n] [6],pDoc->m_Line[n][4]);
CPen * poldpen=dc.SelectObject(&pen);
dc.MoveTo(CPoint(x1,y1));
dc.LineTo(CPoint(x2,y2));
}
for(int p=0;p<pDoc->m_nRectangleNum;p++)
{
int x3=pDoc->m_Rectangle[p] [0];
int y3=pDoc->m_Rectangle[p] [1];
int x4=pDoc->m_Rectangle[p] [2];
int y4=pDoc->m_Rectangle[p] [3];
CPen pen(pDoc->m_Rectangle[p] [5],pDoc->m_Rectangle[p] [6],pDoc->m_Rectangle[p][4]);
CPen * poldpen=dc.SelectObject(&pen);
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.SelectObject(pBrush);
dc.Rectangle(CRect(CPoint(x3,y3),CPoint(x4,y4)));
}
for(int q=0;q<pDoc->m_nEllipseNum;q++)
{
int x5=pDoc->m_Ellipse[q][0];
int y5=pDoc->m_Ellipse[q][1];
int x6=pDoc->m_Ellipse[q][2];
int y6=pDoc->m_Ellipse[q][3];
CPen pen(pDoc->m_Ellipse[q][5],pDoc->m_Ellipse[q][6],pDoc->m_Ellipse[q][4]);
CPen * poldpen=dc.SelectObject(&pen);
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.SelectObject(pBrush);
dc.Ellipse(x5,y5,x6,y6);
}
for(int r=0;r<pDoc->m_nTextNum;r++)
{
int text_x=pDoc->m_Text[r][0];
int text_y=pDoc->m_Text[r][1];
dc.TextOut(text_x,text_y,pDoc->m_TextStr[r][0]);
}
pDoc->DrawPloygon(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// CMyWorkView printing
BOOL CMyWorkView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyWorkView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyWorkView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyWorkView diagnostics
#ifdef _DEBUG
void CMyWorkView::AssertValid() const
{
CView::AssertValid();
}
void CMyWorkView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyWorkDoc* CMyWorkView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyWorkDoc)));
return (CMyWorkDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyWorkView message handlers
void CMyWorkView::OnDot()
{
// TODO: Add your command handler code here
m_nDrawType=POINT;
}
void CMyWorkView::OnLine()
{
// TODO: Add your command handler code here
m_nDrawType=LINE;
}
void CMyWorkView::OnRectangle()
{
// TODO: Add your command handler code here
m_nDrawType=RECTANGLE;
}
void CMyWorkView::OnEllipse()
{
// TODO: Add your command handler code here
m_nDrawType=ELLIPSE;
}
void CMyWorkView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMyWorkDoc* pDoc=GetDocument();
CClientDC dc(this);
CPen pen(m_nLineStyle,m_nLineWidth,m_clr);
CPen * poldpen=dc.SelectObject(&pen);
if(m_nDrawType==TRIANGLE)
{
CPoint array[3];
CBrush Brush(pDoc->m_TriaBrushClr);
CBrush *poldbrush=dc.SelectObject(&Brush);
array[0]=CPoint(point.x,point.y-20);
array[1]=CPoint(point.x+17.32,point.y+10);
array[2]=CPoint(point.x-17.32,point.y+10);
dc.Polygon(array,3);
int i=pDoc->m_nTriaNum;
pDoc->m_Tria[i][0]=point.x;
pDoc->m_Tria[i][1]=point.y;
pDoc->m_Tria[i][2]=m_clr;
pDoc->m_Tria[i][3]=pDoc->m_TriaBrushClr;
pDoc->m_nTriaNum++;
dc.SelectObject(poldbrush);
}
else if(m_nDrawType==CIRCLE)
{
CBrush Brush(pDoc->m_BrushClr);
CBrush *poldbrush=dc.SelectObject(&Brush);
dc.Ellipse(point.x-14.14,point.y-14.14,point.x+14.14,point.y+14.14);
int i=pDoc->m_nPointNum;
pDoc->m_Point[i][0]=point.x;
pDoc->m_Point[i][1]=point.y;
pDoc->m_Point[i][2]=m_clr;
pDoc->m_Point[i][3]=pDoc->m_BrushClr;
pDoc->m_nPointNum++;
dc.SelectObject(poldbrush);
}
else if(m_nDrawType==SQUARE)
{
CBrush Brush(pDoc->m_RectBrushClr);
CBrush *poldbrush=dc.SelectObject(&Brush);
dc.Rectangle(point.x-12,point.y-12,point.x+12,point.y+12);
int i=pDoc->m_nRectNum;
pDoc->m_Rect[i][0]=point.x;
pDoc->m_Rect[i][1]=point.y;
pDoc->m_Rect[i][2]=m_clr;
pDoc->m_Rect[i][3]=pDoc->m_RectBrushClr;
pDoc->m_nRectNum++;
dc.SelectObject(poldbrush);
}
else if(m_nDrawType==LINE)
{
m_ptOrigin=point;
int i=pDoc->m_nLineNum;
pDoc->m_Line[i][0]=m_ptOrigin.x;
pDoc->m_Line[i][1]=m_ptOrigin.y;
}
else if(m_nDrawType==RECTANGLE)
{
m_ptOrigin=point;
int i=pDoc->m_nRectangleNum;
pDoc->m_Rectangle[i][0]=m_ptOrigin.x;
pDoc->m_Rectangle[i][1]=m_ptOrigin.y;
}
else if(m_nDrawType==ELLIPSE)
{
m_ptOrigin=point;
int i=pDoc->m_nEllipseNum;
pDoc->m_Ellipse[i][0]=m_ptOrigin.x;
pDoc->m_Ellipse[i][1]=m_ptOrigin.y;
}
else if(m_nDrawType==POLYGON)
{
m_Ploygon[m_numPloy]=point;
m_numPloy++;
m_Drag=1;
if(m_numPloy==1)
{
SetCapture;
mPointOrign=point;//原点等于点中点
mPointOld=point; //将前一个点设为点中点的坐标
}
else
{
mPointOrign=mPointOld; //将上一个点设为原点
mPointOld=point; //将点中点设为原点
//dc.Polyline(m_Ploygon,m_numPloy);
}
}
else if(m_nDrawType==TEXT)
{
CText dlg;
TextPoint=point;
if(dlg.DoModal()==IDOK)
{
str=dlg.m_nStr;
dc.TextOut(TextPoint.x,TextPoint.y,str);
}
int i=pDoc->m_nTextNum;
pDoc->m_Text[i][0]=TextPoint.x;
pDoc->m_Text[i][1]=TextPoint.y;
pDoc->m_TextStr[i][0]=str;
pDoc->m_nTextNum++;
}
dc.SelectObject(poldpen);
CView::OnLButtonDown(nFlags, point);
}
void CMyWorkView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMyWorkDoc* pDoc=GetDocument();
CClientDC dc(this);
CPen pen(m_nLineStyle,m_nLineWidth,m_clr);
CPen * poldpen=dc.SelectObject(&pen);
if(m_nDrawType==POINT)
{
dc.SetPixel(point,m_clr);
int i=pDoc->m_PixelNum;
pDoc->m_Pixel[i][0]=point.x;
pDoc->m_Pixel[i][1]=point.y;
pDoc->m_Pixel[i][2]=m_clr;
pDoc->m_PixelNum++;
}
if(m_nDrawType==LINE)
{
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
int i=pDoc->m_nLineNum;
pDoc->m_Line[i][2]=point.x;
pDoc->m_Line[i][3]=point.y;
pDoc->m_Line[i][4]=m_clr;
pDoc->m_Line[i][5]=m_nLineStyle;
pDoc->m_Line[i][6]=m_nLineWidth;
pDoc->m_nLineNum++;
}
if(m_nDrawType==RECTANGLE)
{
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.SelectObject(pBrush);
dc.Rectangle(CRect(m_ptOrigin,point));
int i=pDoc->m_nRectangleNum;
pDoc->m_Rectangle[i][2]=point.x;
pDoc->m_Rectangle[i][3]=point.y;
pDoc->m_Rectangle[i][4]=m_clr;
pDoc->m_Rectangle[i][5]=m_nLineStyle;
pDoc->m_Rectangle[i][6]=m_nLineWidth;
pDoc->m_nRectangleNum++;
}
if(m_nDrawType==ELLIPSE)
{
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.SelectObject(pBrush);
dc.Ellipse(CRect(m_ptOrigin,point));
int i=pDoc->m_nEllipseNum;
pDoc->m_Ellipse[i][2]=point.x;
pDoc->m_Ellipse[i][3]=point.y;
pDoc->m_Ellipse[i][5]=m_nLineStyle;
pDoc->m_Ellipse[i][6]=m_nLineWidth;
pDoc->m_Ellipse[i][4]=m_clr;
pDoc->m_nEllipseNum++;
}
dc.SelectObject(poldpen);
CView::OnLButtonUp(nFlags, point);
}
void CMyWorkView::OnSetting()
{
// TODO: Add your command handler code here
CMyWorkDoc* pDoc=GetDocument();
CSettingDlg dlg;
dlg.m_nLineWidth=m_nLineWidth;
dlg.m_nLineStyle=m_nLineStyle;
dlg.m_clr=m_clr;
if(IDOK==dlg.DoModal())
{
m_nLineWidth=dlg.m_nLineWidth;
m_nLineStyle=dlg.m_nLineStyle;
m_clr=dlg.m_clr;
}
}
void CMyWorkView::OnColor()
{
// TODO: Add your command handler code here
CColorDialog dlg;
dlg.m_cc.Flags|=CC_RGBINIT | CC_FULLOPEN;
dlg.m_cc.rgbResult=m_clr;
if(IDOK==dlg.DoModal())
{
m_clr=dlg.m_cc.rgbResult;
}
}
void CMyWorkView::OnRect()
{
// TODO: Add your command handler code here
m_nDrawType=TRIANGLE;
}
void CMyWorkView::OnSmallrect()
{
// TODO: Add your command handler code here
m_nDrawType=CIRCLE;
}
void CMyWorkView::OnSquare()
{
// TODO: Add your command handler code here
m_nDrawType=SQUARE;
}
void CMyWorkView::OnFileWrite()
{
// TODO: Add your command handler code here
CFile file("1.txt",CFile::modeCreate | CFile::modeWrite);
CArchive ar(&file,CArchive::store);
CString str("郭老师您好,我的学号是11404327!");
ar<<str;
}
void CMyWorkView::OnFileRead()
{
// TODO: Add your command handler code here
CFile file("1.txt",CFile::modeRead);
CArchive ar(&file,CArchive::load);
CString str;
CString strResult;
ar>>str;
strResult.Format("%s",str);
MessageBox(strResult);
}
void CMyWorkView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
CString str;
str.Format("x=%d,y=%d",point.x,point.y);
((CMainFrame*)GetParent())->SetMessageText(str);
if(m_nDrawType==POLYGON)
{
if(m_Drag)
{
dc.SetROP2(R2_NOT);//设置线条反转模式
if(mPointOld!=point)//如果鼠标的现在移动点与上一个点不相同
//将上一个顶点到上一个移动点的直线擦除,绘制从上一个顶点到移动点的直线
{
dc.MoveTo(mPointOrign);
dc.LineTo(mPointOld); //擦除原来的直线
dc.MoveTo(mPointOrign);
dc.LineTo(point); //重画移动到点的直线
mPointOld=point;
}
}
}
CView::OnMouseMove(nFlags, point);
}
void CMyWorkView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
CMyWorkDoc *pDoc=GetDocument();
int i=m_numPloy;
if(m_nDrawType==POLYGON)
{
if(m_numPloy==2)
{
MessageBox("多边形的顶点至少要三个,请重新绘制!","友情提示");
dc.MoveTo(m_Ploygon[0].x,m_Ploygon[0].y);
dc.LineTo(m_Ploygon[1].x,m_Ploygon[1].y);
}
else
{
pDoc->AddPloygon(m_Ploygon,m_numPloy);
if(m_Drag)
{
m_Drag=0;
ClipCursor(NULL);
}
m_numPloy=0;
}
Invalidate();
}
CView::OnRButtonDown(nFlags, point);
}
void CMyWorkView::OnDrawPolygon()
{
// TODO: Add your command handler code here
m_nDrawType=POLYGON;
}
void CMyWorkView::OnText()
{
// TODO: Add your command handler code here
m_nDrawType=TEXT;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -