CElement* CSketcherView::CreateElement(void) const { // Get a pointer to the document for this view CSketcherDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // Verify the pointer is good // Now select the element using the type stored in the document switch(pDoc->GetElementType()) { case RECTANGLE: return new CRectangle(m_FirstPoint, m_SecondPoint,pDoc->GetElementColor(),pDoc->GetElementPenStyle()); case CIRCLE: return new CCircle(m_FirstPoint, m_SecondPoint, pDoc->GetElementColor(),pDoc->GetElementPenStyle()); case CURVE: return new CCurve(new CPoint(m_FirstPoint), new CPoint(m_SecondPoint), pDoc->GetElementColor(),pDoc->GetElementPenStyle()); case LINE: return new CLine(m_FirstPoint, m_SecondPoint, pDoc->GetElementColor(),pDoc->GetElementPenStyle()); case ELLIPSE: return new CEllipse(m_FirstPoint, m_SecondPoint, pDoc->GetElementColor(),pDoc->GetElementPenStyle()); default: // Something's gone wrong AfxMessageBox(_T("Bad Element code"), MB_OK); AfxAbort(); return nullptr; } }
void CSketcherView::OnLButtonDown(UINT nFlags, CPoint point) { CClientDC aDC(this); // Create a device context OnPrepareDC(&aDC); // Get origin adjusted aDC.DPtoLP(&point); // Convert point to logical coordinatesm_FirstPoint = point; // Record the cursor position if(m_MoveMode) { // In moving mode, so drop the element m_MoveMode = false; // Kill move mode m_pSelected = nullptr; // De-select the element GetDocument()->UpdateAllViews(0); // Redraw all the views GetDocument()->SetModifiedFlag(); return; } CSketcherDoc* pDoc = GetDocument();// Get a document pointer if(pDoc->GetElementType() == TEXT) { CTextDialog aDlg; if(aDlg.DoModal() == IDOK) { // Exit OK so create a text element CSize textExtent = aDC.GetTextExtent(aDlg.m_TextString); CRect rect(point, textExtent); //Create enclosing rectangle CText* pTextElement = new CText( aDlg.m_TextString, rect, pDoc->GetElementColor()); // Add the element to the document pDoc->AddElement(pTextElement); // Get all views updated pDoc->UpdateAllViews(nullptr, 0, pTextElement); } return; } m_FirstPoint = point; // Record the cursor position SetCapture(); // Capture subsequent mouse messages }