// Create a new element on the heap CElement* CSketcherView::CreateElement(void) { // 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->GetPenWidth()); case CIRCLE: return new CCircle(m_FirstPoint, m_SecondPoint, pDoc->GetElementColor(), pDoc->GetPenWidth()); case CURVE: return new CCurve(m_FirstPoint, m_SecondPoint, pDoc->GetElementColor(), pDoc->GetPenWidth()); case LINE: return new CLine(m_FirstPoint, m_SecondPoint, pDoc->GetElementColor(), pDoc->GetPenWidth()); default: // Something's gone wrong AfxMessageBox(_T("Bad Element code"), MB_OK); AfxAbort(); return NULL; } }
void CSketcherView::OnElementDelete() { if(m_pSelected) { CSketcherDoc* pDoc = GetDocument();// Get the document pointer pDoc->DeleteElement(m_pSelected); // Delete the element pDoc->UpdateAllViews(nullptr); // Redraw all the views m_pSelected = nullptr; // Reset selected element ptr } }
void CSketcherView::OnDraw(CDC* pDC) { CSketcherDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; CElement* pElement(0); for(auto iter = pDoc->begin() ; iter != pDoc->end() ; ++iter) { pElement = *iter; if(pDC->RectVisible(pElement->GetBoundRect())) // If the element is visible pElement->Draw(pDC, m_pSelected); // ...draw it } }
void CSketcherView::OnDraw(CDC* pDC) { CSketcherDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if(!pDoc) return; POSITION aPos = pDoc->GetListHeadPosition(); CElement* pElement = 0; // Store for an element pointer while(aPos) // Loop while aPos is not null { pElement = pDoc->GetNext(aPos); // Get the current element pointer // If the element is visible... if(pDC->RectVisible(pElement->GetBoundRect())) pElement->Draw(pDC, m_pSelected);// ...draw it } }
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 }
void CSketcherView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) { CScrollView::OnPrepareDC(pDC, pInfo); CSketcherDoc* pDoc = GetDocument(); pDC->SetMapMode(MM_ANISOTROPIC); // Set the map mode CSize DocSize = pDoc->GetDocSize(); // Get the document size pDC->SetWindowExt(DocSize); // Now set the window extent // Get the number of pixels per inch in x and y int xLogPixels = pDC->GetDeviceCaps(LOGPIXELSX); int yLogPixels = pDC->GetDeviceCaps(LOGPIXELSY); // Calculate the viewport extent in x and y int xExtent = (DocSize.cx*m_Scale*xLogPixels)/100; int yExtent = (DocSize.cy*m_Scale*yLogPixels)/100; pDC->SetViewportExt(xExtent,yExtent); // Set viewport extent }
void CSketcherView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) { // TODO: Add your specialized code here and/or call the base class CScrollView::OnPrepareDC(pDC, pInfo); CSketcherDoc* pDoc = GetDocument(); pDC->SetMapMode(MM_ANISOTROPIC); // Set the map mode CSize DocSize = pDoc->GetDocSize(); // Get the document size // y extent must be negative because we want MM_LOENGLISH DocSize.cy = -DocSize.cy; // Change sign of y pDC->SetWindowExt(DocSize); // Now set the window extent // Get the number of pixels per inch in x and y int xLogPixels = pDC->GetDeviceCaps(LOGPIXELSX); int yLogPixels = pDC->GetDeviceCaps(LOGPIXELSY); // Calculate the viewport extent in x and y long xExtent = static_cast<long>(DocSize.cx)*m_Scale*xLogPixels/100L; long yExtent = static_cast <long>(DocSize.cy)*m_Scale*yLogPixels/100L; pDC->SetViewportExt(static_cast<int>(xExtent), static_cast<int>(-yExtent)); // Set viewport extent }
void CSketcherView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if ( m_DrawTrigger == 2 ) { m_DrawTrigger = 0; CClientDC aDC(this); TCHAR sz[128]= {0}; //wsprintf(sz, _T("Called Times of OnMoveMouse is %d"), m_CalledTimes_OnMouseMove); _stprintf_s(sz, _T("Called Times of OnMoveMouse is %08d"), m_CalledTimes_OnMouseMove); aDC.TextOutW(0,0,sz); // The format "%08d" will prevent last display length is bigger. m_CalledTimes_OnMouseMove = 0; CSketcherDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; TCHAR szColor[32] = {0}; TCHAR szElement[32] = {0}; switch(pDoc->GetColor()) { case RED: wcscat_s(szColor,L"red"); // 没有找到合适的_T定义函数 break; case GREEN: wcscat_s(szColor,L"green"); break; case BLUE: wcscat_s(szColor,L"blue"); break; case BLACK: wcscat_s(szColor,L"black"); break; default: wcscat_s(szColor,L"unknow"); break; } switch( pDoc->GetElement() ) { case LINE: wcscat_s(szElement,L"line"); break; case RECTANGLE: wcscat_s(szElement,L"rectangle"); break; case CIRCLE: wcscat_s(szElement,L"circle"); break; case CURVE: wcscat_s(szElement,L"curve"); break; default: wcscat_s(szElement,L"unknow"); break; } TCHAR sz1[128] = {0}; _stprintf_s(sz1, _T("Color is %s, and Element is %s "), szColor, szElement); // The last spaces is to override last characters. aDC.TextOutW(0,30,sz1); } CView::OnLButtonUp(nFlags, point); }