在MFC中 如何对已经画好的图形进行删除,或者如何对图形操作。 我要的是代码,谢谢。
1个回答
展开全部
#include <iostream>
#include <time.h>
#include <set>
using namespace std;
struct CPoint
{
int x;
int y;
CPoint(int xx, int yy) : x(xx), y(yy){}
};
struct shape
{
virtual ~shape() {}
virtual bool HitTest(CPoint pt) = 0;
virtual void Move(int dx, int dy) = 0;
virtual void Draw(/*CDC *pdc*/) = 0;
};
class Line : public shape
{
public:
Line(CPoint s, CPoint e) :start_(s), end_(e) {}
virtual bool HitTest(CPoint pt)
{
//distance between point and the line
return true;
}
virtual void Move(int dx, int dy)
{
start_.x += dx;
start_.y += dy;
}
virtual void Draw() {cout << "draw Line" << endl;}
private:
CPoint start_;
CPoint end_;
};
class ShapeManager
{
public:
~ShapeManager() {/*delete shapes or memery leak*/}
shape *HitShape(CPoint pt)
{
for (Itr itr = shapes_.begin(); itr != shapes_.end(); ++itr)
if ((*itr)->HitTest(pt))
return *itr;
return 0;
}
void AddShape(shape *item) {shapes_.insert(item);}
void DeleteShape(shape *item)
{
shapes_.erase(item);
}
private:
typedef set<shape *>::iterator Itr;
set<shape *> shapes_;
};
int main()
{
ShapeManager sm;
sm.AddShape(new Line(CPoint(0, 0), CPoint(100, 100)));
shape *sel = sm.HitShape(CPoint(20, 30));
sel->Move(10, 10);
sm.DeleteShape(sel);
return 0;
}
#include <time.h>
#include <set>
using namespace std;
struct CPoint
{
int x;
int y;
CPoint(int xx, int yy) : x(xx), y(yy){}
};
struct shape
{
virtual ~shape() {}
virtual bool HitTest(CPoint pt) = 0;
virtual void Move(int dx, int dy) = 0;
virtual void Draw(/*CDC *pdc*/) = 0;
};
class Line : public shape
{
public:
Line(CPoint s, CPoint e) :start_(s), end_(e) {}
virtual bool HitTest(CPoint pt)
{
//distance between point and the line
return true;
}
virtual void Move(int dx, int dy)
{
start_.x += dx;
start_.y += dy;
}
virtual void Draw() {cout << "draw Line" << endl;}
private:
CPoint start_;
CPoint end_;
};
class ShapeManager
{
public:
~ShapeManager() {/*delete shapes or memery leak*/}
shape *HitShape(CPoint pt)
{
for (Itr itr = shapes_.begin(); itr != shapes_.end(); ++itr)
if ((*itr)->HitTest(pt))
return *itr;
return 0;
}
void AddShape(shape *item) {shapes_.insert(item);}
void DeleteShape(shape *item)
{
shapes_.erase(item);
}
private:
typedef set<shape *>::iterator Itr;
set<shape *> shapes_;
};
int main()
{
ShapeManager sm;
sm.AddShape(new Line(CPoint(0, 0), CPoint(100, 100)));
shape *sel = sm.HitShape(CPoint(20, 30));
sel->Move(10, 10);
sm.DeleteShape(sel);
return 0;
}
追问
首先,谢谢你。
我是在MFC中编程,有的只是对象,没有过程,我的问题已经解决。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询