HITSY 版 (精华区)
发信人: shs (Snoopy.鬼迷心窍), 信区: HITSY
标 题: 用c语言做一个图形界面"程序"(zz for freshman)
发信站: 哈工大紫丁香 (2003年03月06日08:54:47 星期四), 站内信件
用c语言做一个图形界面"程序"
作者会员名:bille
今天看了秦岭兄的"C语言制作的下拉文本菜单"一文,突然想起我在上学期做的一次课
程设计,也想发表出来,供大家品论。
这个小程序引用了c语言中的图形函数,构造了一个640*480的图形界面,其中包括了
一个静态的菜单。程序中可以有一段音乐(当然是pc喇叭放出的难以忍受的音乐),可以
驱动鼠标,画指定大小的圆(稍作修改一下就可画出多种图形了),执行ms-dos命令,还
有一小段动画(崭且称之为动画吧,实在太不起眼了),有帮助信息提示如何操作,虽小
,但应用了c给我们的较多功能。
这是我在大二开学初匆忙完成的(为了应付作业),其中有很多不当之处,比如,用
了不被人看好的goto语句,没有层次等,希望大家予以指正。
标题中的引号,是因为他根本算不上什么程序,不能解决什么问题。因为看了秦岭兄
的文章,才斗胆将它拿出来。以下时源代码部分,保持原文,其中还有我当时的一些充满
好奇的注释,也希望能给初学者(我自己也是一个初学者)一点提示。
/**************************launch*******************************//
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#define TRUE 1
#define FALSE 0
#define KB_S_N_DOWN 80
#define KB_S_N_UP 72
#define KB_S_N_ENTER 28
#define KB_S_N_F1 59
#define KB_S_N_A 29
#define KB_S_N_D 32
#define KB_S_N_C 31
#define KB_S_N_M 50
#define KB_S_N_Q 16
#define KB_S_N_S 33
#define PopWindow 8
#define WindowMinX 0
#define WindowMinY 0
#define WindowMaxX 639
#define WindowMaxY 479
union REGS regs;
int init();
int read();
void cursor(),newxy();
void MessageBox(char *Message);
/*Define function*/
int get_key();
int get_item(int,int);
void *buf_curse;
main()
{
int driver,mode;
int row,item_num,done,k;
int j,gdriver,gmode,size;
void *buf;
int i;
int a[]={657,8000,1,2000,657,10000,698,10000,784,8000,1,2000,
784,10000,698,10000,657,10000,587,10000,523,8000,1,2000,
523,10000,587,10000,657,8000,1,2000,657,15000,587,3000,
1,2000,587,20000,657,8000,1,2000,657,10000,698,10000,
784,8000,1,2000,784,10000,698,10000,657,10000,587,10000,
523,8000,1,2000,523,10000,587,10000,657,10000,587,150000,
523,3000,1,2000,523,20000,
587,8000,1,2000,587,10000,657,10000,523,10000,587,10000,
657,5000,698,5000,657,10000,523,10000,587,10000,657,5000,
698,5000,657,10000,587,10000,523,10000,587,10000,392,20000,
657,8000,1,2000,657,10000,698,10000,
784,8000,1,2000,784,10000,698,10000,657,10000,587,10000,
523,8000,1,2000,523,10000,587,10000,657,10000,587,15000,
523,3000,1,2000,523,20000,1,10000};
gdriver=DETECT;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"");
setbkcolor(BLUE);
cleardevice();
setcolor(YELLOW);
setlinestyle(0,0,1);
setfillstyle(1,5);
settextstyle(1,0,9);
circle(100,200,30);
outtextxy(100,215,"Welcome!");
floodfill(100,215,YELLOW);
settextstyle(2,0,5);
setcolor(WHITE);
outtextxy(110,310,"press any key to continue...");
/*floodfill(110,310,BLACK);*/
settextstyle(3,0,2);
outtextxy(180,450,"?2000 Awaresoft.All Rights Reserved.");
size=imagesize(69,169,131,231);
buf=malloc(size);
getimage(69,169,131,231,buf);
putimage(500,169,buf,COPY_PUT);
do
{
for(j=0;j<185;j++)
{
putimage(70+j,170,buf,COPY_PUT);
putimage(500-j,170,buf,COPY_PUT);
}
for(j=0;j<185;j++)
{
putimage(255-j,170,buf,COPY_PUT);
putimage(315+j,170,buf,COPY_PUT);
}
}while(!kbhit());
getch();
MENU:
driver=DETECT;
mode=0;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
init(WindowMinY,WindowMaxX,WindowMinX,458); /*init is a defined function*/
size=imagesize(0,0,90,22); /*count the bytes needs for the image*/
if(size!=1) /*if it isn't only one byte*/
buf_curse=malloc(size); /*apply the ram space*/
/*get ligh bar */
setfillstyle(1,13); /*1:solid_fill;13:color*/
bar(0,0,90,22); /*draw a rectangle without the bound*/
getimage(0,0,90,22,buf_curse); /*copy image to buf_curse*/
setfillstyle(1,BLACK); /*the same as line66*/
bar(0,0,91,23); /*left,top,right,bottom*/
/*display menu */
setcolor(YELLOW); /*set the color yellow*/
rectangle(0,0,639,479);
rectangle(0,457,639,479); /*l t r b*/
setfillstyle(1,RED); /*fill with color lightgray*/
bar(1,458,638,478); /*rectangle*/
setcolor(BLUE);
outtextxy(10,467,"F1-Help Alt_D-Draw Alt_S-SOUND Alt_M-MS-DOS Alt_Q-Quit");
setfillstyle(1,BLUE); /*print the string at 10,467 and color it blue*/
rectangle(544,1,638,150); /*is it necessary?*/
bar(544,1,638,150);
item_num=6; /*6 item and it's a useful point*/
setcolor(YELLOW);
for(k=0;k<item_num;k++){
rectangle(544,k*25,638,25+k*25);
floodfill(544,k*25,YELLOW);
} /*draw four rectangles in a column*/
setcolor(WHITE);
outtextxy(548,10,"Draw");
outtextxy(548,35,"Clean");
outtextxy(548,60,"Ms-DOS");
outtextxy(548,85,"Quit"); /*fill the four rectangles with words*/
outtextxy(548,110,"Sound");
outtextxy(548,135,"About...");
/*added hot key*/
setcolor(LIGHTRED);
outtextxy(548,10,"D");
outtextxy(548,35,"C"); /*cover the 'D' with another 'D'with a special color*/
outtextxy(548,60,"M");
outtextxy(548,85,"Q");
outtextxy(548,110,"S");
outtextxy(548,135,"A");
/*display light bar on first menu item*/
putimage(545,1,buf_curse,XOR_PUT); /*output the 'getimage'at
545,1;buf_curse:size;XOR_PUT:can be recognized by putimage()*/
setcolor(WHITE); /*set color*/
row=0;
done=FALSE;
do{
row=get_item(row,item_num); /*get_item is a function defined by me*/
switch(row){
case 0:
/*do function*/
setcolor(LIGHTRED);
bar(1,458,638,478);
Mouse(); /*defined*/
goto MENU; /*?*/
case 1:
putimage(545,1+row*25,buf_curse,XOR_PUT);
row=0;
putimage(545,1+row*25,buf_curse,XOR_PUT);
break;
case 2:
/*enter MS-DOS*/
free(buf_curse); /*free the rom space*/
done=TRUE; /*make done equal to TRUE???*/
restorecrtmode(); /*a function in graphic.h*/
system(""); /*a lib function*/
goto MENU;
case 3:
/*quit*/
setcolor(WHITE);
MessageBox("Made by XuJiwei.");/*defined function*/
done=TRUE;
closegraph(); /*make it out of the graphics condition,may be used later.*/
exit(0); /*end properly*/
case 4:
/*play the music*/
for(i=0;i<154;i++)
{if(a[i]<1500&&a[i]!=1)
sound(a[i]);
if(a[i]>1500)
delay(a[i]);
if(a[i]==1)
nosound();
}
goto MENU;
/*about*/
case 5:
outtextxy(100,100,"It is a free software,made by turboc2.0,");
outtextxy(100,120,"there must be some bugs,I hope to commun");
outtextxy(100,140,"-icate with you and kill them,and hope t ");
outtextxy(100,160,"-o be your friend,for more information,p");
outtextxy(100,180,"-lease write to #2140 HIT(zip code150001");
outtextxy(100,200,"),or bill_e@sina.com");
outtextxy(100,220," by XuJiwei,09-23-00.");
getch();
goto MENU;
}
}while(!done); /*!done means FAUSE*/
restorecrtmode();
} /*by now,the program comes to an end*/
/*creat several functions*/
int get_item(int row,int item_num)
{
int key,done;
done=FALSE;
do{
key=get_key();
switch(key){
case KB_S_N_DOWN:
putimage(545,1+row*25,buf_curse,XOR_PUT);
if(row==item_num-1)
row=0;
else row+=1;
putimage(545,1+row*25,buf_curse,XOR_PUT);
break;
case KB_S_N_UP:
putimage(545,1+row*25,buf_curse,XOR_PUT);
if(row==0)
row=item_num-1;
else row-=1;
putimage(545,1+row*25,buf_curse,XOR_PUT);
break;
case KB_S_N_ENTER:
done=TRUE; /*then......*/
break;
case KB_S_N_F1:
switch(row){
case 0:
MessageBox("Drawing with the mouse.");
break;
case 1:
MessageBox("Make all empty.");
break;
case 2:
MessageBox("MS-DOS and press exit return.");
break;
case 3:
MessageBox("Quit and return to MS-DOS.");
break;
case 4:
MessageBox("Do you here that?");
break;
case 5: MessageBox("C program made by XuJiwei in 23rd,Sep,2000.");
break;
}
break;
case KB_S_N_D:
if(row!=0)
{
putimage(545,1+row*25,buf_curse,XOR_PUT);
row=0;
putimage(545,1+row*25,buf_curse,XOR_PUT);
}
done=TRUE;
break;
case KB_S_N_A:
if(row!=5)
{
putimage(545,1+row*25,buf_curse,XOR_PUT);
row=5;
putimage(545,1+row*25,buf_curse,XOR_PUT);
}
done=TRUE;
getch();
break;
case KB_S_N_S:
if(row!=4)
{
putimage(545,1+row*25,buf_curse,XOR_PUT);
row=4;
putimage(545,1+row*25,buf_curse,XOR_PUT);
}
done=TRUE;
break;
case KB_S_N_C: /*there appears a bug*/
if(row!=1)
{
putimage(545,1+row*25,buf_curse,XOR_PUT);
row=1;
putimage(545,1+row*25,buf_curse,XOR_PUT);
}
done=TRUE;
break;
case KB_S_N_M:
if(row!=2)
{
putimage(545,1+row*25,buf_curse,XOR_PUT);
row=2;
putimage(545,1+row*25,buf_curse,XOR_PUT);
}
done=TRUE;
break;
case KB_S_N_Q:
if(row!=3)
{
putimage(545,1+row*25,buf_curse,XOR_PUT);
row=3;
putimage(545,1+row*25,buf_curse,XOR_PUT);
}
done=TRUE;
break;
default:
break;
}
}while(!done);
return row; /*useful*/
}
int get_key() /*receive the keyboard massage*/
{
union REGS rg;
rg.h.ah=0;
int86(0x16,&rg,&rg); /*?*/
return rg.h.ah;
}
Mouse()
{
int buttons,x,y;
setcolor(YELLOW);
outtextxy(5,15,"Move mouse using any button,click Quit to quit.");
setcolor(WHITE);
setwritemode(XOR_PUT);
if(init(WindowMinY,WindowMaxX,WindowMinX,458)==0)
{ /*init() :a function defined*/
printf("Mouse or mouse driver absent,please install it!");
delay(5000);
exit(1);
}
x=320;
y=240;
cursor(x,y);
for(;;){
newxy(&x,&y,&buttons);
if(x>=548&&x<=638&&y>=85&&y<=100&&buttons){
break;
}
}
}
void cursor(int x,int y)
{
line(x,y,x,y+4);
line(x,y,x+4,y);
line(x,y,x+5,y+5);
}
int init(int xmin,int xmax,int ymin,int ymax)
{
int retcode;
regs.x.ax=0;
int86(51,®s,®s);
retcode=regs.x.ax;
if(retcode==0)
return 0;
regs.x.ax=7;
regs.x.cx=xmin;
regs.x.dx=xmax;
int86(51,®s,®s);
regs.x.ax=8;
regs.x.cx=ymin;
regs.x.dx=ymax;
int86(51,®s,®s);
return retcode;
}
int read(int *mx,int *my,int *mbutt)
{
int xx0=*mx,yy0=*my,buto=0;
int xnew,ynew;
do{
regs.x.ax=3;
int86(51,®s,®s);
xnew=regs.x.cx;
ynew=regs.x.dx;
*mbutt=regs.x.bx;
}while(xnew==xx0&&ynew==yy0&&*mbutt==buto);
*mx=xnew;
*my=ynew;
if(*mbutt){
*mx=xnew;
*my=ynew;
return -1;
}
else{
*mx=xnew;
*my=ynew;
return 1;
}
}
void newxy(int *mx,int *my,int *mbutt)
{
int ch,xx0=*mx,yy0=*my,x,y;
int xm,ym;
ch=read(&xm,&ym,mbutt);
if(ch>0){
cursor(xx0,yy0);
cursor(xm,ym);
}
else{
cursor(xx0,yy0);
cursor(xm,ym);
circle(xm,ym,2);
}
*mx=xm;
*my=ym;
}
void MessageBox(char *Message)
{
int MidX,MidY,x1,y1,x2,y2,size,Len;
void *Buffer;
MidX=(WindowMaxX-WindowMinX)/2;
MidY=(WindowMaxY-WindowMinY)/2;
Len=8*(strlen(Message)+1)/2;
x1=MidX-Len-5*8;
x2=MidX+Len+5*8;
y1=MidY-50;
y2=MidY+50;
size=imagesize(x1,y1,x2,y2);
if((Buffer=malloc(size))==NULL){
outtextxy(100,300,"Out of memorry!Press any key to continue.");
getch();
}
else{
getimage(x1,y1,x2,y2,Buffer);
setfillstyle(SOLID_FILL,BLUE);
bar(x1,y1,x2-13,y2-13);
setfillstyle(SOLID_FILL,BLACK);
bar(x1+12,y2-12,x2,y2);
bar(x2-12,y1+12,x2,y2);
setcolor(GREEN);
settextjustify(CENTER_TEXT,CENTER_TEXT);
settextstyle(1,0,1.5);
outtextxy(MidX,MidY-20,Message);
setcolor(YELLOW);
outtextxy(MidX,MidY+2,"ALL RIGHTS RESERVED.");
setcolor(RED);
outtextxy(MidX,MidY+26,"2000-09-23");
getch();
putimage(x1,y1,Buffer,COPY_PUT);
free(Buffer);
settextjustify(LEFT_TEXT,TOP_TEXT);
}
}
/******************************end*****************************//
作者会员名:bille
--
I want to fly Because I want to be free!
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.226.240]
※ 修改:·shs 於 03月06日09:24:49 修改本文·[FROM: 202.118.226.240]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:210.191毫秒