graphics.h
@函数名称: getarccoords
函数原型: void far getarccoords(struct arccoordstype far *coords)
函数功能: 得到最后一次画圆弧的坐标
函数返回:
参数说明: coords-使用函数arc()画圆弧的坐标,该结构如下:
struct arccoordstype{
int m,n; /* 圆心坐标 */
int xstart,ysrart,xend,yend;/* 圆弧起点终点坐标 */
};
所属文件: <graphics.h>
#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",grapherrormsg(errorcode));
printf("ress any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
arc(midx,midy,stangle,endangle,100);
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
函数原型: void far arc(int x, int y, int start, int end, int radius)
函数功能: 画一圆弧线
函数返回:
参数说明: x,y-圆心,radius-半径,start,end-弧线的起始和中止角度
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
int stangle=45,endangle=135;
int radius=100;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("ress any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
arc(midx,midy,stangle,endangle,radius);
getch();
closegraph();
return 0;
}
@函数名称: circle
函数原型: void far circle(int x, int y, int radius)
函数功能: 画圆
函数返回:
参数说明: x,y-圆心坐标,radius-半径
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
int radius=100;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("ress any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
circle(midx,midy,radius);
getch();
closegraph();
return 0;
}
@函数名称: pieslice
函数原型: void far pieslice(int x,int y,int start,int end,int radius)
函数功能: 画扇形
函数返回:
参数说明: x,y-扇形所在园的圆心坐标,start,end-扇形的开始和结束角度,radius-园的半径
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
int stangle=45,endangle=135,radius=100;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setfillstyle(EMPTY_FILL,getmaxcolor());
pieslice(midx,midy,stangle,endangle,radius);
getch();
closegraph();
return 0;
}
@函数名称: getaspectratio,setaspectratio
函数原型: void far getaspectratio(int far *xasp, int far *yasp)
函数功能: 得到图形显示的比例因子
函数返回:
参数说明: xasp-x方向比例因子,yasp-y方向比例因子
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int xasp,yasp,midx,midy;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
getaspectratio(&xasp,&yasp);
circle(midx,midy,100);
getch();
cleardevice();
setaspectratio(xasp/2,yasp);
circle(midx,midy,100);
getch();
cleardevice();
setaspectratio(xasp,yasp/2);
circle(midx,midy,100);
getch();
closegraph();
return 0;
}
@函数名称: sector
函数原型: void far sector(int x,int y,int start,int end,int xrad,int yrad)
函数功能: 画扇形,并填充以预定填充图案和颜色
函数返回:
参数说明: x,y-圆心坐标,start,end-圆弧起点和终点角度,单位度,xrad,yrad-椭圆x,y方向的半径
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver=DETECT,gmode,errorcode;
int midx,midy,i;
int stangle=45,endangle=135;
int xrad=100, yrad=50;
/* initialize graphics and local variables */
initgraph(&gdriver,&gmode,"");
/* read result of initialization */
errorcode=graphresult();
if (errorcode!=grOk) /* an error occurred */
{
printf("Graphics error: %s/n",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx=getmaxx()/2;
midy=getmaxy()/2;
/* loop through the fill patterns */
for(i=EMPTY_FILL;i<USER_FILL;i++)
{
/* set the fill style */
setfillstyle(i,getmaxcolor());
/* draw the sector slice */
sector(midx,midy,stangle,endangle,xrad,yrad);
getch();
}
/* clean up */
closegraph();
return 0;
}
@函数名称: ellipse
函数原型: void far ellipse(int x,int y,int start,int end,int xradius,int yradius)
函数功能: 画椭圆弧
函数返回:
参数说明: x,y-圆心坐标,start,end-圆弧起点和终点角度,单位为度,xradius,yradius-椭圆x,y方向的半径
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
int stangle=0,endangle=360;
int xradius=100,yradius=50;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
ellipse(midx,midy,stangle,endangle,xradius,yradius);
getch();
closegraph();
return 0;
}
@函数名称: fillellipse
函数原型: void far fillellipse(int x, int y, int xradius, int yradius)
函数功能: 画椭圆并用当前的填充色和填充图案填充
函数返回:
参数说明: x,y-圆心坐标,xradius,yradius-椭圆x,y方向的半径
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy,i;
int xradius=100,yradius=50;
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;
/* loop through the fill patterns */
for (i=EMPTY_FILL;i<USER_FILL;i++){
/* set fill pattern */
setfillstyle(i,getmaxcolor());
fillellipse(midx,midy,xradius,yradius);
getch();
}
closegraph();
return 0;
}
@函数名称: getfillsettings
函数原型: void far getfillsettings(struct fillsettingstype far *info)
函数功能: 得到当前填充模式和颜色编号
函数返回:
参数说明: info-当前填充模式和颜色编号,该结构定义如下:
struct fillsettingstype{
int patterns;
int color;
}
其中patterns定义如下:
EMPTY_FILL 0背景色填充 SOLID_FILL 1实填充
LINE_FILL 2--- 线填充 LTSLASH_FILL 3斜线填充
SLASH_FILL 4粗斜线填充 BKSLASH_FILL 5反粗斜线填充
LTBKSLASH_FILL 6反斜线填充 HATCH_FILL 7网格填充
XHATCH_FILL 8斜网格填充 INTERLEAVE_FILL 9间隔点线填充
WIDE_DOT_FILL 10稀疏点填充 CLOSE_DOT_FILL 11密集点填充
USER_FILL 12用户自定义填充
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
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",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;
}
@函数名称: getfillpattern,setfillpattern
函数原型: void far getfillpattern(char far *pattern)
void far setfillpattern(char far *pattern,int color)
函数功能: 得到当前的填充模式/用户自定义填充
函数返回:
参数说明: color-填充颜色,pattern-填充数据数组指针,一般为8个字节,
当该数据上的某位为1时,填充以color所指定的颜色.
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int maxx,maxy;
/* 这是一个8*8点阵的‘Tc’ */
char pattern[8]={0x00,0x70,0x20,0x27,0x24,0x24,0x07,0x00};
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();
pattern[4]-=1;
pattern[5]-=3;
pattern[6]+=3;
pattern[7]-=4;
setfillpattern(pattern,getmaxcolor());
bar(0,0,maxx,maxy);
getch();
closegraph();
return 0;
}
@函数名称: setfillstyle
函数原型: void far setfillstyle(int pattern,int color)
函数功能: 设置填充模式和填充颜色
函数返回:
参数说明: color-填充颜色 pattern-填充模式
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int maxx,maxy;
char pattern[8]={0x00,0x70,0x20,0x27,0x24,0x24,0x07,0x00};
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",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();
closegraph();
return 0;
}
@函数名称: settextstyle
函数原型: void far settextstyle(int font, int direction, int charsize)
函数功能: 设置图形模式下字符显示底字体、方向和大小
函数返回:
参数说明: font-显示字体,取值如下:
DEFAULT_FONT =0 8x8 点阵
TRIPLEX_FONT =1 三倍笔划
SMALL_FONT =2 小号字体
SANS_SERIF_FONT=3 无衬线字体
GOTHIC_FONT =4 哥特字体
directiom 显示方向,取值如下:
HORIZ_DIR =0 水平显示
VERT_DIR =1 垂直显示
charsize 字体放大倍数,取值为0-10
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void xat(int x,int y);
char *hjust[]={"LEFT_TEXT","CENTER_TEXT","RIGHT_TEXT"};
char *vjust[]={"LEFT_TEXT","CENTER_TEXT","RIGHT_TEXT"};
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy,hj,vj;
char msg[80];
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
for (hj=LEFT_TEXT;hj<=RIGHT_TEXT;hj++)
for (vj=LEFT_TEXT;vj<=RIGHT_TEXT;vj++)
{
cleardevice();
settextjustify(hj,vj);
sprintf(msg,"%s %s",hjust[hj],vjust[vj]);
xat(midx,midy);
outtextxy(midx,midy,msg);
getch();
}
closegraph();
return 0;
}
@函数名称: gettextsettings
函数原型: void far gettextsettings(struct textsettingstype far *texttypeinfo)
函数功能: 得到当前图形模式下的文字显示设置信息
函数返回:
参数说明: texttypeinfo-文字显示设置信息,该结构的定义如下:
struct textsettingstype{
int font; /*显示字体*/
int direction; /*方向*/
int charsize; /*大小*/
int horiz; /*水平对齐*/
int vert; /*垂直对齐*/
};
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
char *font[]={"DEFAULT_FONT","TRIPLEX_FONT","SMALL_FONT","SANS_SERIF_FONT","GOTHIC_FONT"};
char *dir[]={"HORIZ_DIR","VERT_DIR"};
char *hjust[]={"LEFT_TEXT","CENTER_TEXT","RIGHT_TEXT"};
char *vjust[]={"BOTTOM_TEXT","CENTER_TEXT","TOP_TEXT"};
int main()
{
int gdriver=DETECT,gmode,errorcode;
struct textsettingstype textinfo;
int midx,midy,ht;
char fontstr[80],dirstr[80],sizestr[80];
char hjuststr[80],vjuststr[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); /* terminate with an error code */
}
midx=getmaxx()/2;
midy=getmaxy()/2;
gettextsettings(&textinfo);
sprintf(fontstr,"%s is the text style.",font[textinfo.font]);
sprintf(dirstr,"%s is the text direction.",dir[textinfo.direction]);
sprintf(sizestr,"%d is the text size.",textinfo.charsize);
sprintf(hjuststr,"%s is the horizontal justification.",hjust[textinfo.horiz]);
sprintf(vjuststr,"%s is the vertical justification.",vjust[textinfo.vert]);
ht=textheight("W");
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(midx,midy,fontstr);
outtextxy(midx,midy+2*ht,dirstr);
outtextxy(midx,midy+4*ht,sizestr);
outtextxy(midx,midy+6*ht,hjuststr);
outtextxy(midx,midy+8*ht,vjuststr);
getch();
closegraph();
return 0;
}
@函数名称: outtext
函数原型: void far outtext(char far *textstring)
函数功能: 图形模式下在当前位置(视口)显示一行字符串
函数返回:
参数说明: textstring-要显示的字符串
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
moveto(midx,midy);
outtext("This");
outtext("is");
outtext("a");
outtext("test.");
getch();
closegraph();
return 0;
}
@函数名称: outtextxy
函数原型: void far outtextxy(int x, int y, char far *textstring)
函数功能: 图形模式下在x,y坐标位置显示一行字符串
函数返回:
参数说明: x,y-显示的位置,textstring-要显示的字符串
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
outtextxy(midx,midy,"This is a test.");
getch();
closegraph();
return 0;
}
@函数名称: registerbgifont
函数原型: int registerbgifont(void (*font)(void))
函数功能: 注册已连接的字体驱动器程序
函数返回:
参数说明: font-字体驱动器函数
所属文件: <graphics.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy,size;
errorcode=registerbgifont(sansserif_font);
if (errorcode<0){
printf("Graphics error: %s/n",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
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;
size=4;
settextjustify(CENTER_TEXT,CENTER_TEXT);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,size);
outtextxy(midx,midy,"SANSSERIF");
getch();
closegraph();
return 0;
}
@函数名称: settextjustify
函数原型: void far settextjustify(int horiz, int vert)
函数功能: 设置图形方式下的字符显示对齐方式
函数返回:
参数说明: horiz 水平对齐模式,vert 垂直对齐模式,含义如下:
水平: 垂直:
LEFT_TEXT =0 左对齐 BOTTOM_TEXT=0 底对齐
CENTER_TEXT=1 中央对齐 CENTER_TEXT=1 中间对齐
RIGHT_TEXT =2 右对齐 TOP_TEXT =2 顶对齐
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void xat(int x, int y);
char *hjust[]={"LEFT_TEXT","CENTER_TEXT","RIGHT_TEXT"};
char *vjust[]={"LEFT_TEXT","CENTER_TEXT","RIGHT_TEXT"};
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy,hj,vj;
char msg[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;
for (hj=LEFT_TEXT;hj<=RIGHT_TEXT;hj++)
for (vj=LEFT_TEXT;vj<=RIGHT_TEXT;vj++)
{
cleardevice();
settextjustify(hj,vj);
sprintf(msg,"%s %s",hjust[hj],vjust[vj]);
xat(midx, midy);
outtextxy(midx, midy, msg);
getch();
}
closegraph();
return 0;
}
/* draw an "x" at (x, y) */
void xat(int x,int y)
{
line(x-4,y,x+4,y);
line(x,y-4,x,y+4);
}
@函数名称: getarccoords
函数原型: void far getarccoords(struct arccoordstype far *coords)
函数功能: 得到最后一次画圆弧的坐标
函数返回:
参数说明: coords-使用函数arc()画圆弧的坐标,该结构如下:
struct arccoordstype{
int m,n; /* 圆心坐标 */
int xstart,ysrart,xend,yend;/* 圆弧起点终点坐标 */
};
所属文件: <graphics.h>
#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",grapherrormsg(errorcode));
printf("ress any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
arc(midx,midy,stangle,endangle,100);
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
函数原型: void far arc(int x, int y, int start, int end, int radius)
函数功能: 画一圆弧线
函数返回:
参数说明: x,y-圆心,radius-半径,start,end-弧线的起始和中止角度
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
int stangle=45,endangle=135;
int radius=100;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("ress any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
arc(midx,midy,stangle,endangle,radius);
getch();
closegraph();
return 0;
}
@函数名称: circle
函数原型: void far circle(int x, int y, int radius)
函数功能: 画圆
函数返回:
参数说明: x,y-圆心坐标,radius-半径
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
int radius=100;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("ress any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
circle(midx,midy,radius);
getch();
closegraph();
return 0;
}
@函数名称: pieslice
函数原型: void far pieslice(int x,int y,int start,int end,int radius)
函数功能: 画扇形
函数返回:
参数说明: x,y-扇形所在园的圆心坐标,start,end-扇形的开始和结束角度,radius-园的半径
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
int stangle=45,endangle=135,radius=100;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setfillstyle(EMPTY_FILL,getmaxcolor());
pieslice(midx,midy,stangle,endangle,radius);
getch();
closegraph();
return 0;
}
@函数名称: getaspectratio,setaspectratio
函数原型: void far getaspectratio(int far *xasp, int far *yasp)
函数功能: 得到图形显示的比例因子
函数返回:
参数说明: xasp-x方向比例因子,yasp-y方向比例因子
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int xasp,yasp,midx,midy;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
getaspectratio(&xasp,&yasp);
circle(midx,midy,100);
getch();
cleardevice();
setaspectratio(xasp/2,yasp);
circle(midx,midy,100);
getch();
cleardevice();
setaspectratio(xasp,yasp/2);
circle(midx,midy,100);
getch();
closegraph();
return 0;
}
@函数名称: sector
函数原型: void far sector(int x,int y,int start,int end,int xrad,int yrad)
函数功能: 画扇形,并填充以预定填充图案和颜色
函数返回:
参数说明: x,y-圆心坐标,start,end-圆弧起点和终点角度,单位度,xrad,yrad-椭圆x,y方向的半径
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver=DETECT,gmode,errorcode;
int midx,midy,i;
int stangle=45,endangle=135;
int xrad=100, yrad=50;
/* initialize graphics and local variables */
initgraph(&gdriver,&gmode,"");
/* read result of initialization */
errorcode=graphresult();
if (errorcode!=grOk) /* an error occurred */
{
printf("Graphics error: %s/n",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx=getmaxx()/2;
midy=getmaxy()/2;
/* loop through the fill patterns */
for(i=EMPTY_FILL;i<USER_FILL;i++)
{
/* set the fill style */
setfillstyle(i,getmaxcolor());
/* draw the sector slice */
sector(midx,midy,stangle,endangle,xrad,yrad);
getch();
}
/* clean up */
closegraph();
return 0;
}
@函数名称: ellipse
函数原型: void far ellipse(int x,int y,int start,int end,int xradius,int yradius)
函数功能: 画椭圆弧
函数返回:
参数说明: x,y-圆心坐标,start,end-圆弧起点和终点角度,单位为度,xradius,yradius-椭圆x,y方向的半径
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
int stangle=0,endangle=360;
int xradius=100,yradius=50;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
ellipse(midx,midy,stangle,endangle,xradius,yradius);
getch();
closegraph();
return 0;
}
@函数名称: fillellipse
函数原型: void far fillellipse(int x, int y, int xradius, int yradius)
函数功能: 画椭圆并用当前的填充色和填充图案填充
函数返回:
参数说明: x,y-圆心坐标,xradius,yradius-椭圆x,y方向的半径
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy,i;
int xradius=100,yradius=50;
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;
/* loop through the fill patterns */
for (i=EMPTY_FILL;i<USER_FILL;i++){
/* set fill pattern */
setfillstyle(i,getmaxcolor());
fillellipse(midx,midy,xradius,yradius);
getch();
}
closegraph();
return 0;
}
@函数名称: getfillsettings
函数原型: void far getfillsettings(struct fillsettingstype far *info)
函数功能: 得到当前填充模式和颜色编号
函数返回:
参数说明: info-当前填充模式和颜色编号,该结构定义如下:
struct fillsettingstype{
int patterns;
int color;
}
其中patterns定义如下:
EMPTY_FILL 0背景色填充 SOLID_FILL 1实填充
LINE_FILL 2--- 线填充 LTSLASH_FILL 3斜线填充
SLASH_FILL 4粗斜线填充 BKSLASH_FILL 5反粗斜线填充
LTBKSLASH_FILL 6反斜线填充 HATCH_FILL 7网格填充
XHATCH_FILL 8斜网格填充 INTERLEAVE_FILL 9间隔点线填充
WIDE_DOT_FILL 10稀疏点填充 CLOSE_DOT_FILL 11密集点填充
USER_FILL 12用户自定义填充
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
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",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;
}
@函数名称: getfillpattern,setfillpattern
函数原型: void far getfillpattern(char far *pattern)
void far setfillpattern(char far *pattern,int color)
函数功能: 得到当前的填充模式/用户自定义填充
函数返回:
参数说明: color-填充颜色,pattern-填充数据数组指针,一般为8个字节,
当该数据上的某位为1时,填充以color所指定的颜色.
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int maxx,maxy;
/* 这是一个8*8点阵的‘Tc’ */
char pattern[8]={0x00,0x70,0x20,0x27,0x24,0x24,0x07,0x00};
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();
pattern[4]-=1;
pattern[5]-=3;
pattern[6]+=3;
pattern[7]-=4;
setfillpattern(pattern,getmaxcolor());
bar(0,0,maxx,maxy);
getch();
closegraph();
return 0;
}
@函数名称: setfillstyle
函数原型: void far setfillstyle(int pattern,int color)
函数功能: 设置填充模式和填充颜色
函数返回:
参数说明: color-填充颜色 pattern-填充模式
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int maxx,maxy;
char pattern[8]={0x00,0x70,0x20,0x27,0x24,0x24,0x07,0x00};
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",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();
closegraph();
return 0;
}
@函数名称: settextstyle
函数原型: void far settextstyle(int font, int direction, int charsize)
函数功能: 设置图形模式下字符显示底字体、方向和大小
函数返回:
参数说明: font-显示字体,取值如下:
DEFAULT_FONT =0 8x8 点阵
TRIPLEX_FONT =1 三倍笔划
SMALL_FONT =2 小号字体
SANS_SERIF_FONT=3 无衬线字体
GOTHIC_FONT =4 哥特字体
directiom 显示方向,取值如下:
HORIZ_DIR =0 水平显示
VERT_DIR =1 垂直显示
charsize 字体放大倍数,取值为0-10
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void xat(int x,int y);
char *hjust[]={"LEFT_TEXT","CENTER_TEXT","RIGHT_TEXT"};
char *vjust[]={"LEFT_TEXT","CENTER_TEXT","RIGHT_TEXT"};
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy,hj,vj;
char msg[80];
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
for (hj=LEFT_TEXT;hj<=RIGHT_TEXT;hj++)
for (vj=LEFT_TEXT;vj<=RIGHT_TEXT;vj++)
{
cleardevice();
settextjustify(hj,vj);
sprintf(msg,"%s %s",hjust[hj],vjust[vj]);
xat(midx,midy);
outtextxy(midx,midy,msg);
getch();
}
closegraph();
return 0;
}
@函数名称: gettextsettings
函数原型: void far gettextsettings(struct textsettingstype far *texttypeinfo)
函数功能: 得到当前图形模式下的文字显示设置信息
函数返回:
参数说明: texttypeinfo-文字显示设置信息,该结构的定义如下:
struct textsettingstype{
int font; /*显示字体*/
int direction; /*方向*/
int charsize; /*大小*/
int horiz; /*水平对齐*/
int vert; /*垂直对齐*/
};
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
char *font[]={"DEFAULT_FONT","TRIPLEX_FONT","SMALL_FONT","SANS_SERIF_FONT","GOTHIC_FONT"};
char *dir[]={"HORIZ_DIR","VERT_DIR"};
char *hjust[]={"LEFT_TEXT","CENTER_TEXT","RIGHT_TEXT"};
char *vjust[]={"BOTTOM_TEXT","CENTER_TEXT","TOP_TEXT"};
int main()
{
int gdriver=DETECT,gmode,errorcode;
struct textsettingstype textinfo;
int midx,midy,ht;
char fontstr[80],dirstr[80],sizestr[80];
char hjuststr[80],vjuststr[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); /* terminate with an error code */
}
midx=getmaxx()/2;
midy=getmaxy()/2;
gettextsettings(&textinfo);
sprintf(fontstr,"%s is the text style.",font[textinfo.font]);
sprintf(dirstr,"%s is the text direction.",dir[textinfo.direction]);
sprintf(sizestr,"%d is the text size.",textinfo.charsize);
sprintf(hjuststr,"%s is the horizontal justification.",hjust[textinfo.horiz]);
sprintf(vjuststr,"%s is the vertical justification.",vjust[textinfo.vert]);
ht=textheight("W");
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(midx,midy,fontstr);
outtextxy(midx,midy+2*ht,dirstr);
outtextxy(midx,midy+4*ht,sizestr);
outtextxy(midx,midy+6*ht,hjuststr);
outtextxy(midx,midy+8*ht,vjuststr);
getch();
closegraph();
return 0;
}
@函数名称: outtext
函数原型: void far outtext(char far *textstring)
函数功能: 图形模式下在当前位置(视口)显示一行字符串
函数返回:
参数说明: textstring-要显示的字符串
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
moveto(midx,midy);
outtext("This");
outtext("is");
outtext("a");
outtext("test.");
getch();
closegraph();
return 0;
}
@函数名称: outtextxy
函数原型: void far outtextxy(int x, int y, char far *textstring)
函数功能: 图形模式下在x,y坐标位置显示一行字符串
函数返回:
参数说明: x,y-显示的位置,textstring-要显示的字符串
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
outtextxy(midx,midy,"This is a test.");
getch();
closegraph();
return 0;
}
@函数名称: registerbgifont
函数原型: int registerbgifont(void (*font)(void))
函数功能: 注册已连接的字体驱动器程序
函数返回:
参数说明: font-字体驱动器函数
所属文件: <graphics.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy,size;
errorcode=registerbgifont(sansserif_font);
if (errorcode<0){
printf("Graphics error: %s/n",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
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;
size=4;
settextjustify(CENTER_TEXT,CENTER_TEXT);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,size);
outtextxy(midx,midy,"SANSSERIF");
getch();
closegraph();
return 0;
}
@函数名称: settextjustify
函数原型: void far settextjustify(int horiz, int vert)
函数功能: 设置图形方式下的字符显示对齐方式
函数返回:
参数说明: horiz 水平对齐模式,vert 垂直对齐模式,含义如下:
水平: 垂直:
LEFT_TEXT =0 左对齐 BOTTOM_TEXT=0 底对齐
CENTER_TEXT=1 中央对齐 CENTER_TEXT=1 中间对齐
RIGHT_TEXT =2 右对齐 TOP_TEXT =2 顶对齐
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void xat(int x, int y);
char *hjust[]={"LEFT_TEXT","CENTER_TEXT","RIGHT_TEXT"};
char *vjust[]={"LEFT_TEXT","CENTER_TEXT","RIGHT_TEXT"};
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy,hj,vj;
char msg[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;
for (hj=LEFT_TEXT;hj<=RIGHT_TEXT;hj++)
for (vj=LEFT_TEXT;vj<=RIGHT_TEXT;vj++)
{
cleardevice();
settextjustify(hj,vj);
sprintf(msg,"%s %s",hjust[hj],vjust[vj]);
xat(midx, midy);
outtextxy(midx, midy, msg);
getch();
}
closegraph();
return 0;
}
/* draw an "x" at (x, y) */
void xat(int x,int y)
{
line(x-4,y,x+4,y);
line(x,y-4,x,y+4);
}