将以上代码用类封装实现
时间: 2024-01-17 11:03:35 浏览: 28
好的,我将帮您将以上代码进行封装成一个类。请看下面的代码:
```cpp
#include <graphics.h>
#include <easyx.h>
#include <conio.h>
#include <cmath>
#include <iostream>
class GraphicsApp {
private:
int width;
int height;
IMAGE img;
const _TCHAR* bgImagePath;
const _TCHAR* topic;
const _TCHAR* poem[4];
public:
GraphicsApp(int w, int h, const _TCHAR* imagePath, const _TCHAR* t, const _TCHAR** p) {
width = w;
height = h;
bgImagePath = imagePath;
topic = t;
for (int i = 0; i < 4; ++i) {
poem[i] = p[i];
}
}
void run() {
initgraph(width, height);
loadimage(&img, bgImagePath);
Resize(NULL, img.getwidth(), img.getheight());
putimage(0, 0, &img);
setbkmode(RED);
settextcolor(0xff0000);
settextstyle(20, 0, _T("楷书"));
int nHeight = textheight(topic[0]);
outtextxy(30, 100, topic);
for (int i = 0; i < 4; ++i) {
outtextxy(20, 100 + nHeight * 1.5 * (2 + i), poem[i]);
}
_getch();
closegraph();
}
};
int main()
{
const _TCHAR* bgImagePath = _T("C:/Users/86181/Pictures/Camera Roll/imgBG.jpg");
const _TCHAR* topic = _T("送元二使安西");
const _TCHAR* poem[4] = { _T("渭城朝雨浥轻尘,"), _T("客舍青青柳色新。"),
_T("劝君更尽一杯酒,"), _T("西出阳关无故人。") };
GraphicsApp app(600, 800, bgImagePath, topic, poem);
app.run();
return 0;
}
```
以上代码将原来的代码封装到了名为`GraphicsApp`的类中,并添加了构造函数和`run`方法来运行图形界面。在`main`函数中,创建了一个`GraphicsApp`对象并调用`run`方法来执行程序。这样可以更好地组织代码,并提高代码的可读性和可维护性。