📄 graphic.part4.h
字号:
@函数名称: getlinesettings
函数原型: void far getlinesettings(struct linesettingstype far *lineinfo)
函数功能: 得到当前画线模式
函数返回:
参数说明: lineinfo-保存画线模式信息,该结构的定义如下:
struct linesettingstype{
int linestyle;
unsigned upattern;
int thickness;
};
其中linestyle的取值和含义如下:
SOLID_LINE=0 实线 DOTTED_LINE=1 点线
CENTER_LINE=2 中心线 DASHED_LINE=3 虚线
USERBIT_LINE=4 用户自定义模式
upattern-在linestyle=USERBIT_LINE时,表示自定义的线型,thickness-线宽,有2个取值:
NORM_WIDTH=1 1个象素宽度
THICK_WIDTH=3 3个象素宽度
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
char *lname[]={"SOLID_LINE","DOTTED_LINE","CENTER_LINE","DASHED_LINE","USERBIT_LINE"};
int main()
{
int gdriver=DETECT,gmode,errorcode;
struct linesettingstype lineinfo;
int midx,midy;
char lstyle[80],lpattern[80],lwidth[80];
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
getlinesettings(&lineinfo);
sprintf(lstyle,"%s is the line style.",lname[lineinfo.linestyle]);
sprintf(lpattern,"0x%X is the user-defined line pattern.",lineinfo.upattern);
sprintf(lwidth,"%d is the line thickness.",lineinfo.thickness);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(midx,midy,lstyle);
outtextxy(midx,midy+2*textheight("W"),lpattern);
outtextxy(midx,midy+4*textheight("W"),lwidth);
getch();
closegraph();
return 0;
}
@函数名称: getmoderange
函数原型: void far getmoderange(int graphdriver,int far *lomode,int far *himode)
函数功能: 得到某个图形驱动器driver的图形模式范围
函数返回:
参数说明: driver 图形驱动器编号,含义如下:
CGA 1 MCGA 2
EGA 3 EGA64 4
EGAMONO 5 IBM8514 6
HERCMONO 7 ATT400 8
VGA 9 PC3270 10
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
int low,high;
char mrange[80];
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
getmoderange(gdriver,&low,&high);
sprintf(mrange,"This driver supports modes %d..%d",low,high);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(midx,midy,mrange);
getch();
closegraph();
return 0;
}
@函数名称: getpixel
函数原型: unsigned far getpixel(int x,int y)
函数功能: 得到坐标为x,y的象素颜色
函数返回: 该象素颜色数值
参数说明: x,y-象素坐标
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define PIXEL_COUNT 1000
#define DELAY_TIME 100 /* in milliseconds */
int main(void)
{
int gdriver=DETECT,gmode,errorcode;
int i,x,y,color,maxx,maxy,maxcolor,seed;
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 */
}
maxx=getmaxx()+1;
maxy=getmaxy()+1;
maxcolor=getmaxcolor()+1;
while(!kbhit()){
seed=random(32767);
srand(seed);
for(i=0;i<PIXEL_COUNT;i++){
x=random(maxx);
y=random(maxy);
color=random(maxcolor);
putpixel(x,y,color);
}
delay(DELAY_TIME);
srand(seed);
for(i=0;i<PIXEL_COUNT;i++){
x=random(maxx);
y=random(maxy);
color=random(maxcolor);
if(color==getpixel(x,y))
putpixel(x,y,0);
}
}
getch();
closegraph();
return 0;
}
@函数名称: putpixel
函数原型: void far putpixel(int x, int y, int color)
函数功能: 在屏幕上画一个点
函数返回:
参数说明: x,y 画点位置坐标,color 画点颜色
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define PIXEL_COUNT 1000
#define DELAY_TIME 100
int main()
{
int gdriverDETECT,gmode,errorcode;
int i,x,y,color,maxx,maxy,maxcolor,seed;
initgraph(&gdriver,&gmode,"");
errorcodegraphresult();
if (errorcode != grOk){
printf("Graphics error: %s\n",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
maxxgetmaxx()+1;
maxygetmaxy()+1;
maxcolorgetmaxcolor()+1;
while (!kbhit())
{
seedrandom(32767);
srand(seed);
for (i=0;i<PIXEL_COUNT;i++){
xrandom(maxx);
yrandom(maxy);
colorrandom(maxcolor);
putpixel(x,y,color);
}
delay(DELAY_TIME);
srand(seed);
for (i=0;i<PIXEL_COUNT;i++){
xrandom(maxx);
yrandom(maxy);
colorrandom(maxcolor);
if (color==getpixel(x,y))
putpixel(x,y,0);
}
}
getch();
closegraph();
return 0;
}
@函数名称: graphdefaults
函数原型: void far graphdefaults(void)
函数功能: 将图形系统恢复为缺省状态
函数返回:
参数说明:
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int maxx,maxy;
initgraph(&gdriver,&gmode,"");
errorcode=graphresult();
if (errorcode!=grOk){
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
maxx=getmaxx();
maxy=getmaxy();
/* output line with nondefault settings */
setlinestyle(DOTTED_LINE,0,3);
line(0,0,maxx,maxy);
outtextxy(maxx/2,maxy/3,"Before default values are restored.");
getch();
/* restore default values for everything */
graphdefaults();
/* clear the screen */
cleardevice();
/* output line with default settings */
line(0,0,maxx,maxy);
outtextxy(maxx/2, maxy/3, "After restoring default values.");
getch();
closegraph();
return 0;
}
@函数名称: grapherrormsg
函数原型: char far *grapherrormsg(int errorcode)
函数功能: 得到图形系统的错误信息指针
函数返回: 错误描述的文字字符串指针
参数说明: errormode-错误信息编码,由graphresult()函数得到
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define NONSENSE -50
int main()
{
int gdriver=NONSENSE,gmode,errorcode;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
line(0,0,getmaxx(),getmaxy());
getch();
closegraph();
return 0;
}
@函数名称: _graphfreemem、_graphgetmem
函数原型: void far _graphfreemem(void far *ptr, unsigned size)
void far _graphgetmem(unsigned size)
函数功能: 释放图形系统所用的内存
得到图形系统所用的内存
实际上,它们调用的是malloc,free,因此它们常被重定义,以便图库管理模式能被程序员控制
函数返回:
参数说明: 此二函数由图形库内核调用
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
int main()
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy;
clrscr();
printf("Press any key to initialize graphics mode:");
getch();
clrscr();
initgraph(&gdriver,&gmode,"");
errorcode=graphresult();
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;
/* display a message */
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(midx,midy,"Press any key to exit graphics mode:");
getch();
closegraph();
return 0;
}
/* called by the graphics kernel to allocate memory */
void far * far _graphgetmem(unsigned size)
{
printf("_graphgetmem called to allocate %d bytes.\n",size);
printf("press any key:");
getch();
printf("\n");
/* allocate memory from far heap */
return farmalloc(size);
}
/* called by the graphics kernel to free memory */
void far _graphfreemem(void far *ptr, unsigned size)
{
printf("_graphfreemem called to free %d bytes.\n",size);
printf("press any key:");
getch();
printf("\n");
/* free ptr from far heap */
farfree(ptr);
}
@函数名称: imagesize
函数原型: unsigned far imagesize(int left, int top, int right, int bottom)
函数功能: 得到存储一块屏幕图象所需要的字节数
函数返回: 存储图象的字节数
参数说明: left,top,right,bottom-矩形图形屏幕的坐标
所属文件: <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define ARROW_SIZE 10
void draw_arrow(int x,int y);
int main()
{
int gdriver=DETECT,gmode,errorcode;
void *arrow;
int x,y,maxx;
unsigned int size;
initgraph(&gdriver,&gmode,);
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s",grapherrormsg(errorcode));
printf("Press any key to halt:");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -