⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 王大刚--c语言编程宝典--g.htm

📁 初学者的良师益友。其中包括C的全部教程。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
      getmodename(mode)); <BR>
      <P>/* display the information */ <BR>&nbsp;&nbsp; 
      settextjustify(CENTER_TEXT, CENTER_TEXT); <BR>&nbsp;&nbsp; outtextxy(midx, 
      midy, numname); <BR>&nbsp;&nbsp; outtextxy(midx, midy+2*textheight("W"), 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      modename); <BR>
      <P>/* clean up */ <BR>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; closegraph(); 
      <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: getimage <BR>功&nbsp; 能: 将指定区域的一个位图存到主存中 <BR>用&nbsp; 法: void far 
      getimage(int left, int top, int right, int bottom, 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp; void far *bitmap); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>#include &lt;alloc.h&gt; 
      <BR>
      <P>void save_screen(void far *buf[4]); <BR>void restore_screen(void far 
      *buf[4]); <BR>
      <P>int maxx, maxy; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; int gdriver=DETECT, gmode, 
      errorcode; <BR>&nbsp;&nbsp; void far *ptr[4]; <BR>
      <P>&nbsp;&nbsp; /* auto-detect the graphics driver and mode */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>&nbsp;&nbsp; 
      errorcode = graphresult(); /* check for any errors */ <BR>&nbsp;&nbsp; if 
      (errorcode != grOk) <BR>&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Graphics error: %s\n", grapherrormsg(errorcode)); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Press any key to halt:"); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch(); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); <BR>&nbsp;&nbsp; } 
      <BR>&nbsp;&nbsp; maxx = getmaxx(); <BR>&nbsp;&nbsp; maxy = getmaxy(); <BR>
      <P>&nbsp;&nbsp; /* draw an image on the screen */ <BR>&nbsp;&nbsp; 
      rectangle(0, 0, maxx, maxy); <BR>&nbsp;&nbsp; line(0, 0, maxx, maxy); 
      <BR>&nbsp;&nbsp; line(0, maxy, maxx, 0); <BR>
      <P>&nbsp;&nbsp; save_screen(ptr);&nbsp;&nbsp;&nbsp; /* save the current 
      screen */ <BR>&nbsp;&nbsp; 
      getch();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      /* pause screen */ <BR>&nbsp;&nbsp; 
      cleardevice();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* clear screen */ 
      <BR>&nbsp;&nbsp; restore_screen(ptr); /* restore the screen */ 
      <BR>&nbsp;&nbsp; 
      getch();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      /* pause screen */ <BR>
      <P>&nbsp;&nbsp; closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>
      <P>void save_screen(void far *buf[4]) <BR>{ <BR>&nbsp;&nbsp; unsigned 
      size; <BR>&nbsp;&nbsp; int ystart=0, yend, yincr, block; <BR>
      <P>&nbsp;&nbsp; yincr = (maxy+1) / 4; <BR>&nbsp;&nbsp; yend = yincr; 
      <BR>&nbsp;&nbsp; size = imagesize(0, ystart, maxx, yend); /* get byte size 
      of image */ <BR>
      <P>&nbsp;&nbsp; for (block=0; block&lt;=3; block++) <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((buf[block] = farmalloc(size)) == 
      NULL) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; closegraph(); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Error: not 
      enough heap space in save_screen().\n"); <BR>&nbsp; exit(1); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getimage(0, ystart, maxx, yend, 
      buf[block]); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ystart = yend + 1; 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yend += yincr + 1; <BR>&nbsp;&nbsp; } 
      <BR>} <BR>
      <P>void save_screen(void far *buf[4]) <BR>{ <BR>&nbsp;&nbsp; unsigned 
      size; <BR>&nbsp;&nbsp; int ystart=0, yend, yincr, block; <BR>
      <P>&nbsp;&nbsp; yincr = (maxy+1) / 4; <BR>&nbsp;&nbsp; yend = yincr; 
      <BR>&nbsp;&nbsp; size = imagesize(0, ystart, maxx, yend); /* get byte size 
      of image */ <BR>
      <P>&nbsp;&nbsp; for (block=0; block&lt;=3; block++) <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((buf[block] = farmalloc(size)) == 
      NULL) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; closegraph(); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Error: not 
      enough heap space in save_screen().\n"); <BR>&nbsp; exit(1); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getimage(0, ystart, maxx, yend, 
      buf[block]); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ystart = yend + 1; 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yend += yincr + 1; <BR>&nbsp;&nbsp; } 
      <BR>} <BR>
      <P>void restore_screen(void far *buf[4]) <BR>{ <BR>&nbsp;&nbsp; int 
      ystart=0, yend, yincr, block; <BR>
      <P>&nbsp;&nbsp; yincr = (maxy+1) / 4; <BR>&nbsp;&nbsp; yend = yincr; <BR>
      <P>&nbsp;&nbsp; for (block=0; block&lt;=3; block++) <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putimage(0, ystart, buf[block], 
      COPY_PUT); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; farfree(buf[block]); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ystart = yend + 1; 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yend += yincr + 1; <BR>&nbsp;&nbsp; } 
      <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: getlinesettings <BR>功&nbsp; 能: 取当前线型、模式和宽度 <BR>用&nbsp; 法: void far 
      getlinesettings(struct linesettingstype far *lininfo): <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>/* the names of the line styles supported */ <BR>char *lname[] = { 
      "SOLID_LINE", 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      "DOTTED_LINE", 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      "CENTER_LINE", 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      "DASHED_LINE", 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      "USERBIT_LINE" 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      }; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request auto detection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>&nbsp;&nbsp; 
      struct linesettingstype lineinfo; <BR>&nbsp;&nbsp; int midx, midy; 
      <BR>&nbsp;&nbsp; char lstyle[80], lpattern[80], lwidth[80]; <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>
      <P>&nbsp;&nbsp; /* read result of initialization */ <BR>&nbsp;&nbsp; 
      errorcode = graphresult(); <BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; 
      /* an error occurred */ <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", 
      grapherrormsg(errorcode)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Press any key to halt:"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      getch(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an 
      error code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; midx = getmaxx() / 2; <BR>&nbsp;&nbsp; midy = getmaxy() / 
      2; <BR>
      <P>&nbsp;&nbsp; /* get information about current line settings */ 
      <BR>&nbsp;&nbsp; getlinesettings(&amp;lineinfo); <BR>
      <P>&nbsp;&nbsp; /* convert line information into strings */ 
      <BR>&nbsp;&nbsp; sprintf(lstyle, "%s is the line style.", 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      lname[lineinfo.linestyle]); <BR>&nbsp;&nbsp; sprintf(lpattern, "0x%X is 
      the user-defined line pattern.", 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      lineinfo.upattern); <BR>&nbsp;&nbsp; sprintf(lwidth, "%d is the line 
      thickness.", <BR>&nbsp;&nbsp;&nbsp; lineinfo.thickness); <BR>
      <P>&nbsp;&nbsp; /* display the information */ <BR>&nbsp;&nbsp; 
      settextjustify(CENTER_TEXT, CENTER_TEXT); <BR>&nbsp;&nbsp; outtextxy(midx, 
      midy, lstyle); <BR>&nbsp;&nbsp; outtextxy(midx, midy+2*textheight("W"), 
      lpattern); <BR>&nbsp;&nbsp; outtextxy(midx, midy+4*textheight("W"), 
      lwidth); <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; 
      closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: getmaxcolor <BR>功&nbsp; 能: 返回可以传给函数setcolor的最大颜色值 <BR>用&nbsp; 法: 
      int far getmaxcolor(void); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request auto detection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>&nbsp;&nbsp; 
      int midx, midy; <BR>&nbsp;&nbsp; char colstr[80]; <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables <BR>&nbsp; */ 
      initgraph(&amp;gdriver, &amp;gmode, ""); <BR>
      <P>&nbsp;&nbsp; /* read result of initialization */ <BR>&nbsp;&nbsp; 
      errorcode = graphresult(); <BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; 
      /* an error occurred */ <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", 
      grapherrormsg(errorcode)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Press any key to halt:"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      getch(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an 
      error code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; midx = getmaxx() / 2; <BR>&nbsp;&nbsp; midy = getmaxy() / 
      2; <BR>
      <P>&nbsp;&nbsp; /* grab the color info. and convert it to a string */ 
      <BR>&nbsp;&nbsp; sprintf(colstr, "This mode supports colors 0..%d", 
      getmaxcolor()); <BR>
      <P>&nbsp;&nbsp; /* display the information */ <BR>&nbsp;&nbsp; 
      settextjustify(CENTER_TEXT, CENTER_TEXT); <BR>&nbsp;&nbsp; outtextxy(midx, 
      midy, colstr); <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; 
      closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; 
      <BR>&nbsp; <BR>
      <P>函数名: getmaxx <BR>功&nbsp; 能: 返回屏幕的最大x坐标 <BR>用&nbsp; 法: int far 
      getmaxx(void); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request auto detection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>&nbsp;&nbsp; 
      int midx, midy; <BR>&nbsp;&nbsp; char xrange[80], yrange[80]; <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>
      <P>&nbsp;&nbsp; /* read result of initialization */ <BR>&nbsp;&nbsp; 
      errorcode = graphresult(); <BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; 
      /* an error occurred */ <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", 
      grapherrormsg(errorcode)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Press any key to halt:"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      getch(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an 
      error code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; midx = getmaxx() / 2; <BR>&nbsp;&nbsp; midy = getmaxy() / 
      2; <BR>
      <P>&nbsp;&nbsp; /* convert max resolution values into strings */ 
      <BR>&nbsp;&nbsp; sprintf(xrange, "X values range from 0..%d", getmaxx()); 
      <BR>&nbsp;&nbsp; sprintf(yrange, "Y values range from 0..%d", getmaxy()); 
      <BR>
      <P>&nbsp;&nbsp; /* display the information */ <BR>&nbsp;&nbsp; 
      settextjustify(CENTER_TEXT, CENTER_TEXT); <BR>&nbsp;&nbsp; outtextxy(midx, 
      midy, xrange); <BR>&nbsp;&nbsp; outtextxy(midx, midy+textheight("W"), 
      yrange); <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; 
      closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: getmaxy <BR>功&nbsp; 能: 返回屏幕的最大y坐标 <BR>用&nbsp; 法: int far 
      getmaxy(void); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request auto detection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>&nbsp;&nbsp; 
      int midx, midy; <BR>&nbsp;&nbsp; char xrange[80], yrange[80]; <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -