initgraph函数(讲解)

函数名: initgraph() 
  功 能: 初始化 图形系统  
   函数原型 : void far initgraph(int far *graphdriver, int far *graphmode, 
  char far *pathtodriver);graphdriver是上涨指向图形驱动序号变量的 指针 ;graphmode是在graphdriver选定后,指向图形显示模式序号变量的指针。pathtodriver表示存放图形驱动文件的路径。 
   头文件 : graphics.h  
  ★图形模式的初始化★
  不同的显示器适配器有不同的 图形分辨率 。即是同一显示器适配器, 在不同
  模式下也有不同分辨率。因此, 在屏幕作图之前, 必须根据显示器适配器种类将
  显示器设置成为某种图形模式, 在未设置图形模式之前, 微机系统默认屏幕为文
  本模式(80列, 25行字符模式), 此时所有图形函数均不能工作。设置屏幕为图形
  模式, 可用下列图形初始化函数:
  void far initgraph(int far *gdriver, int far *gmode, char *path);
  其中gdriver和gmode分别表示图形驱动器和模式, path是指图形 驱动程序
  在的 目录路径 。有关图形驱动器、图形模式的符号常数及对应的分辨率见表2。
  图形驱动程序由Turbo C出版商提供,  文件扩展名 为.BGI。根据不同的图形
  适配器有不同的图形驱动程序。例如对于EGA、 VGA 图形适配器就调用驱动程序
  EGAVGA.BGI。 
  表2. 图形驱动器、模式的符号常数及数值
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  图形驱动器(gdriver) 图形模式(gmode)
  ─────────── ─────────── 色调 分辨率
  符号常数 数值 符号常数 数值
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  CGA 1 CGAC0 0 C0 320*200
  CGAC1 1 C1 320*200
  CGAC2 2 C2 320*200
  CGAC3 3 C3 320*200
  CGAHI 4 2色 640*200
  ───────────────────────────────────
  MCGA 2 MCGAC0 0 C0 320*200
  MCGAC1 1 C1 320*200
  MCGAC2 2 C2 320*200
  MCGAC3 3 C3 320*200
  MCGAMED 4 2色 640*200
  MCGAHI 5 2色 640*480
  ───────────────────────────────────
  EGA 3 EGALO 0 16色 640*200
  EGAHI 1 16色 640*350
  ───────────────────────────────────
  EGA64 4 EGA64LO 0 16色 640*200
  EGA64HI 1 4色 640*350
  ───────────────────────────────────
  EGAMON 5 EGAMONHI 0 2色 640*350
  ───────────────────────────────────
  IBM8514 6 IBM8514LO 0 256色 640*480
  IBM8514HI 1 256色 1024*768
  ───────────────────────────────────
  HERC 7 HERCMONOHI 0 2色 720*348
  ───────────────────────────────────
  ATT400 8 ATT400C0 0 C0 320*200
  ATT400C1 1 C1 320*200
  ATT400C2 2 C2 320*200
  ATT400C3 3 C3 320*200
  ATT400MED 4 2色 320*200
  ATT400HI 5 2色 320*200
  ───────────────────────────────────
  VGA 9 VGALO 0 16色 640*200
  VGAMED 1 16色 640*350
  VGAHI 2 16色 640*480
  ───────────────────────────────────
  PC3270 10 PC3270HI 0 2色 720*350
  ───────────────────────────────────
  DETECT 0 用于硬件测试
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 
  例4. 使用图形初始化函数设置VGA高分辨率图形模式
  #include <graphics.h>
  int main()
  {
  int gdriver, gmode;
  gdriver=VGA;
  gmode=VGAHI;
  initgraph(&gdriver, &gmode, "c:\\tc");
  bar3d(100, 100, 300, 250, 50, 1); /*画一长方体*/
  getch();
  closegraph();
  return 0;
  } 
  有时编程者并不知道所用的图形显示器适配器种类, 或者需要将编写的程序
  用于不同图形驱动器, Turbo C提供了一个自动检测显示器硬件的函数, 其调用
  格式为:
  void far detectgraph(int *gdriver, *gmode);
  其中gdriver和gmode的意义与上面相同。
  例5. 自动进行硬件测试后进行图形初始化
  #include <graphics.h>
  int main()
  {
  int gdriver, gmode;
  detectgraph(&gdriver, &gmode); /* 自动测试 硬件*/
  printf("the graphics driver is %d, mode is %d\n", gdriver,
  gmode); /*输出测试结果*/
  getch();
  initgraph(&gdriver, &gmode, "c:\\tc");
  /* 根据测试结果初始化图形*/
  bar3d(10, 10, 130, 250, 20, 1);
  getch();
  closegraph();
  return 0;
  } 
  上例程序中先对图形显示器自动检测, 然后再用图形初始化函数进行初始化
  设置, 但Turbo C提供了一种更简单的方法, 即用gdriver= DETECT 语句后再跟
  initgraph()函数就行了。采用这种方法后, 上例可改为:
  例6.
  #include <graphics.h>
  int main()
  {
  int gdriver=DETECT, gmode;
  initgraph(&gdriver, &gmode, "c:\\tc");
  bar3d(50, 50, 150, 30, 1);
  getch();
  closegraph();
  return 0;
  }
  另外, Turbo C提供了退出图形状态的函数closegraph(), 其调用格式为:
  void far closegraph(void);
  调用该函数后可退出图形状态而进入文本方式(Turbo C 默认方式), 并释放
  用于保存图形驱动程序和字体的 系统内存
  程序例: 
  #include "graphics.h"
  #include "stdio.h"
  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; 

  }


