设计矢量图,运用多继承设计组合图形,要求具备创建不同类型矢量图、选择图形、移动图形、用不同颜色显示图形(表示选中与否),用vector或数组管理图形。
#include<vector>
#include "graphics.h"
#include<iostream>
#include "CShape.h"
using namespace std;
int main()
{
initgraph(640, 480);
setbkcolor(WHITE);
delay_ms(0);
setcolor(BLACK);
setfont(20, 0, "楷体");
setbkmode(TRANSPARENT);
vector<CShape*>shapes;
vector<CShape*>shapestmp;
shapes.push_back(new CTriangle(CPoint(320, 320), CPoint(250, 340), CPoint(340, 450)));
//shapes.push_back(new CTriangle(CPoint(10, 10), CPoint(150, 10), CPoint(150, 150)));
shapes.push_back(new CRect(CPoint(200, 200), CPoint(300, 300)));
shapes.push_back(new Comgraphics(CRect(CPoint(250, 50))));
//移动
bool move_flag = false;
bool copy_flag = false;
bool redraw = true;
//鼠标点击时记录它的坐标
int clickX, clickY;
int copyX, copyY;
int checkedid = -1;
int copyid = -1;
for (; is_run(); delay_fps(60)) {
while (mousemsg()) {
mouse_msg msg = getmouse();
//判断鼠标的移动
if (msg.is_move()) {
if (checkedid != -1) {
if (move_flag) {
shapes[checkedid]->Move(msg.x - clickX, msg.y - clickY);
}
}
clickX = msg.x;
clickY = msg.y;
redraw = true;
}
// 判断鼠标左键
else if (msg.is_left()) {
// 判断鼠标左键是否按下
if (msg.is_down()) {
clickX = msg.x;
clickY = msg.y;
CPoint pt = CPoint(clickX, clickY);
int isIn = 0;
for (int i = 0; i < shapes.size(); i++) {
if (shapes[i]->ptIn(pt)) {
isIn = 1;
//如果鼠标在图形区域内就设置移动的flag为true
move_flag = true;
checkedid = i;
redraw = true;
break;
}
}
if (isIn == 0)
checkedid = -1;
}
else {
move_flag = false;
}
}
}
// 重新绘图
if (redraw) {
redraw = false;
cleardevice();
for (int i = 0; i < shapes.size(); i++) {
if (i == checkedid)
shapes[i]->DrawColor();
else
shapes[i]->Draw();
}
}
while (kbmsg()) {
key_msg msgk = getkey();
if (msgk.key == key_enter && msgk.msg == key_msg_down) {
mouse_msg msgm = getmouse();
if (msgm.is_left()) {
// 判断鼠标左键是否按下
if (msgm.is_down()) {
shapes.push_back(new CRect(CPoint(msgm.x, msgm.y)));
redraw = true;
}
}
if (msgm.is_right()) {
// 判断鼠标右键是否按下
if (msgm.is_down()) {
shapes.push_back(new CTriangle(CPoint(msgm.x, msgm.y)));
redraw = true;
}
}
if (msgm.is_mid()) {
CRect r1 = CRect(CPoint(msgm.x, msgm.y));
// 判断鼠标中键是否按下
if (msgm.is_down()) {
shapes.push_back(new Comgraphics(r1));
redraw = true;