C++ MFC Rectangle Class?

This is a discussion on C++ MFC Rectangle Class? within the C++ Programming forums, part of the General Programming Boards category; Hello, i am modifying the whole c++ MFC. I have mouse draw Line shape, circle shape. However, my rectangle shape ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    15

    C++ MFC Rectangle Class?

    Hello, i am modifying the whole c++ MFC. I have mouse draw Line shape, circle shape. However, my rectangle shape does not work. Can anybody help me writing it?

    All my shapes are different classes.

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you post your code for your rectangle class, then I'm sure we can help you...

    It would also be good if you describe what is wrong with your current implementation (e.g. it crashes, it draws the wrong size, or whatever)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    Ok, here is my rectangle class. Ive written it by modifing circle class so there is bunch of unnecessary parts. I feel like Resize function is wrong.


    Code:
    #include "stdafx.h"
    #include "shaperectangle.h"
    #include <cmath>
    using namespace std;
    
    
    namespace CC {
    
    
    Factory<Rectangle> rectangleFactory("Rectangle");
    
    
    // class Line
    
    Rectangle::Rectangle ()
    {
    }
    	
    Rectangle::Rectangle (const Appearance &someLook, CPoint p, CPoint q)
    	: Shape(someLook)
    {
    	m_Pen=1;
    	 m_enclosingrect = CRect(p,q);
    	 m_enclosingrect.NormalizeRect();
    
    }
    
    Shape* Rectangle::Clone ()
    {
    	return new Rectangle(*this);
    }
    
    void Rectangle::Paint (CDC *pDC)
    {
    	// create the pen
    	CPen pen(PS_SOLID, look.lineWidth, look.lineColor);
    	CPen *oldpen = pDC->SelectObject(&pen);
    
    	// create the brush
    	CBrush brush;
    	if (look.transparent)
    		brush.CreateStockObject(NULL_BRUSH);
    	else
    		brush.CreateSolidBrush(look.fillColor);
    	CBrush *oldbrush = pDC->SelectObject(&brush);
    
    	// draw the line
    	pDC->Rectangle(m_enclosingrect);
    
    	pDC->SelectObject(oldpen);
    	pDC->SelectObject(oldbrush);
    }
    
    CRect Rectangle::GetRect ()
    {
    	CRect BoundingRect;
    	BoundingRect = m_enclosingrect;
    
    	BoundingRect.InflateRect(m_Pen, m_Pen, m_Pen, m_Pen);
    
    	BoundingRect = m_enclosingrect + CRect(m_Pen, m_Pen, m_Pen, m_Pen);
    	BoundingRect=m_enclosingrect;
    	BoundingRect.top -= m_Pen;
    	BoundingRect.left -= m_Pen;
    	BoundingRect.bottom += m_Pen;
    	BoundingRect.right += m_Pen;
    
    	return BoundingRect;
    }
    
    void Rectangle::Resize (CPoint p, CPoint q)
    {
    	m_Start = p;
    	m_End = q;
    }
    
    void Rectangle::Move (CPoint delta)
    {
    	center += delta;
    }
    
    bool Rectangle::Contains (CPoint p)
    {
    	int dx = p.x - center.x;
    	int dy = p.y - center.y;
    	return dx*dx + dy*dy < radius*radius;
    }
    
    bool Rectangle::Persist (Serializer &io)
    {
    	return Shape::Persist(io) &&
    		io.Move(center.x) &&
    		io.Move(center.y) &&
    		io.Move(radius);
    }
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 06:11 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM
  5. Setting Object Data *double* For Class ComboBox :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 03-24-2002, 06:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21