#include <graphics.h> #include <iostream> #include <conio.h> int main() { initgraph(640, 480); // 创建 640*480 的窗口 setbkcolor(WHITE); // 设置窗口背景色为白色 int x = 100, y = 100; setlinecolor(LIGHTGRAY); line(x + 1, y + 2, x + 8, y + 2); line(x + 2, y + 1, x + 2, y + 8); line(x + 29, y + 2, x + 22, y + 2); line(x + 29, y + 1, x + 29, y + 8); line(x + 2, y + 29, x + 8, y + 29); line(x + 2, y + 22, x + 2, y + 29); line(x + 29, y + 29, x + 22, y + 29); line(x + 29, y + 22, x + 29, y + 29); COLORREF color = RGB(135, 206, 235); // 浅蓝色RGB值 setfillcolor(color); solidrectangle(x + 3, y + 3, x + 28, y + 28); // 填充浅蓝色 getch(); // 暂停等待用户按键 closegraph(); // 关闭窗口 return 0; }这个代码显示的窗口闪现一下就关闭了,怎么让他一直存在
时间: 2023-11-18 17:04:11 浏览: 31
你可以在代码结尾添加一个循环来让窗口一直存在,例如:
```
while (!kbhit()) { // 检测是否有键盘输入
delay(200); // 等待一段时间,防止CPU占用过高
}
closegraph(); // 关闭窗口
return 0;
```
这样的话,窗口会一直存在直到用户按下键盘上的任意键。
相关问题
#include <graphics.h> #include<easyx.h> #include<conio.h> #include<cmath> #include<iostream> //using namespace std; int main() { initgraph(600, 800); IMAGE img; loadimage(&img, _T("C:/Users/86181/Pictures/Camera Roll/imgBG.jpg")); Resize(NULL,img.getwidth(),img.getheight()); /* setbkcolor(RED); cleardevice();*/ putimage(0, 0, &img); setbkmode(RED); settextcolor(0xff0000); settextstyle(20, 0, _T("楷书")); _TCHAR topic[] = _T("送元二使安西"); _TCHAR poem[][20] = { _T("渭城朝雨浥轻尘,"),_T("客舍青青柳色新。"), _T("劝君更尽一杯酒,"),_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(); // 关闭图形窗口 return 0; }
这是一个使用C++编写的程序,使用了graphics.h和easyx.h库来实现图形界面的绘制。程序的功能是在窗口中显示一张图片,并在图片上绘制一首诗。具体的实现步骤如下:
1. 初始化图形界面,设置窗口的宽度和高度为600和800。
2. 创建一个IMAGE对象img,并加载一张图片到该对象中。
3. 调整窗口大小以适应图片的宽度和高度。
4. 在窗口中绘制图片,绘制位置为(0, 0)。
5. 设置文本背景模式为红色,设置文本颜色为红色。
6. 设置文本样式为楷书,字体大小为20。
7. 定义一个_TCHAR类型的变量topic,并赋值为"送元二使安西"。
8. 定义一个二维数组poem,存储四行诗的内容。
9. 计算出每行诗的高度。
10. 在窗口中绘制诗的标题,位置为(30, 100)。
11. 使用循环绘制每行诗的内容,位置逐行下移。
12. 使用_getch()函数等待按键,按下任意键后退出程序。
13. 关闭图形窗口,释放资源。
14. 返回0,表示程序运行结束。
请注意,该程序使用了graphics.h和easyx.h库,这两个库可能是基于Windows的,所以在其他平台上可能无法运行。
#include<iostream> #include<conio.h> #include<graphics.h> #include<time.h> #include<math.h> using namespace std; class Csin { double A, W, B, x_bg,x_end,b_bg, b_end; int step=1; public: Csin(double a, double w, double b, double x_b, double x_e,double b_b, double b_e) { A = a, W = w, B = b; x_bg = x_b,x_end=x_e; b_bg = b_b, b_end = b_e; } void cartoon() { double B1 = b_bg; while (B1 <= b_end) { for (double i = x_bg; i <= x_end; i++) { line(i, A * sin(W * i + B) + 150, i + step, A * sin(W * (i + step) + B) + 150); } B1++; Sleep(30); cleardevice(); } } }; int main() { double a,b,c,d,e; initgraph(800,800); srand(time(0)); a = rand() % 100; b = rand() % 100*0.01; c = rand() % 5; d = rand() % 200+100; do { e = rand() % 200+100;} while(e < d); Csin C1(a,b,c,200.0,400.0,d,e); _getch(); closegraph(); return 0; }有什么bug?
While循环的基本语法是:
```
while (条件) {
// 循环执行的代码
}
```
其中,条件是一个布尔表达式。只要条件为真,就会一直执行循环体中的代码,直到条件为假才会停止循环。
例如,下面的代码使用 while 循环输出 1 到 10 的整数:
```
int i = 1;
while (i <= 10) {
cout << i << " ";
i++;
}
```
输出结果为:1 2 3 4 5 6 7 8 9 10
在每次循环开始时,都会检查条件 i <= 10 是否满足。只要满足条件,就会执行 cout << i << " "; 和 i++; 的语句,然后再次检查条件。当 i 变成 11 时,不满足条件,循环停止。