China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 21:02
中国DOS联盟论坛 » DOS开发编程 & 发展交流 (开发室) » Repost: Making a graphical interface "program" in C View 1,570 Replies 0
Original Poster Posted 2003-04-01 00:00 ·  中国 广东 广州 增城区 电信
元老会员
★★★
Credits 1,468
Posts 407
Joined 2002-10-21 00:00
23-year member
UID 34
Gender Male
From 广州
Status Offline
Article summary:
  This small program uses the graphics functions in C to build a 640*480 graphical interface, including a static menu. The program can play a piece of music (of course, the unbearable kind produced by the PC speaker), drive the mouse, draw circles of a specified size (with slight modification it can draw many kinds of graphics), execute MS-DOS commands, and also has a short bit of animation (let's call it animation for now; it's really too insignificant), plus help information explaining how to operate it. Though small, it uses quite a few of the functions C gives us.


--------------------------------------------------------------------------------

Main text:

Making a graphical interface "program" in C


  Today I read brother Qinling's article "Drop-down text menu made in C language", and suddenly remembered a course design project I did last semester. I also wanted to post it for everyone to comment on.
  This small program uses the graphics functions in C to build a 640*480 graphical interface, including a static menu. The program can play a piece of music (of course, the unbearable kind produced by the PC speaker), drive the mouse, draw circles of a specified size (with slight modification it can draw many kinds of graphics), execute MS-DOS commands, and also has a short bit of animation (let's call it animation for now; it's really too insignificant), plus help information explaining how to operate it. Though small, it uses quite a few of the functions C gives us.
  I hurriedly finished this at the beginning of my sophomore year (to deal with an assignment), and there are many improper parts in it. For example, I used the much-disliked goto statement, there is no clear structure, and so on. I hope everyone will point out the problems.
  The quotation marks in the title are because it really doesn't count as much of a program and can't solve any practical problem. It was only after reading brother Qinling's article that I dared to bring it out. Below is the source code section, kept in its original form. It also includes some of the curious comments I wrote at the time. I also hope it can give beginners (I myself am also a beginner) a few hints.

/**************************launch*******************************//
#include
#include
#include
#include
#include
#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(a1500)
delay(a);
if(a==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=85&&y0){
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*****************************//
欢迎大家观临我的个人主页:

http://dosdiy.bluepc.com.cn/
http://dosdiy.ys168.com/


EMAIL: lydong@china.com.cn lydong@yeah.net
----------------------------------------------
Forum Jump: