最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

C语言 图形库函数

来源:动视网 责编:小OO 时间:2025-10-02 19:07:15
文档

C语言 图形库函数

1.初始化图形系统 函数名:initgraph功 能:初始化图形系统用 法:voidfarinitgraph(intfar*graphdriver,intfar*graphmode,   charfar*pathtodriver);程序例:#include#include#include#includeintmain(void){  /*requestautodetection*/  intgdriver=DETECT,gmode,errorcode;  /*initializegraphic
推荐度:
导读1.初始化图形系统 函数名:initgraph功 能:初始化图形系统用 法:voidfarinitgraph(intfar*graphdriver,intfar*graphmode,   charfar*pathtodriver);程序例:#include#include#include#includeintmain(void){  /*requestautodetection*/  intgdriver=DETECT,gmode,errorcode;  /*initializegraphic
1. 初始化图形系统  

函数名: initgraph 

功  能: 初始化图形系统 

用  法: void far initgraph(int far *graphdriver, int far *graphmode, 

    char far *pathtodriver); 

程序例: 

#include

#include

#include

#include

int main(void) 

   /* request auto detection */ 

   int gdriver = DETECT, gmode, errorcode; 

   /* initialize graphics mode */ 

   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);             /* return with error code */ 

   } 

   /* draw a line */ 

   line(0, 0, getmaxx(), getmaxy()); 

   /* clean up */ 

   getch(); 

   closegraph(); 

   return 0; 

  

  

2.   

函数名: drawpoly 

功  能: 画多边形 

用  法: void far drawpoly(int numpoints, int far *polypoints); 

程序例: 

#include

#include

#include

#include

int main(void) 

   /* request auto detection */ 

   int gdriver = DETECT, gmode, errorcode; 

   int maxx, maxy; 

   /* our polygon array */ 

   int poly[10]; 

   /* 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(); 

   /* terminate with an error code */ 

      exit(1); 

   } 

   maxx = getmaxx(); 

   maxy = getmaxy(); 

   poly[0] = 20;        /* 1st vertext */ 

   poly[1] = maxy / 2; 

   poly[2] = maxx - 20; /* 2nd */ 

   poly[3] = 20; 

   poly[4] = maxx - 50; /* 3rd */ 

   poly[5] = maxy - 20; 

   poly[6] = maxx / 2;  /* 4th */ 

   poly[7] = maxy / 2; 

/* 

   drawpoly doesn't automatically close 

   the polygon, so we close it. 

*/ 

   poly[8] = poly[0]; 

   poly[9] = poly[1]; 

   /* draw the polygon */ 

   drawpoly(5, poly); 

   /* clean up */ 

   getch(); 

   closegraph(); 

   return 0; 

  

  

  

函数名: ellipse 

功  能: 画一椭圆 

用  法: void far ellipse(int x, int y, int stangle, int endangle, 

    int xradius, int yradius); 

程序例: 

#include

#include

#include

#include

int main(void) 

   /* request auto detection */ 

   int gdriver = DETECT, gmode, errorcode; 

   int midx, midy; 

   int stangle = 0, endangle = 360; 

   int xradius = 100, yradius = 50; 

   /* initialize graphics, 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; 

   setcolor(getmaxcolor()); 

   /* draw ellipse */ 

   ellipse(midx, midy, stangle, endangle, 

    xradius, yradius); 

   /* clean up */ 

   getch(); 

   closegraph(); 

   return 0; 

  

  

  

函数名: fillellipse 

功  能: 画出并填充一椭圆 

用  法: void far fillellipse(int x, int y, int xradius, int yradius); 

程序例: 

#include

#include

int main(void) 

   int gdriver = DETECT, gmode; 

   int xcenter, ycenter, i; 

   initgraph(&gdriver,&gmode,""); 

   xcenter = getmaxx() / 2; 

   ycenter = getmaxy() / 2; 

   for (i=0; i<13; i++)

   { 

      setfillstyle(i,WHITE); 

      fillellipse(xcenter,ycenter,100,50); 

      getch(); 

   } 

   closegraph(); 

   return 0; 

  

  

  

  

