1、图形显示,从文本状态转换成图形状态: #include Option Linker Libraries 激活Graphics Library initgraph和closegraph,图形显示的坐标与象素,使用图形方式时先进行图形系统初始化, 检测或设置指定的图形模式,图形系统的初始化 图形系统初始化函数 void initgraph(int *driver,int *mode,char *pathtodriver) initgraph(驱动程序地址,显示模式地址,驱动程序文件的路径); 其中; driver显示适配器驱动程序的枚举变量 mode图形模式(模式号或模式标识符) pathtodr
2、iver适配器驱动程序的寻找路径,“”表示在当前目录下(*.BGI) enum graphics_driverDETECT,CGA,MCGA,EGA,EGA64,VGA,PC3270; DETECT自动检测所用显示适配器的类型,将相应的驱动程序装入,并将其最高显示模式作为当前显示模式,#include void main() int gdriver=DETECT, gmode;initgraph( ,closegraph();,初始化:,关闭图形方式:,清屏和恢复显示方式函数 void cleardevice(void); void closegraph(void); 基本图形函数 画点:pu
3、tpixel() getpixel() 坐标移动:moveto() moverel() getx() gety() 画线:line() lineto() linerel() 画矩形和条形图:rectangle() bar() 椭圆、圆、圆弧和扇形:ellipse() circle() arc() pieslice() 颜色控制函数 设调色板:setpalette() 设背景色:setbkcolor() 设绘图色:setcolor(),关闭图形系统回到文本模式,几个常用的函数原型,void setcolor(int color); void setbkcolor(int color); void
4、 line(int x1, int y1, int x2, int y2); void rectangle(int left, int top, int right, int bottom); void circle(int x, int y, int radius); void putpixel(int x, int y, int color);,设定线型函数: setlinestyle() 填充函数 设定填充模式:setfillstyle() 常用函数:bar3d() sector() fillellipse() fillpoly() 屏幕操作函数 屏幕图象存储与显示: getimage(
5、) putimage() imagesize() 视口函数 setviewport() clearviewport(),图形方式下的文本输出 文本输出:outtext() outtextxy() 定义文本字型:settextstyle(),#include “stdio.h“ #include “graphics.h“ main() int i,j,driver=VGA,mode=VGAHI; initgraph(,例1:画图,学用circle画圆形,#include “graphics.h“ main() int driver,mode,i; float j=1,k=1; driver=VG
6、A;mode=VGAHI; initgraph( ,例2:画图,学用line画直线,#include “graphics.h“ main() int driver,mode,i; float x0,y0,y1,x1; float j=12,k; driver=VGA;mode=VGAHI; initgraph( ,x0=263;y1=275;y0=263; for(i=0;i=20;i+) setcolor(5); line(x0,y0,x0,y1); x0=x0+5; y0=y0+5; y1=y1-5; ,例3:画图,综合例子,# define PAI 3.1415926 # define
7、B 0.809 # include “graphics.h“ #include “math.h“ main() int i,j,k,x0,y0,x,y,driver,mode;float a;driver=CGA;mode=CGAC0;initgraph(,for(i=0;i16;i+) a=(2*PAI/16)*i; x=ceil(x0+48*cos(a); y=ceil(y0+48*sin(a)*B); setcolor(2); line(x0,y0,x,y); setcolor(3);circle(x0,y0,60); /* Make 0 time normal size letters
8、 */ settextstyle(DEFAULT_FONT,HORIZ_DIR,0); outtextxy(10,170,“press a key“); getch(); setfillstyle(HATCH_FILL,YELLOW); floodfill(202,100,WHITE); getch();,for(k=0;k=500;k+) setcolor(3); for(i=0;i=16;i+) a=(2*PAI/16)*i+(2*PAI/180)*k; x=ceil(x0+48*cos(a); y=ceil(y0+48+sin(a)*B); setcolor(2); line(x0,y0,x,y); for(j=1;j=50;j+) a=(2*PAI/16)*i+(2*PAI/180)*k-1; x=ceil(x0+48*cos(a); y=ceil(y0+48*sin(a)*B); line(x0,y0,x,y); restorecrtmode();,