initgraph

这个函数用于初始化绘图环境。

HWND initgraph(
    int width,
    int height,
    int flag = NULL
);

参数:

width

绘图环境的宽度。

height

绘图环境的高度。

flag

绘图环境的样式,默认为 NULL。可为以下值:

含义
NOCLOSE禁用绘图环境的关闭按钮。
NOMINIMIZE禁用绘图环境的最小化按钮。
SHOWCONSOLE保留原控制台窗口。

返回值:

创建的绘图窗口的句柄。

示例:

以下局部代码创建一个尺寸为 640x480 的绘图环境:

initgraph(640, 480);

以下局部代码创建一个尺寸为 640x480 的绘图环境,同时显示控制台窗口:

initgraph(640, 480, SHOWCONSOLE);

以下局部代码创建一个尺寸为 640x480 的绘图环境,同时显示控制台窗口,并禁用关闭按钮:

initgraph(640, 480, SHOWCONSOLE | NOCLOSE);

  • 36
    点赞
  • 153
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言函数及相关知识 函数名: abort 功 能: 异常终止一个进程 用 法: void abort(void); 程序例: #include <stdio.h> #include <stdlib.h> int main(void) { printf("Calling abort()\n"); abort(); return 0; /* This is never reached */ } 函数名: abs 功 能: 求整数的绝对值 用 法: int abs(int i); 程序例: #include <stdio.h> #include <math.h> int main(void) { int number = -1234; printf("number: %d absolute value: %d\n", number, abs(number)); return 0; } 函数名: absread, abswirte 功 能: 绝对磁盘扇区读、写数据 用 法: int absread(int drive, int nsects, int sectno, void *buffer); int abswrite(int drive, int nsects, in tsectno, void *buffer); 程序例: /* absread example */ #include <stdio.h> #include <conio.h> #include <process.h> #include <dos.h> int main(void) { int i, strt, ch_out, sector; char buf[512]; printf("Insert a diskette into drive A and press any key\n"); getch(); sector = 0; if (absread(0, 1, sector, &buf) != 0) { perror("Disk problem"); exit(1); } printf("Read OK\n"); strt = 3; for (i=0; i<80; i++) { ch_out = buf[strt+i]; putchar(ch_out); } printf("\n"); return(0); } 函数名: access 功 能: 确定文件的访问权限 用 法: int access(const char *filename, int amode); 程序例: #include <stdio.h> #include <io.h> int file_exists(char *filename); int main(void) { printf("Does NOTEXIST.FIL exist: %s\n", file_exists("NOTEXISTS.FIL") ? "YES" : "NO"); return 0; } int file_exists(char *filename) { return (access(filename, 0) == 0); } 函数名: acos 功 能: 反余弦函数 用 法: double acos(double x); 程序例: #include <stdio.h> #include <math.h> int main(void) { double result; double x = 0.5; result = acos(x); printf("The arc cosine of %lf is %lf\n", x, result); return 0; } 函数名: allocmem 功 能: 分配DOS存储段 用 法: int allocmem(unsigned size, unsigned *seg); 程序例: #include <dos.h> #include <alloc.h> #include <stdio.h> int main(void) { unsigned int size, segp; int stat; size = 64; /* (64 x 16) = 1024 bytes */ stat = allocmem(size, &segp); if (stat == -1) printf("Allocated memory at segment: %x\n", segp); else printf("Failed: maximum number of paragraphs available is %u\n", stat); return 0; } 函数名: arc 功 能: 画一弧线 用 法: void far arc(int x, int y, int stangle, int endangle, int radius); 程序例: #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; int stangle = 45, endangle = 135; int radius = 100; /* 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); /* terminate with an error code */ } midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor()); /* draw arc */ arc(midx, midy, stangle, endangle, radius); /* clean up */ getch(); closegraph(); return 0; } 函数名: asctime 功 能: 转换日期和时间为ASCII码 用 法: char *asctime(const struct tm *tblock); 程序例: #include <stdio.h> #include <string.h> #include <time.h> int main(void) { struct tm t; char str[80]; /* sample loading of tm structure */ t.tm_sec = 1; /* Seconds */ t.tm_min = 30; /* Minutes */ t.tm_hour = 9; /* Hour */ t.tm_mday = 22; /* Day of the Month */ t.tm_mon = 11; /* Month */ t.tm_year = 56; /* Year - does not include century */ t.tm_wday = 4; /* Day of the week */ t.tm_yday = 0; /* Does not show in asctime */ t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */ /* converts structure to null terminated string */ strcpy(str, asctime(&t)); printf("%s\n", str); return 0; } 函数名: asin 功 能: 反正弦函数 用 法: double asin(double x); 程序例: #include <stdio.h> #include <math.h> int main(void) { double result; double x = 0.5; result = asin(x); printf("The arc sin of %lf is %lf\n", x, result); return(0); } 函数名: assert 功 能: 测试一个条件并可能使程序终止 用 法: void assert(int test); 程序例: #include <assert.h> #include <stdio.h> #include <stdlib.h> struct ITEM { int key; int value; }; /* add item to list, make sure list is not null */ void additem(struct ITEM *itemptr) { assert(itemptr != NULL); /* add item to list */ } int main(void) { additem(NULL); return 0; } 函数名: atan 功 能: 反正切函数 用 法: double atan(double x); 程序例: #include <stdio.h> #include <math.h> int main(void) { double result; double x = 0.5; result = atan(x); printf("The arc tangent of %lf is %lf\n", x, result); return(0); } 函数名: atan2 功 能: 计算Y/X的反正切值 用 法: double atan2(double y, double x); 程序例: #include <stdio.h> #include <math.h> int main(void) { double result; double x = 90.0, y = 45.0; result = atan2(y, x); printf("The arc tangent ratio of %lf is %lf\n", (y / x), result); return 0; } 函数名: atexit 功 能: 注册终止函数 用 法: int atexit(atexit_t func); 程序例: #include <stdio.h> #include <stdlib.h> void exit_fn1(void) { printf("Exit function #1 called\n"); } void exit_fn2(void) { printf("Exit function #2 called\n"); } int main(void) { /* post exit function #1 */ atexit(exit_fn1); /* post exit function #2 */ atexit(exit_fn2); return 0; } 函数名: atof 功 能: 把字符串转换成浮点数 用 法: double atof(const char *nptr); 程序例: #include <stdlib.h> #include <stdio.h> int main(void) { float f; char *str = "12345.67"; f = atof(str); printf("string = %s float = %f\n", str, f); return 0; } 函数名: atoi 功 能: 把字符串转换成长整型数 用 法: int atoi(const char *nptr); 程序例: #include <stdlib.h> #include <stdio.h> int main(void) { int n; char *str = "12345.67"; n = atoi(str); printf("string = %s integer = %d\n", str, n); return 0; } 函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *nptr); 程序例: #include <stdlib.h> #include <stdio.h> int main(void) { long l; char *str = "98765432"; l = atol(lstr); printf("string = %s integer = %ld\n", str, l); return(0); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值