函数名: getbkcolor 

功  能: 返回当前背景颜色 

用  法: int far getbkcolor(void); 

程序例: 

#include

#include

#include

#include

#include

int main(void) 

   /* request auto detection */ 

   int gdriver = DETECT, gmode, errorcode; 

   int bkcolor, midx, midy; 

   char bkname[35]; 

/* initialize graphics and local variables */ 

   initgraph(&gdriver, &gmode, ""); 

/* read result of initialization */ 

   errorcode = graphresult(); 

/* an error occurred */ 

   if (errorcode != grOk) 

   { 

      printf("Graphics error: %s\\n", 

             grapherrormsg(errorcode)); 

      printf("Press any key to halt:"); 

      getch(); 

/* terminate with an error code */ 

      exit(1); 

   } 

   midx = getmaxx() / 2; 

   midy = getmaxy() / 2; 

   setcolor(getmaxcolor()); 

/* for centering text on the display */ 

   settextjustify(CENTER_TEXT, CENTER_TEXT); 

/* get the current background color */ 

   bkcolor = getbkcolor(); 

/* convert color value into a string */ 

   itoa(bkcolor, bkname, 10); 

   strcat(bkname, 

 " is the current background color."); 

/* display a message */ 

   outtextxy(midx, midy, bkname); 

/* clean up */ 

   getch(); 

   closegraph(); 

   return 0; 

  

  

  

  

函数名: line 

功  能: 在指定两点间画一直线 

用  法: void far line(int x0, int y0, int x1, int y1); 

程序例: 

#include

#include

#include

#include

int main(void) 

   /* request auto detection */ 

   int gdriver = DETECT, gmode, errorcode; 

   int xmax, ymax; 

   /* initialize graphics and local variables */ 

   initgraph(&gdriver, &gmode, ""); 

   /* read result of initialization */ 

   errorcode = graphresult(); 

   /* an error occurred */ 

   if (errorcode != grOk) 

   { 

      printf("Graphics error: %s\\n", 

             grapherrormsg(errorcode)); 

      printf("Press any key to halt:"); 

      getch(); 

      exit(1); 

   } 

   setcolor(getmaxcolor()); 

   xmax = getmaxx(); 

   ymax = getmaxy(); 

   /* draw a diagonal line */ 

   line(0, 0, xmax, ymax); 

   /* clean up */ 

   getch(); 

   closegraph(); 

   return 0; 

  

  

函数名: rectangle 

功  能: 画一个矩形 

用  法: void far rectangle(int left, int top, int right, int bottom);

  

函数名: setbkcolor 

功  能: 用调色板设置当前背景颜色 

用  法: void far setbkcolor(int color); 

程序例: 

#include

#include

#include

#include

int main(void) 

   /* select a driver and mode that supports */ 

   /* multiple background colors.            */ 

   int gdriver = EGA, gmode = EGAHI, errorcode; 

   int bkcol, maxcolor, x, y; 

   char msg[80]; 

   /* 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 */ 

   } 

   /* maximum color index supported */ 

   maxcolor = getmaxcolor(); 

   /* for centering text messages */ 

   settextjustify(CENTER_TEXT, CENTER_TEXT); 

   x = getmaxx() / 2; 

   y = getmaxy() / 2; 

   /* loop through the available colors */ 

   for (bkcol=0; bkcol<=maxcolor; bkcol++)

   { 

      /* clear the screen */ 

      cleardevice(); 

      /* select a new background color */ 

      setbkcolor(bkcol); 

      /* output a messsage */ 

      if (bkcol == WHITE) 

  setcolor(EGA_BLUE); 

      sprintf(msg, "Background color: %d", bkcol); 

      outtextxy(x, y, msg); 

      getch(); 

   } 

   /* clean up */ 

   closegraph(); 

   return 0; 

  

  

  

