函数声明 | 函数功能 |
---|---|
| 把浮点数转换成字符串,同时返回一个指向字符串的存储位置的指针的函数。 |
| 取最后一次调用arc的坐标 |
| 获取当前背景颜色 |
| 从流中取字符 |
| 从 stdin 流中读字符 |
| 当前画线的颜色 |
| 获取当前工作目录 |
| 获取调色板定义结构 |
| 获取当前图形驱动程序名字 |
| 将用户定义的填充模式拷贝到内存中 |
| 获取有关当前填充模式和填充颜色的信息 |
| 获取当前图形模式 |
| 保存指定区域的屏幕上的像素图形到指定的内存区域 |
| 取当前线型、模式和宽度 |
| 可以传给函数 setcolor 的最大颜色值 |
| 屏幕的最大x坐标 |
| 屏幕的最大y坐标 |
函数声明 | 函数功能 |
---|---|
| 把浮点数转换成字符串,同时返回一个指向字符串的存储位置的指针的函数。 |
参数:
value:
被转换的值。ndigit:
存储的有效数字位数。buf:
结果的存储位置。注意:
gcvt
函数把一个浮点值转换成一个字符串 (包括一个小数点和可能的符号字节) 并存储该字符串在buffer
中。该buffer
应足够大以便容纳转换的值加上结尾的 结束符'\0'
,它是自动添加的。 如果一个缓冲区的大小为ndigit + 1
,则gcvt
函数将覆盖该缓冲区的末尾。这是因为转换的字符串包括一个小数点以及可能包含符号和指数信息。
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char str[25];
double num;
int sig = 5;
num = 1.23;
gcvt(num, sig, str);
printf("string = %s\n", str);
num = -456.78912;
gcvt(num, sig, str);
printf("string = %s\n", str);
num = 0.345e5;
gcvt(num, sig, str);
printf("string = %s\n", str);
return(0);
}
函数声明 | 函数功能 |
---|---|
| 取最后一次调用arc的坐标 |
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver = DETECT, gmode, errorcode;
struct arccoordstype arcinfo;
int midx, midy;
int stangle = 45, endangle = 270;
char sstr[80], estr[80];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
arc(midx, midy, stangle, endangle, 100);
// 取最后一次调用arc的坐标
getarccoords(&arcinfo);
sprintf(sstr, "*- (%d, %d)", arcinfo.xstart, arcinfo.ystart);
sprintf(estr, "*- (%d, %d)", arcinfo.xend, arcinfo.yend);
outtextxy(arcinfo.xstart, arcinfo.ystart, sstr);
outtextxy(arcinfo.xend, arcinfo.yend, estr);
getch();
closegraph();
return 0;
}
上述代码是一个简单的图形程序,使用了图形库函数 arc
来绘制一个弧线并显示其起始和结束点的坐标。
大致逻辑如下:
(midx, midy)
。45
度,结束角度为 270
度,半径为 100
像素。arc
函数时的坐标信息,并将其存储在 arcinfo
结构体中。sprintf
函数将起始点和结束点的坐标格式化为字符串。函数声明 | 函数功能 |
---|---|
| 获取当前背景颜色 |
颜色值 | 英文枚举 | 中文描述 |
---|---|---|
0 | BLACK | 黑 |
1 | BLUE | 蓝 |
2 | GREEN | 绿 |
3 | CYAN | 青 |
4 | RED | 红 |
5 | MAGENTA | 洋红 |
6 | BROWN | 棕 |
7 | LIGHTGRAY | 淡灰 |
8 | DARKGRAY | 深灰 |
9 | LIGHTBLUE | 淡兰 |
10 | LIGHTGREEN | 淡绿 |
11 | LIGHTCYAN | 淡青 |
12 | LIGHTRED | 淡红 |
13 | LIGHTMAGENTA | 淡洋红 |
14 | YELLOW | 黄 |
15 | WHITE | 白 |
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int bkcolor, midx, midy;
char bkname[35];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
settextjustify(CENTER_TEXT, CENTER_TEXT);
cleardevice();
for (int i = WHITE; i >= 0; i--)
{
setbkcolor(i);
bkcolor = getbkcolor(); // 获取当前背景颜色
if (i == WHITE) setcolor(BLACK);
else setcolor(WHITE);
itoa(bkcolor, bkname, 10);
strcat(bkname," is the current background color.");
outtextxy(midx, midy, bkname);
getch();
cleardevice();
}
getch();
closegraph();
return 0;
}
上述也是一个简单的图形程序,通过使用图形库来绘制背景颜色变化。
下面来简单总结下:
(midx, midy)
。i
所代表的颜色。bkcolor
数组中。bkname
字符串中。函数声明 | 函数功能 |
---|---|
| 从流中取字符 |
#include <stdio.h>
int main()
{
char ch;
printf("Input a character:");
ch = getc(stdin);
printf("The character input was: '%c'\n", ch);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 从 stdin 流中读字符 |
#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) != '\n')
printf("%c ", c);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 当前画线的颜色 |
颜色值 | 英文枚举 | 中文描述 |
---|---|---|
0 | BLACK | 黑 |
1 | BLUE | 蓝 |
2 | GREEN | 绿 |
3 | CYAN | 青 |
4 | RED | 红 |
5 | MAGENTA | 洋红 |
6 | BROWN | 棕 |
7 | LIGHTGRAY | 淡灰 |
8 | DARKGRAY | 深灰 |
9 | LIGHTBLUE | 淡兰 |
10 | LIGHTGREEN | 淡绿 |
11 | LIGHTCYAN | 淡青 |
12 | LIGHTRED | 淡红 |
13 | LIGHTMAGENTA | 淡洋红 |
14 | YELLOW | 黄 |
15 | WHITE | 白 |
#include <graphics.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int color, midx, midy;
char colname[35];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
settextjustify(CENTER_TEXT, CENTER_TEXT);
for (int i = WHITE; i > 0; i--)
{
color = getcolor();
itoa(color, colname, 10);
strcat(colname, " is the current drawing color.");
outtextxy(midx, midy, colname);
getch();
cleardevice();
setcolor(i - 1);
}
getch();
closegraph();
return 0;
}
上述程序,通过使用图形库,在一个循环中遍历所有颜色,每次在屏幕中心显示当前颜色的名称和值,等待用户按键后更改颜色并清除屏幕,直到所有颜色展示完毕。
函数声明 | 函数功能 |
---|---|
| 获取当前工作目录 |
注意:getcwd 函数是将当前工作目录的绝对路径复制到参数 buffer 所指的内存空间中,参数 maxlen 为 buffer 的空间大小。
#include <stdio.h>
#include <dir.h>
#define MAXPATH 1000
int main()
{
char buffer[MAXPATH];
getcwd(buffer, MAXPATH);
printf("The current directory is: %s\n", buffer);
return 0;
}
函数声明 | 函数功能 |
---|---|
| 获取调色板定义结构 |
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int i, midx, midy;;
struct palettetype far *pal=NULL;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 3;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
// 获取调色板定义结构
pal = getdefaultpalette();
char buffer[100];
for (i=BLACK; i<WHITE + 1; i++)
{
sprintf(buffer, "colors[%d] = %d", i, pal->colors[i]);
outtextxy(midx, midy, buffer);
getch();
cleardevice();
}
getch();
closegraph();
return 0;
}
函数声明 | 函数功能 |
---|---|
| 获取当前图形驱动程序名字 |
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
char *drivername;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
setcolor(getmaxcolor());
// 当前图形驱动程序名字
drivername = getdrivername();
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(getmaxx() / 2, getmaxy() / 2, drivername);
getch();
closegraph();
return 0;
}
函数声明 | 函数功能 |
---|---|
| 将用户定义的填充模式拷贝到内存中 |
#include <graphics.h>
#include <stdio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x25, 0x27, 0x04, 0x04};
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
maxx = getmaxx();
maxy = getmaxy();
setcolor(getmaxcolor());
// 选择用户定义的填充模式
setfillpattern(pattern, getmaxcolor());
bar(0, 0, maxx, maxy);
getch();
// 将用户定义的填充模式拷贝到内存中
getfillpattern(pattern);
pattern[0] += 1;
pattern[1] -= 2;
pattern[2] += 3;
pattern[3] -= 4;
pattern[4] += 5;
pattern[5] -= 6;
pattern[6] += 7;
pattern[7] -= 8;
// 选择用户定义的填充模式
setfillpattern(pattern, getmaxcolor());
bar(0, 0, maxx, maxy);
getch();
closegraph();
return 0;
}
函数声明 | 函数功能 |
---|---|
| 获取有关当前填充模式和填充颜色的信息 |
参数:
struct fillsettingstype *fillinfo
: 一个指向 fillsettingstype
结构体的指针,该结构体用于存储当前的填充模式设置。 fillsettingstype
结构体通常包含以下成员:int pattern
:指定填充图案的索引。 BGI库提供了一套预定义的填充图案,可以通过这个索引来选择。unsigned char color
:指定填充图案的颜色。 颜色通常是通过一个颜色索引来指定的,该索引对应于一个预定义的颜色表。#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
// the names of the fill styles supported
char *fname[] = { "EMPTY_FILL",
"SOLID_FILL",
"LINE_FILL",
"LTSLASH_FILL",
"SLASH_FILL",
"BKSLASH_FILL",
"LTBKSLASH_FILL",
"HATCH_FILL",
"XHATCH_FILL",
"INTERLEAVE_FILL",
"WIDE_DOT_FILL",
"CLOSE_DOT_FILL",
"USER_FILL"
};
int main()
{
int gdriver = DETECT, gmode, errorcode;
struct fillsettingstype fillinfo;
int midx, midy;
char patstr[40], colstr[40];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
// 获取有关当前填充模式和填充颜色的信息
getfillsettings(&fillinfo);
sprintf(patstr, "%s is the fill style.", fname[fillinfo.pattern]);
sprintf(colstr, "%d is the fill color.", fillinfo.color);
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, patstr);
outtextxy(midx, midy+2*textheight("W"), colstr);
getch();
closegraph();
return 0;
}
函数声明 | 函数功能 |
---|---|
| 获取当前图形模式 |
#include <graphics.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy, mode;
char numname[80], modename[80];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
// 获取当前图形模式
mode = getgraphmode();
sprintf(numname, "%d is the current mode number.", mode);
sprintf(modename, "%s is the current graphics mode", getmodename(mode));
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, numname);
outtextxy(midx, midy+2*textheight("W"), modename);
getch();
closegraph();
return 0;
}
函数声明 | 函数功能 |
---|---|
| 保存指定区域的屏幕上的像素图形到指定的内存区域 |
参数:
int left
:指定要获取的图像区域的左边界的x坐标,以像素为单位int top
: 指定要获取的图像区域的上边界的y坐标,以像素为单位int right
: 指定要获取的图像区域的右边界的x坐标,以像素为单位int bottom
:指定要获取的图像区域的下边界的y坐标,以像素为单位void *bitmap
: 指向存储获取到的图像数据的内存位置的指针,这个指针可以指向任何类型的数据。在实际使用中,这个指针通常指向一个预先分配好的缓冲区,该缓冲区的大小应足够存储指定区域的图像数据。#include<graphics.h>
int main()
{
int driver,mode;
unsigned size;
void *buf;
driver=DETECT;
mode=0; initgraph(&driver,&mode,"");
setcolor(15);
rectangle(20,20,200,200);
setcolor(RED);
line(20,20,200,200);
setcolor(GREEN);
line(20,200,200,20);
getch();
size=imagesize(20,20,200,200);
if(size!=-1)
{
buf=malloc(size);
if(buf)
{
getimage(20,20,200,200,buf);
putimage(100,100, buf,COPY_PUT);
putimage(300,50, buf,COPY_PUT);
}
}
outtext("press a key");
getch();
return 0;
}
函数声明 | 函数功能 |
---|---|
| 取当前线型、模式和宽度 |
参数:
struct linesettingstype *lininfo
: 一个指向 linesettingstype
结构体的指针,该结构体用于存储当前的线条绘制设置。linesettingstype
结构体通常包含以下成员:int linestyle
: 指定线条的类型。它通常是一个整数,用于选择预定义的线条样式,如实线、虚线、点线等。unsigned char upattern
: 对于某些线条样式(如虚线),该参数可能用于指定线条中点和空白的具体模式。int thickness
: 指定线条的宽度。在BGI中,线条的宽度可能是以像素为单位的,但具体实现可能有所不同。unsigned char color
: 指定线条的颜色。颜色通常是通过一个颜色索引来指定的,该索引对应于一个预定义的颜色表。#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
// the names of the line styles supported
char *lname[] = { "SOLID_LINE",
"DOTTED_LINE",
"CENTER_LINE",
"DASHED_LINE",
"USERBIT_LINE"
};
int main()
{
int gdriver = DETECT, gmode, errorcode;
struct linesettingstype lineinfo;
int midx, midy;
char lstyle[80], lpattern[80], lwidth[80];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
// 取当前线型、模式和宽度
getlinesettings(&lineinfo);
sprintf(lstyle, "%s is the line style.", lname[lineinfo.linestyle]);
sprintf(lpattern, "0x%X is the user-defined line pattern.", lineinfo.upattern);
sprintf(lwidth, "%d is the line thickness.", lineinfo.thickness);
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, lstyle);
outtextxy(midx, midy+2*textheight("W"), lpattern);
outtextxy(midx, midy+4*textheight("W"), lwidth);
getch();
closegraph();
return 0;
}
函数声明 | 函数功能 |
---|---|
| 可以传给函数 |
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
char colstr[80];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
sprintf(colstr, "This mode supports colors 0~%d", getmaxcolor());
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, colstr);
getch();
closegraph();
return 0;
}
函数声明 | 函数功能 |
---|---|
| 屏幕的最大x坐标 |
| 屏幕的最大y坐标 |
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
char xrange[80], yrange[80];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
sprintf(xrange, "X values range from 0~%d", getmaxx());
sprintf(yrange, "Y values range from 0~%d", getmaxy());
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, xrange);
outtextxy(midx, midy+2*textheight("W"), yrange);
getch();
closegraph();
return 0;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。