Hey everyone,
I'm new at visual c++ and MFC Applications, and so I am going through Ivor Horton's Beginning Visual C++ 2010 to make a drawing application through Microsoft Foundation Class.
I added menu bars for options, such as shape(rectangle, line, and ellipse) and select colors (black, red, blue green).
I have added message handlers to the program that makes it so that you can select different menu options like rectangle & blue, or ellipse & black. And I am at the point where the drawer can click LButtonDown to set the first point and the LButtonUp to release the second point.
But, I am having a problem when I try to draw any shapes where it records every mouse move so that it doesn't just record the first and second points, which I believe is called rubber banding?
I have attached my MFC GUI to this thread if anyone wants to take a look at what the shapes look like when drawn. THE TOP LEFT ARE RECTANGLES, THE BOTTOM LEFT ARE CIRCLES, AND THE BOTTOM RIGHT ARE SUPPOSE TO BE LINES..
And here is a couple of the important functions:
CElement* CBoxView::CreateElement(void)const{
// Get a pointer to the document for this view
CBoxDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); // verifies that 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());
case ELLIPSE:
return new CEllipse(m_FirstPoint, m_SecondPoint, pDoc->GetElementColor());
case LINE:
return new CLine(m_FirstPoint, m_SecondPoint, pDoc->GetElementColor());
default:
// something's gone wrong
AfxMessageBox(_T("Bad Element Code"), MB_OK);
AfxAbort();
return nullptr;
}
}
void CBoxView::OnMouseMove(UINT nFlags, CPoint point)
{
CClientDC aDC(this);
if((nFlags & MK_LBUTTON) && (this == GetCapture())){
m_SecondPoint = point;
}
aDC.SetROP2(R2_NOTXORPEN); // set the drawing mode
// drawing it for the first time on a white background
m_pTempElement; // redraw the old element
delete m_pTempElement; // delete the old element
m_pTempElement = nullptr; // reset the ptr
m_pTempElement = CreateElement(); // create a new element
m_pTempElement->Draw(&aDC); // draw the element
CView::OnMouseMove(nFlags, point);
}
void CBoxView::OnLButtonUp(UINT nFlags, CPoint point)
{
if(this == GetCapture()){
ReleaseCapture();
}
if(m_pTempElement){ // verifies its value isn't 0
// Call a document class function to store the element pointed
// to by m_pTempElement in the document object
delete m_pTempElement;
m_pTempElement = nullptr; // reset the element pointer
}
}
void CBoxView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_FirstPoint = point; // records the cursors position
SetCapture(); // capture subsequent mouse messages
}
void CBoxView::OnDraw(CDC* pDC)
{
CBoxDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
}
CBoxView::CBoxView()
: m_FirstPoint(CPoint(0,0))
, m_SecondPoint(CPoint(0,0))
, m_pTempElement(nullptr)
{
// TODO: add construction code here
}
If anyone sees the reason why the GUI is acting as is, please let me know. I am completely new at this, so the help is very very appreciated!
- Ben