c 语言中的图形模式

本文介绍了C语言中Turbo C的图形模式初始化方法,包括如何使用initgraph函数设置不同的图形驱动器和模式,并展示了自动检测硬件和设置高分辨率图形模式的示例代码。

为了弄清c语言的图形模式的函数,在网上查了些资料,在此声明以下的资料完全来自于网上,放在这里与大家共享。

    Turbo C提供了非常丰富的图形函数, 所有图形函数的原型均在graphics. h中, 本节主要介绍图形模式的初始化、独立图形程序的建立、基本图形功能、图形窗口以及图形模式下的文本输出等函数。另外, 使用图形函数时要确保有显示器图形驱动程序*BGI, 同时将集成开发环境Options/Linker中的Graphics lib选为on, 只有这样才能保证正确使用图形函数。

1. 图形模式的初始化
   不同的显示器适配器有不同的图形分辨率。即是同一显示器适配器, 在不同模式下也有不同分辨率。因此, 在屏幕作图之前, 必须根据显示器适配器种类将显示器设置成为某种图形模式, 在未设置图形模式之前, 微机系统默认屏幕为文本模式(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        

MCGAC0  C0    320*200
MCGAC1  C1    320*200
MCGAC2  C2    320*200
MCGAC3  C3    320*200
MCGAMED 4  2色   640*200
MCGAHI  2色   640*480
———————————————————————————————————
  EGA         

EGALO 0 16色   640*200
EGAHI 1 16色   640*350
———————————————————————————————————
  EGA64       

EGA64LO 0 16色   640*200
EGA64HI 1  4色   640*350
———————————————————————————————————
  EGAMON      

EGAMONHI0  2色   640*350
———————————————————————————————————
  IBM8514     

IBM8514LO       0256色   640*480
IBM8514HI       1256色  1024*768
———————————————————————————————————
  HERC         7

HERCMONOHI     2色   720*348
———————————————————————————————————
  ATT400      

ATT400C0  C0    320*200
ATT400C1  C1    320*200
ATT400C2  C2    320*200
ATT400C3  C3    320*200
ATT400MED 4  2色   320*200
ATT400HI  2色   320*200
———————————————————————————————————
  VGA         

VGALO   0 16色   640*200
VGAMED  1 16色   640*350
VGAHI   2 16色   640*480
———————————————————————————————————
  PC3270       10

PC3270HI   2色   720*350
———————————————————————————————————
  DETECT        用于硬件测试
例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 默认方式), 并释放用于保存图形驱动程序和字体的系统内存。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style> .AlignLeft { text-align: left; } .AlignCenter { text-align: center; } .AlignRight { text-align: right; } body { font-family: sans-serif; font-size: 11pt; } td { vertical-align: top; padding-left: 4px; padding-right: 4px; } tr.SectionGap td { font-size: 4px; border-left: none; border-top: none; border-bottom: 1px solid Black; border-right: 1px solid Black; } tr.SectionAll td { border-left: none; border-top: none; border-bottom: 1px solid Black; border-right: 1px solid Black; } tr.SectionBegin td { border-left: none; border-top: none; border-right: 1px solid Black; } tr.SectionEnd td { border-left: none; border-top: none; border-bottom: 1px solid Black; border-right: 1px solid Black; } tr.SectionMiddle td { border-left: none; border-top: none; border-right: 1px solid Black; } tr.SubsectionAll td { border-left: none; border-top: none; border-bottom: 1px solid Gray; border-right: 1px solid Black; } tr.SubsectionEnd td { border-left: none; border-top: none; border-bottom: 1px solid Gray; border-right: 1px solid Black; } table.fc { border-top: 1px solid Black; border-left: 1px solid Black; width: 100%; font-family: monospace; font-size: 10pt; } td.TextItemInsigAdd { color: #000000; background-color: #EEEEFF; } td.TextItemInsigDel { color: #000000; background-color: #EEEEFF; text-decoration: line-through; } td.TextItemInsigMod { color: #000000; background-color: #EEEEFF; } td.TextItemInsigOrphan { color: #000000; background-color: #FAEEFF; } td.TextItemNum { color: #696969; background-color: #F0F0F0; } td.TextItemSame { color: #000000; background-color: #FFFFFF; } td.TextItemSigAdd { color: #000000; background-color: #FFE3E3; } td.TextItemSigDel { color: #000000; background-color: #FFE3E3; text-decoration: line-through; } td.TextItemSigMod { color: #000000; background-color: #FFE3E3; } td.TextItemSigOrphan { color: #000000; background-color: #F1E3FF; } .TextSegInsigDiff { color: #0000FF; } .TextSegReplacedDiff { color: #0000FF; font-style: italic; } .TextSegSigDiff { color: #FF0000; } .TextSegElement_20851_38190_23383 { font-weight: bold; } .TextSegElement_35782_21035_31526 { } .TextSegElement_25968_23383 { color: #2E9269; } .TextSegElement_23383_31526_20018 { color: #3A7726; } .TextSegElement_32534_35793_22120_25351_20196 { color: #681717; } .TextSegElement_27880_37322 { color: #786A41; } .TextSegElement_25805_20316_31526 { } </style> <title>GA_D82DD83D_00-00-05 VS GA_D82DD83D_00-00-04_Warning</title> </head> <body> GA_D82DD83D_00-00-05 VS GA_D82DD83D_00-00-04_Warning<br/> 已产生: 2025/10/20 11:20:35<br/>     <br/> 模式:  全部   <br/> 左边文件: E:\1_临时代码仓\GA_D37D_03-00-01\mainline\spa_traveo\src\IpcApplication\diagClient\canTp\canTp.c   <br/> 右边文件: E:\1_临时代码仓\GA_D37D_02-00-04\mainline\spa_traveo\src\IpcApplication\diagClient\canTp\canTp.c   <br/> <table class="fc" cellspacing="0" cellpadding="0"> <tr class="SectionBegin"> <td class="TextItemNum AlignRight">1</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> <td class="AlignCenter">=</td> <td class="TextItemNum AlignRight">1</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">2</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  Copyright DENSO Corporation                                                                                                      */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">2</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  Copyright DENSO Corporation                                                                                                      */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">3</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">3</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">4</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  Version  Date        Author   Change Description                                                                                 */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">4</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  Version  Date        Author   Change Description                                                                                 */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">5</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/* --------- ----------  -------  -------------------------------------------------------------------------------------------------- */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">5</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/* --------- ----------  -------  -------------------------------------------------------------------------------------------------- */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">6</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  1.0.0    3/6/2019    LW       New.                                                                                               */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">6</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  1.0.0    3/6/2019    LW       New.                                                                                               */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">7</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  2.0.0    9/26/2021   DC&nbsp;      add a new function which is sending  multi frame request                                           */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">7</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  2.0.0    9/26/2021   DC&nbsp;      add a new function which is sending  multi frame request                                           */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">8</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  2.0.1    10/8/2021   DC&nbsp;      Add PBD.can id and can index for D03B                                                              */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">8</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  2.0.1    10/8/2021   DC&nbsp;      Add PBD.can id and can index for D03B                                                              */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">9</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">9</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">10</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  * LW   = Luo Wei, KOTEI  create                                                                                                  */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">10</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  * LW   = Luo Wei, KOTEI  create                                                                                                  */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">11</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  * DC&nbsp;  = Ding Cong, KOTEI                                                                                                        */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">11</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  * DC&nbsp;  = Ding Cong, KOTEI                                                                                                        */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">12</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">12</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">13</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  CAN TP 15765-2 implement                                                                                                         */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">13</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  CAN TP 15765-2 implement                                                                                                         */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">14</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">14</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">15</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">15</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">16</td> <td class="TextItemSame"> </td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">16</td> <td class="TextItemSame"> </td> </tr>取得.c
10-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值