函数名: setcolor 

功  能: 设置当前画线颜色 

用  法: void far setcolor(int color); 

程序例: 

#include

#include

#include

#include

int main(void) 

   /* select a driver and mode that supports */ 

   /* multiple drawing colors.               */ 

   int gdriver = EGA, gmode = EGAHI, errorcode; 

   int color, maxcolor, x, y; 

   char msg[80]; 

   /* 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 */ 

   } 

   /* maximum color index supported */ 

   maxcolor = getmaxcolor(); 

   /* for centering text messages */ 

   settextjustify(CENTER_TEXT, CENTER_TEXT); 

   x = getmaxx() / 2; 

   y = getmaxy() / 2; 

   /* loop through the available colors */ 

   for (color=1; color<=maxcolor; color++)

   { 

      /* clear the screen */ 

      cleardevice(); 

      /* select a new background color */ 

      setcolor(color); 

      /* output a messsage */ 

      sprintf(msg, "Color: %d", color); 

      outtextxy(x, y, msg); 

      getch(); 

   } 

   /* clean up */ 

   closegraph(); 

   return 0; 

  

  

_   

函数名: setfillstyle 

功  能: 设置填充模式和颜色 

用  法: void far setfillstyle(int pattern, int color); 

程序例: 

#include

#include

#include

#include

#include

/* 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(void) 

   /* request auto detection */ 

   int gdriver = DETECT, gmode, errorcode; 

   int style, midx, midy; 

   char stylestr[40]; 

   /* 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; 

   for (style = EMPTY_FILL; style < USER_FILL; style++)

   { 

      /* select the fill style */ 

      setfillstyle(style, getmaxcolor()); 

      /* convert style into a string */ 

      strcpy(stylestr, fname[style]); 

      /* fill a bar */ 

      bar3d(0, 0, midx-10, midy, 0, 0); 

      /* output a message */ 

      outtextxy(midx, midy, stylestr); 

      /* wait for a key */ 

      getch(); 

      cleardevice(); 

   } 

   /* clean up */ 

   getch(); 

   closegraph(); 

   return 0; 

  

  

  

函数名: setlinestyle 

功  能: 设置当前画线宽度和类型 

用  法: void far setlinestyle(int linestype, unsigned upattern); 

程序例: 

#include

#include

#include

#include

#include

/* the names of the line styles supported */ 

char *lname[] = { 

   "SOLID_LINE", 

   "DOTTED_LINE", 

   "CENTER_LINE", 

   "DASHED_LINE", 

   "USERBIT_LINE" 

   }; 

int main(void) 

   /* request auto detection */ 

   int gdriver = DETECT, gmode, errorcode; 

   int style, midx, midy, userpat; 

   char stylestr[40]; 

   /* 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; 

   /* a user defined line pattern */ 

   /* binary: "0000000000000001"  */ 

   userpat = 1; 

   for (style=SOLID_LINE; style<=USERBIT_LINE; style++)

   { 

      /* select the line style */ 

      setlinestyle(style, userpat, 1); 

      /* convert style into a string */ 

      strcpy(stylestr, lname[style]); 

      /* draw a line */ 

      line(0, 0, midx-10, midy); 

      /* draw a rectangle */ 

      rectangle(0, 0, getmaxx(), getmaxy()); 

      /* output a message */ 

      outtextxy(midx, midy, stylestr); 

      /* wait for a key */ 

      getch(); 

      cleardevice(); 

   } 

   /* clean up */ 

   closegraph(); 

   return 0; 

  

  

  

文档

C语言 图形库函数

1.初始化图形系统 函数名:initgraph功 能:初始化图形系统用 法:voidfarinitgraph(intfar*graphdriver,intfar*graphmode,   charfar*pathtodriver);程序例:#include#include#include#includeintmain(void){  /*requestautodetection*/  intgdriver=DETECT,gmode,errorcode;  /*initializegraphic
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top