#pragma once
#include "atltypes.h"
#include "afxwin.h"
enum ShapType
{
knone = 0,
kLine,
kCircle,
kEllip,
kRect
};
class CSharp
{
public:
int m_nLineWidth;
int m_nLineType;
COLORREF m_color;
virtual void Draw(CDC* du) = 0;
virtual ShapType type() { return knone; }
};
class Cline : public CSharp
{
public:
Cline()
{
}
Cline(CPoint& pts, CPoint& pte, int& LinWidth, int& LineType, COLORREF color)
{
m_ptS = pts;
m_ptE = pte;
m_nLineWidth = LinWidth;
m_nLineType = LineType;
m_color = color;
}
public:
CPoint m_ptS;
CPoint m_ptE;
virtual void Draw(CDC* du);
virtual ShapType type() { return kLine; };
};
class CCircle : public CSharp
{
public:
CCircle()
{
}
CCircle(CPoint& pts, CPoint& pte, int& LinWidth, int& LineType, COLORREF color)
{
m_ptCenter.x = (pts.x + pte.x) / 2;
m_ptCenter.y = (pts.y + pte.y) / 2;
m_iRadius = 100;
m_nLineWidth = LinWidth;
m_nLineType = LineType;
m_color = color;
}
public:
CPoint m_ptCenter;
int m_iRadius;
virtual void Draw(CDC* du);
virtual ShapType type() { return kLine; };
};
class CEllipse : public CSharp
{
public:
CEllipse()
{
}
CEllipse(CPoint& pts, CPoint& pte, int& LinWidth, int& LineType, COLORREF color)
{
m_ptS = pts;
m_ptE = pte;
m_nLineWidth = LinWidth;
m_nLineType = LineType;
m_color = color;
}
public:
CPoint m_ptS;
CPoint m_ptE;
virtual void Draw(CDC* du);
virtual ShapType type() { return kEllip; };
};
class Rectangle1 : public CSharp
{
public:
Rectangle1()
{
}
Rectangle1(CPoint& pts, CPoint& pte, int& LinWidth, int& LineType, COLORREF color)
{
m_ptS = pts;
m_ptE = pte;
m_nLineWidth = LinWidth;
m_nLineType = LineType;
m_color = color;
}
public:
CPoint m_ptS;
CPoint m_ptE;
virtual void Draw(CDC* du);
virtual ShapType type() { return kRect; };
};
CArchive& operator <<(CArchive& s, CSharp& shap);
CArchive& operator >>(CArchive& s, CSharp& shap);
CArchive& operator <<(CArchive& s, Cline& line);
CArchive& operator >>(CArchive& s, Cline& line);
CArchive& operator <<(CArchive& s, CCircle& circle);
CArchive& operator >>(CArchive& s, CCircle& circle);
CArchive& operator <<(CArchive& s, CEllipse& ellipse);
CArchive& operator >>(CArchive& s, CEllipse& ellipse);
CArchive& operator <<(CArchive& s, Rectangle1& rectangle);
CArchive& operator >>(CArchive& s, Rectangle1& rectangle);
#include "pch.h"
#include "atltypes.h"
#include "afxwin.h"
#include "CSharp.h"
void Cline::Draw(CDC* du)
{
CPen cPen;
cPen.CreatePen(m_nLineType,m_nLineWidth, m_color);
du->SelectObject(&cPen);
du->MoveTo(m_ptS);
du->LineTo(m_ptE);
};
void CCircle::Draw(CDC* du)
{
CPen cPen;
cPen.CreatePen(m_nLineType, m_nLineWidth, m_color);
du->SelectObject(&cPen);
du->Arc(m_ptCenter.x - m_iRadius, m_ptCenter.y - m_iRadius, m_ptCenter.x + m_iRadius, m_ptCenter.y + m_iRadius, 0, 0, 0, 0);
};
void CEllipse::Draw(CDC* du)
{
CPen cPen;
cPen.CreatePen(m_nLineType, m_nLineWidth, m_color);
du->SelectObject(&cPen);
du->Ellipse(m_ptS.x, m_ptS.y, m_ptE.x, m_ptE.y);
};
void Rectangle1::Draw(CDC* du)
{
CPen cPen;
cPen.CreatePen(m_nLineType, m_nLineWidth, m_color);
du->SelectObject(&cPen);
du->Rectangle(CRect(m_ptS, m_ptE));
};
CArchive& operator <<(CArchive& s, CSharp& shap)
{
s << shap.m_nLineType << shap.m_nLineWidth << shap.m_color;
return s;
}
CArchive& operator >>(CArchive& s, CSharp& shap)
{
s >> shap.m_nLineType >> shap.m_nLineWidth >> shap.m_color;
return s;
}
CArchive& operator <<(CArchive& s, Cline& line)
{
s << (CSharp&)line;
s << line.m_ptS << line.m_ptE;
return s;
}
CArchive& operator >>(CArchive& s, Cline& line)
{
s >> (CSharp&)line;
s >> line.m_ptS >> line.m_ptE;
return s;
}
CArchive& operator <<(CArchive& s, CCircle& circle)
{
s << (CSharp&)circle;
s << circle.m_ptCenter << circle.m_iRadius;
return s;
}
CArchive& operator >>(CArchive& s, CCircle& circle)
{
s >> (CSharp&)circle;
s >> circle.m_ptCenter >> circle.m_iRadius;
return s;
}
CArchive& operator <<(CArchive& s, CEllipse& ellipse)
{
s << (CSharp&)ellipse;
s << ellipse.m_ptS << ellipse.m_ptE;
return s;
}
CArchive& operator >>(CArchive& s, CEllipse& ellipse)
{
s >> (CSharp&)ellipse;
s >> ellipse.m_ptS >> ellipse.m_ptE;
return s;
}
CArchive& operator <<(CArchive& s, Rectangle1& rectangle)
{
s << (CSharp&)rectangle;
s << rectangle.m_ptS << rectangle.m_ptE;
return s;
}
CArchive& operator >>(CArchive& s, Rectangle1& rectangle)
{
s >> (CSharp&)rectangle;
s >> rectangle.m_ptS >> rectangle.m_ptE;
return s;
}
#pragma once
class GraphicAttribute : public CDialogEx
{
DECLARE_DYNAMIC(GraphicAttribute)
public:
GraphicAttribute(CWnd* pParent = nullptr);
virtual ~GraphicAttribute();
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_DIALOG_GraphicAttribute };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX);
DECLARE_MESSAGE_MAP()
public:
COLORREF m_color;
int m_nLineWidth;
int m_nLineType;
afx_msg void OnBnClickedButtonColor();
afx_msg void OnBnClickedOk();
};
#include "pch.h"
#include "TaskOne.h"
#include "GraphicAttribute.h"
#include "afxdialogex.h"
IMPLEMENT_DYNAMIC(GraphicAttribute, CDialogEx)
GraphicAttribute::GraphicAttribute(CWnd* pParent )
: CDialogEx(IDD_DIALOG_GraphicAttribute, pParent)
, m_color(0)
, m_nLineWidth(0)
, m_nLineType(0)
{
}
GraphicAttribute::~GraphicAttribute()
{
}
void GraphicAttribute::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_LineWidth, m_nLineWidth);
DDX_Text(pDX, IDC_EDIT_LineType, m_nLineType);
}
BEGIN_MESSAGE_MAP(GraphicAttribute, CDialogEx)
ON_BN_CLICKED(IDC_BUTTON_Color, &GraphicAttribute::OnBnClickedButtonColor)
ON_BN_CLICKED(IDOK, &GraphicAttribute::OnBnClickedOk)
END_MESSAGE_MAP()
void GraphicAttribute::OnBnClickedButtonColor()
{
CMFCColorDialog dlg;
if (dlg.DoModal() != IDOK)
return;
m_color = dlg.GetColor();
}
void GraphicAttribute::OnBnClickedOk()
{
CDialogEx::OnOK();
CString str;
GetDlgItem(IDC_EDIT_LineType)->GetWindowText(str);
m_nLineType = _ttoi(str);
GetDlgItem(IDC_EDIT_LineWidth)->GetWindowText(str);
m_nLineWidth = _ttoi(str);
}
#pragma once
#include "MainFrm.h"
class CTaskOneView : public CView
{
protected:
CTaskOneView() noexcept;
DECLARE_DYNCREATE(CTaskOneView)
public:
CTaskOneDoc* GetDocument() const;
public:
CPoint m_point1;
CPoint m_point2;
bool m_bSaveTag ;
int m_nLineType1;
int m_nLineWidth1 ;
COLORREF m_color1;
CString m_strSaveFilePath;
CPoint m_SavePoint[100];
int m_nDraw = 0;
CMainFrame* mainFrm;
public:
virtual void OnDraw(CDC* pDC);
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
public:
virtual ~CTaskOneView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
protected:
afx_msg void OnFilePrintPreview();
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnLine();
afx_msg void OnCircle();
afx_msg void OnEllipse();
afx_msg void OnRectangle();
afx_msg void OnColor();
afx_msg void OnLinewidth();
afx_msg void OnLinetype();
afx_msg void OnGraphicattribute();
};
#ifndef _DEBUG
inline CTaskOneDoc* CTaskOneView::GetDocument() const
{ return reinterpret_cast<CTaskOneDoc*>(m_pDocument); }
#endif
#include "pch.h"
#include "framework.h"
#ifndef SHARED_HANDLERS
#include "TaskOne.h"
#endif
#include "TaskOneDoc.h"
#include "TaskOneView.h"
#include "CSharp.h"
#include "GraphicAttribute.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CTaskOneView, CView)
BEGIN_MESSAGE_MAP(CTaskOneView, CView)
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CTaskOneView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_LINE, &CTaskOneView::OnLine)
ON_COMMAND(ID_Circle, &CTaskOneView::OnCircle)
ON_COMMAND(ID_Ellipse, &CTaskOneView::OnEllipse)
ON_COMMAND(ID_Rectangle, &CTaskOneView::OnRectangle)
ON_COMMAND(ID_GraphicAttribute, &CTaskOneView::OnGraphicattribute)
END_MESSAGE_MAP()
CTaskOneView::CTaskOneView() noexcept
{
m_bSaveTag = 0;
m_color1 = RGB(0, 0, 0);
m_nLineWidth1 = 0;
m_nDraw = -1;
m_nLineType1 = 0;
}
CTaskOneView::~CTaskOneView()
{
}
BOOL CTaskOneView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
void CTaskOneView::OnDraw(CDC* pDC)
{
CTaskOneDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
auto shaps = pDoc->m_vecShap;
int length = pDoc->m_vecShap.size();
for (int i = 0; i < length; i++)
{
pDoc->m_vecShap[i]->Draw(pDC);
}
}
void CTaskOneView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CTaskOneView::OnPreparePrinting(CPrintInfo* pInfo)
{
return DoPreparePrinting(pInfo);
}
void CTaskOneView::OnBeginPrinting(CDC* , CPrintInfo* )
{
}
void CTaskOneView::OnEndPrinting(CDC* , CPrintInfo* )
{
}
void CTaskOneView::OnRButtonUp(UINT , CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CTaskOneView::OnContextMenu(CWnd* , CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
#ifdef _DEBUG
void CTaskOneView::AssertValid() const
{
CView::AssertValid();
}
void CTaskOneView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTaskOneDoc* CTaskOneView::GetDocument() const
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTaskOneDoc)));
return (CTaskOneDoc*)m_pDocument;
}
#endif
void CTaskOneView::OnLine()
{
m_nDraw = 1;
}
void CTaskOneView::OnCircle()
{
m_nDraw = 2;
}
void CTaskOneView::OnEllipse()
{
m_nDraw = 3;
}
void CTaskOneView::OnRectangle()
{
m_nDraw = 4;
}
void CTaskOneView::OnLButtonDown(UINT nFlags, CPoint point)
{
CView::OnLButtonDown(nFlags, point);
m_point1 = point;
}
void CTaskOneView::OnLButtonUp(UINT nFlags, CPoint point)
{
CDC* pDc = GetDC();
CView::OnLButtonUp(nFlags, point);
m_point2 = point;
CSharp* pShrp = NULL;
if (m_nDraw == 1)
{
pShrp = new Cline(m_point1, m_point2, m_nLineWidth1, m_nLineType1, m_color1);
pShrp->Draw(pDc);
}
else if (m_nDraw == 2)
{
pShrp = new CCircle(m_point1, m_point2, m_nLineWidth1, m_nLineType1, m_color1);
pShrp->Draw(pDc);
}
else if (m_nDraw == 3)
{
pShrp = new CEllipse(m_point1, m_point2, m_nLineWidth1, m_nLineType1, m_color1);
pShrp->Draw(pDc);
}
else if (m_nDraw == 4)
{
pShrp = new Rectangle1(m_point1, m_point2, m_nLineWidth1, m_nLineType1, m_color1);
pShrp->Draw(pDc);
}
else
{
return;
}
CTaskOneDoc* pDoc = GetDocument();
pDoc->AddShap(pShrp);
::ReleaseDC(m_hWnd,*pDc);
pDc = NULL;
m_nDraw = -1;
}
void CTaskOneView::OnGraphicattribute()
{
GraphicAttribute CSel;
if (CSel.DoModal() != IDOK)
return;
m_color1 = CSel.m_color;
m_nLineWidth1 = CSel.m_nLineWidth;
m_nLineType1 = CSel.m_nLineType;
}
#pragma once
#include
#include "CSharp.h"
class CTaskOneDoc : public CDocument
{
protected:
CTaskOneDoc() noexcept;
DECLARE_DYNCREATE(CTaskOneDoc)
public:
public:
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
#ifdef SHARED_HANDLERS
virtual void InitializeSearchContent();
virtual void OnDrawThumbnail(CDC& dc, LPRECT lprcBounds);
#endif
public:
virtual ~CTaskOneDoc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
void AddShap(CSharp* pShap);
protected:
protected:
DECLARE_MESSAGE_MAP()
public:
std::vector<CSharp*> m_vecShap;
#ifdef SHARED_HANDLERS
void SetSearchContent(const CString& value);
#endif
};
#include "pch.h"
#include "framework.h"
#ifndef SHARED_HANDLERS
#include "TaskOne.h"
#endif
#include "TaskOneDoc.h"
#include
#include"MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CTaskOneDoc, CDocument)
BEGIN_MESSAGE_MAP(CTaskOneDoc, CDocument)
END_MESSAGE_MAP()
CTaskOneDoc::CTaskOneDoc() noexcept
{
}
CTaskOneDoc::~CTaskOneDoc()
{
}
BOOL CTaskOneDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
return TRUE;
}
void CTaskOneDoc::AddShap(CSharp* pShap)
{
m_vecShap.push_back(pShap);
}
void CTaskOneDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
CString strTitle = _T("my test");
ar << strTitle;
ar << m_vecShap.size();
for (int i = 0; i < (int)m_vecShap.size(); i++)
{
ShapType Type = m_vecShap[i]->type();
ar << (int)Type;
switch (Type)
{
case knone:
break;
case kLine:
ar << *(Cline*)m_vecShap[i];
break;
case kCircle:
ar << *(CCircle*)m_vecShap[i];
break;
case kEllip:
ar << *(CEllipse*)m_vecShap[i];
break;
case kRect:
ar << *(Rectangle1*)m_vecShap[i];
break;
default:
break;
}
}
}
else
{
CString str;
ar >> str;
if (str != _T("my test"))
{
AfxMessageBox(_T("非保存文件!"));
}
else
{
int size;
ar >> size;
for(int i = 0; i < size; i++)
{
int tmpType ;
ar >> tmpType;
ShapType Type =(ShapType)tmpType;
CSharp* pShap = NULL;
switch (Type)
{
case knone:
break;
case kLine:
{
pShap = new Cline();
ar >> *(Cline*)pShap;
break;
}
case kCircle:
{
pShap = new CCircle();
ar >> *(CCircle*)pShap;
break;
}
case kEllip:
{
pShap = new CEllipse();
ar >> *(CEllipse*)pShap;
break;
}
case kRect:
{
pShap = new Rectangle1();
ar >> *(Rectangle1*)pShap;
break;
}
default:
break;
}
m_vecShap.push_back(pShap);
}
}
}
}
#ifdef SHARED_HANDLERS
void CTaskOneDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));
CString strText = _T("TODO: implement thumbnail drawing here");
LOGFONT lf;
CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
pDefaultGUIFont->GetLogFont(&lf);
lf.lfHeight = 36;
CFont fontDraw;
fontDraw.CreateFontIndirect(&lf);
CFont* pOldFont = dc.SelectObject(&fontDraw);
dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
dc.SelectObject(pOldFont);
}
void CTaskOneDoc::InitializeSearchContent()
{
CString strSearchContent;
SetSearchContent(strSearchContent);
}
void CTaskOneDoc::SetSearchContent(const CString& value)
{
if (value.IsEmpty())
{
RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
}
else
{
CMFCFilterChunkValueImpl *pChunk = nullptr;
ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
if (pChunk != nullptr)
{
pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
SetChunkValue(pChunk);
}
}
}
#endif
#ifdef _DEBUG
void CTaskOneDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CTaskOneDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif