俺老师留的一个题目:整个下拉菜单!
<P>#include<graphics.h><BR>#include<conio.h><BR>#include<stdio.h><BR>#include<bios.h><BR>#define L 0x4b00<BR>#define R 0x4d00<BR>#define U 0x4800<BR>#define D 0x5000<BR>#define ESC 0x11b<BR>int X=0,Y=0;<BR>unsigned KEY;<BR>char MENU[4][3][6]={{"str00\0","str01\0","str02\0"},{"str10\0","str11\0","str12\0"},{"str20\0","str21\0","str22\0"},{"str30\0","str31\0","str32\0"}};<BR> void main()<BR> {<BR> void draw(int a,int b,int sign);<BR> void crtview(void);<BR> void draw1(int a,int b,int sign);</P><P> int gdriver=DETECT,gmode;<BR> initgraph(&gdriver,&gmode,"d:\\tc2");<BR> setbkcolor(EGA_BLUE);<BR> setfillstyle(SOLID_FILL,EGA_WHITE);<BR> bar(0,0,639,19);<BR> bar(0,459,639,479);<BR> setcolor(EGA_WHITE);<BR> rectangle(4,24,634,454);<BR> setcolor(EGA_BLUE);<BR> outtextxy(249,464,"MADE BY ZY");<BR> getch();</P>
<P> for(X=0,Y=0;X<=3;X++)<BR> draw(X,Y,EGA_WHITE);</P>
<P> X=0;<BR> Y=0;<BR> draw(X,Y,EGA_RED);<BR> while(1)<BR> {<BR> KEY=bioskey(0);<BR> switch(KEY)<BR> {<BR> case L:<BR> if(X==0)break;<BR> else<BR> {<BR> if(Y!=0)<BR> {<BR> clearviewport();<BR> setcolor(EGA_WHITE);<BR> line(-5,4,634,4);<BR> Y=0;<BR> graphdefaults();<BR> setbkcolor(EGA_BLUE);<BR> }<BR> draw(X,Y,EGA_WHITE);<BR> X--;<BR> draw(X,Y,EGA_RED);<BR> }<BR> break;<BR> case R:<BR> if(X==3)break;<BR> else<BR> {<BR> if(Y!=0)<BR> {<BR> clearviewport();<BR> setcolor(EGA_WHITE);<BR> line(-5,4,634,4);<BR> Y=0;<BR> graphdefaults();<BR> setbkcolor(EGA_BLUE);<BR> }</P>
<P> draw(X,Y,EGA_WHITE);<BR> X++;<BR> draw(X,Y,EGA_RED);<BR> break;<BR> }<BR> case U:<BR> if(Y==0)break;<BR> else<BR> { if(Y==2)<BR> {<BR> draw1(X,Y,EGA_WHITE);<BR> Y--;<BR> draw1(X,Y,EGA_GREEN);<BR> break;<BR> }<BR> else<BR> break;<BR> }<BR> case D:<BR> if(Y==2)break;<BR> else<BR> {<BR> if(Y==0)<BR> {<BR> crtview();<BR> break;<BR> }<BR> else<BR> {<BR> draw1(X,Y,EGA_WHITE);<BR> Y++;<BR> draw1(X,Y,EGA_GREEN);<BR> break;<BR> }<BR> }<BR> case ESC:<BR> closegraph();<BR> return;<BR> default:<BR> break;<BR> }<BR> }<BR> }<BR> void draw(int a,int b,int sign)<BR> {<BR> setfillstyle(SOLID_FILL,sign);<BR> bar(49+a*100,0,49+(a+1)*100,19);<BR> if(sign==EGA_WHITE)<BR> sign=EGA_RED;<BR> else<BR> sign=EGA_WHITE;<BR> setcolor(sign);<BR> outtextxy(49+a*100,5,MENU[a][b]);<BR> }<BR> void crtview(void)<BR> {<BR> setviewport(49+X*100,20,149+X*100,20*3,1);<BR> setfillstyle(SOLID_FILL,EGA_WHITE);<BR> bar(0,0,99,39);<BR> for(Y=1;Y<=2;Y++)<BR> draw1(X,Y,EGA_WHITE);<BR> Y=1;<BR> draw1(X,Y,EGA_GREEN);<BR> }<BR> void draw1(int a,int b,int sign)<BR> {<BR> setfillstyle(SOLID_FILL,sign);<BR> bar(0,(Y-1)*20,100,Y*20);<BR> if(sign==EGA_WHITE)<BR> sign=EGA_RED;<BR> else<BR> sign=EGA_BLUE;<BR> setcolor(sign);<BR> outtextxy(0,5+(b-1)*20,MENU[a][b]);<BR> }</P> 没什么含量也没 做什么优化改动及添加注释!<BR>就是在网上找了半天没找到需要的;<BR>就自己弄了 一个<BR>希望给一些同学一点思路!<BR>请 多指教!~ <P>参考这个吧,你那个确实需要优化一下。。。。<BR><BR><BR><BR><BR>/*----------------From 《c语言高级实例解析》--------------------*/<BR>#include <conio.h><BR>#include<dos.h><BR>#include <graphics.h><BR>#include<stdio.h><BR>#include<stdlib.h></P>
<P>/*定义一些常数*/<BR>#define NO 0<BR>#define YES 1<BR>#define NONE -1<BR>#define MAX_MENU 7/*根据情况改变可以在菜单显示的最大项*/</P>
<P>/*全局参数*/<BR>int X,Y;<BR>int Selection;<BR>int button,x,y;<BR>void *p;<BR>size_t area;</P>
<P>/*菜单结构*/<BR>struct MENU<BR>{<BR> /*左上角*/<BR> int x; <BR> int y;<BR> /*右下角*/<BR> int x1;<BR> int y1;<BR> unsigned char Active[MAX_MENU];/* 菜单项是否激活的标志*/<BR> char *head;/* 菜单的名字(可选项)*/<BR>};</P>
<P>int MouseOn(struct MENU *t);<BR>void Process();<BR>void Xorbar(int sx,int sy,int ex,int ey,int c);<BR>void Show();<BR>void Hide();<BR>void Status();</P>
<P>/* 通过下面的参数用户可以改变按钮的位置。*/</P>
<P>struct MENU File={200,110,250,130};<BR>struct MENU Edit={295,110,345,130};<BR>struct MENU Help={390,110,440,130};<BR>struct MENU Close={600,10,620,30};<BR>struct MENU Neeraj={380,300,460,315};</P>
<P>void main()<BR>{<BR> int gd = DETECT,gm;<BR> <BR> /*初始化图形界面*/<BR> initgraph(&gd, &gm,"");<BR> Selection=NONE;<BR> /*是否激活菜单项,YES表示激活;NO表示取消激活*/<BR> for(gd=0;gd<MAX_MENU;gd++)<BR> {<BR> File.Active[gd]=YES;<BR> Edit.Active[gd]=YES;<BR> Help.Active[gd]=YES;<BR> Close.Active[gd]=YES;<BR> }<BR> /*菜单项的名字,用户可以修改以显示其他的名称*/<BR> File.head="[ File Menu ]";<BR> Edit.head="[ Edit Menu ]";<BR> Help.head="[ Help Menu ]";<BR> Close.head="[ Exit ]";<BR> /*为界面分配内存*/<BR> area=imagesize(0,0,150,150);<BR> p=malloc(area);<BR> <BR> /*没有足够的内存*/<BR> if(p==NULL)<BR> {<BR> closegraph();<BR> restorecrtmode();<BR> printf("Not Enough Memory !\n try to reduce the area used.\n");<BR> exit(1);<BR> }<BR> setfillstyle(1,7);<BR> bar(0,0,640,480);<BR> X=2;Y=2;<BR> /*绘制这个界面*/<BR> Process();<BR> End();<BR> /*关闭图形显示方式*/<BR> closegraph();<BR> printf("GUI-MENUS By Neeraj Sharma - <a href="mailto:n21@indiatimes.com\n" target="_blank" >n21@indiatimes.com\n</A>");<BR> getch();<BR>}</P>
<P>void Process()<BR>{<BR> /*文件菜单项的子菜单项*/<BR> char *fm[]={"New","Open","Save","Save as","Exit"};<BR> /*编辑菜单项的子菜单项*/<BR> char *em[]={"Undo","Cut","Copy","Paste","Delete","Select All"};<BR> /*帮助菜单项的子菜单项*/<BR> char *hm[]={"Help Topics","About Menus"};<BR> <BR> /* 如果用户在这里改变的了菜单项的值,那么也同样需要改变在函数<BR> 中的PullMenuDown(char *,int)值*/<BR> char KEY;<BR> /* 绘制各个菜单 */<BR> Win(140,100,500,140);<BR> /* 绘制文件菜单项 */<BR> DrawBtn(&File,"File");<BR> /* 绘制编辑菜单项 */<BR> DrawBtn(&Edit,"Edit");<BR> /* 绘制帮助菜单项*/<BR> DrawBtn(&Help,"Help");<BR> settextstyle(0,0,0);<BR> outtextxy(Close.x+7,Close.y+7,"x");<BR> Comments();<BR> Show();<BR> /* 正如下面所示用户可以激活和取消激活一个菜单项<BR> 下面的'save as'被取消激活*/<BR> File.Active[3]=NO;/* Save as*/<BR> Edit.Active[0]=NO;/* 取消激活*/<BR> while(1)<BR> {<BR> if(kbhit())<BR> {<BR> KEY=getch();<BR> /*如果用户按下"ESC"或者"-"推出程序*/<BR> if(KEY==27|| KEY==45) break;<BR> }<BR> Status();<BR> /*如果按下鼠标左键*/<BR> if(button==1)<BR> {<BR> /*用户点击Close*/<BR> if(MouseOn(&Close)) break;<BR> /*用户点击File*/<BR> if(MouseOn(&File))<BR> {<BR> PullMenuDown(fm,5,&File);/* 菜单项为5*/<BR> if(Selection==NONE) Msg("No Selection !");<BR> else Msg(fm[Selection]);<BR> if(Selection==4) break;<BR> }<BR> /*用户点击Edit*/<BR> if(MouseOn(&Edit))<BR> {<BR> PullMenuDown(em,6,&Edit);/* 菜单项为6*/<BR> if(Selection==NONE) Msg("No Selection !");<BR> else Msg(em[Selection]);<BR> }<BR> /*用户点击Help*/<BR> if(MouseOn(&Help))<BR> {<BR> PullMenuDown(hm,2,&Help);/* 菜单项为2*/<BR> if(Selection==NONE) Msg("No Selection !");<BR> else Msg(hm[Selection]);<BR> if(Selection==1) Msg("* Programming By NEERAJ SHARMA *");<BR> }<BR> }<BR> else<BR> {<BR> /*鼠标在那个菜单项上,就高亮显示之*/<BR> <BR> /*高亮显示文件菜单项*/<BR> if(MouseOn(&File)) Hlight(&File);<BR> /*高亮显示编辑菜单项*/<BR> if(MouseOn(&Edit)) Hlight(&Edit);<BR> /*高亮显示帮助菜单项*/<BR> if(MouseOn(&Help)) Hlight(&Help);<BR> /*高亮显示关闭菜单项*/<BR> if(MouseOn(&Close)) Hlight(&Close);<BR> if(MouseOn(&Neeraj))<BR> {<BR> Msg("Hello, everyone");<BR> while(MouseOn(&Neeraj))<BR> {<BR> Status();<BR> if(button!=0) break;<BR> if(kbhit()) break;<BR> }<BR> Msg("");<BR> }<BR> }<BR> }<BR> Msg(":: Bye Bye ::");<BR>}</P>
<P>/* 绘制菜单项 */<BR>DrawBtn(struct MENU *tm,char *txt)<BR>{<BR> /*设置绘制的颜色*/<BR> setcolor(0);<BR> /*设置文本格式*/<BR> settextstyle(2,0,4);<BR> /*输出txt文本*/<BR> outtextxy(tm->x+15,tm->y+4,txt);<BR>}<BR>/*高亮显示函数*/<BR>Hlight(struct MENU *t)<BR>{<BR>/* 高亮 */<BR> winp(t,0);<BR>/* 显示菜单项名称 */<BR> Msg(t->head);<BR> /*在没有输入的情况下,一直高亮*/<BR> while(MouseOn(t))<BR> {<BR> Status();<BR> if(button!=0) break;<BR> if(kbhit()) break;<BR> }<BR> /*消除高亮*/<BR> setcolor(7);<BR> Hide();<BR> rectangle(t->x,t->y,t->x1,t->y1);<BR> Msg("");<BR> /*重绘*/<BR> Show();<BR>}</P>
<P>int MouseOn(struct MENU *t)<BR>{<BR> int flag;<BR> flag=0;<BR> if(x>=t->x && x<=t->x1 && y>=t->y && y<=t->y1) flag=1;<BR> return flag;<BR>}</P>
<P>void Xorbar(int sx,int sy,int ex,int ey,int c)<BR>{<BR> int i;<BR> setwritemode(1);<BR> setcolor(c);<BR> Hide();<BR> for(i=sy;i<=ey;i++)<BR> line(sx,i,ex,i);<BR> Show();<BR> setwritemode(0);<BR>}</P>
<P>Push(struct MENU *tm)<BR>{<BR> winp(tm,1);<BR> while(button==1)<BR> {<BR> Status();<BR> if(kbhit()) break;<BR> }<BR>}</P>
<P>Up(struct MENU *tm)<BR>{<BR> setcolor(7);<BR> Hide();<BR> rectangle(tm->x,tm->y,tm->x1,tm->y1);<BR> Show();<BR> while(button==1)<BR> {<BR> Status();<BR> if(kbhit()) break;<BR> }<BR>}</P>
<P>/*弹出下拉菜单*/<BR>PullMenuDown(char *H[],int n,struct MENU *tm)<BR>{<BR> int i;<BR> i=n*20;<BR> /* 保存背景*/<BR> Push(tm);<BR> /* 隐藏鼠标光标*/<BR> Hide();<BR> /* 把菜单项位图读入内存,以供收回弹出项时重新绘制*/<BR> getimage(tm->x+1,tm->y1+2,tm->x+100,tm->y1+i+5,p);<BR> /* 重新设置这块区域*/<BR> Win(tm->x+1,tm->y1+2,tm->x+100,tm->y1+i+5);<BR> setcolor(0);<BR> settextstyle(2,0,4);<BR> /*弹出下拉菜单项*/<BR> for(i=0;i<n;i++)<BR> {<BR> if(!tm->Active[i])<BR> {<BR> setcolor(15);<BR> outtextxy(tm->x+15,tm->y1+9+i*20,H[i]);<BR> setcolor(0);<BR> outtextxy(tm->x+14,tm->y1+8+i*20,H[i]);<BR> }<BR> else outtextxy(tm->x+14,tm->y1+8+i*20,H[i]);<BR> }<BR> /*显示鼠标光标*/<BR> Show();<BR> while(button==1) Status();<BR> HandleMenu(n,tm);<BR>}</P>
<P>PullMenuUp(int n,struct MENU *tm)<BR>{<BR> int i;<BR> /*隐藏鼠标光标*/<BR> Hide();<BR> /* 把读入内容的原菜单项位图重新绘制出来*/<BR> putimage(tm->x+1,tm->y1+2,p,COPY_PUT);<BR> /*显示鼠标光标*/<BR> Show();<BR> Up(tm);<BR>}<BR>/* 处理菜单项命令函数 */<BR>HandleMenu(int n,struct MENU *tm)<BR>{<BR> int i;<BR> char Key;<BR> setcolor(0);<BR> Selection=NONE;<BR> i=0;<BR> while(1)<BR> {<BR> Status();<BR> if(button==1) break;<BR> for(i=0;i<n;i++)<BR> {<BR> Status();<BR> if(MouseOn1(tm->x+4,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20))<BR> {<BR> Xorbar(tm->x+3,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20,14);<BR> while(MouseOn1(tm->x+4,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20))<BR> {<BR> Status();<BR> if(button==1)<BR> {<BR> if(tm->Active[i]==YES)<BR> {<BR> Selection=i;<BR> break;<BR> }<BR> else<BR> Selection=NONE;<BR> }<BR> if(kbhit()) break;<BR> }<BR> Xorbar(tm->x+3,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20,14);<BR> }<BR> }<BR> }<BR> PullMenuUp(n,tm);<BR>}</P>
<P>MouseOn1(int cx,int cy,int cx1,int cy1)<BR>{<BR> int flag;<BR> flag=0;<BR> if(x>=cx && x<=cx1 && y>=cy && y<=cy1) flag=1;<BR> return flag;<BR>}</P>
<P>/* Mouse & win routines */</P>
<P>union REGS i,o;<BR>struct SREGS ss;</P>
<P>/* 显示鼠标光标函数 */<BR>void Show()<BR>{<BR> i.x.ax=1;<BR> int86(0x33,&i,&o);<BR>}</P>
<P>/* 隐藏鼠标光标函数*/<BR>void Hide()<BR>{<BR> i.x.ax=2;<BR> int86(0x33,&i,&o);<BR>}</P>
<P>/* 获得鼠标状态函数 */<BR>void Status()<BR>{<BR> i.x.ax=3;<BR> int86(0x33,&i,&o);<BR> button=o.x.bx;<BR> x=o.x.cx;<BR> y=o.x.dx;<BR>}<BR>/*绘制菜单项的背景和边框*/<BR>winp(struct MENU *t,int state)<BR>{<BR> Hide();<BR> /*非高亮模式*/<BR> if(state==1)<BR> {<BR> /*两条暗边框线*/<BR> setcolor(0);<BR> line(t->x,t->y,t->x,t->y1);<BR> line(t->x,t->y,t->x1,t->y);<BR> /*两条亮边框线*/<BR> setcolor(15);<BR> line(t->x1,t->y,t->x1,t->y1);<BR> line(t->x,t->y1,t->x1,t->y1);<BR> }<BR> /*高亮模式*/<BR> else<BR> {<BR> /*两条亮边框线*/<BR> setcolor(15);<BR> line(t->x,t->y,t->x,t->y1);<BR> line(t->x,t->y,t->x1,t->y);<BR> /*两条暗边框线*/<BR> setcolor(0);<BR> line(t->x1,t->y,t->x1,t->y1);<BR> line(t->x,t->y1,t->x1,t->y1);<BR> }<BR> Show();<BR>}<BR>/*绘制一个矩形区域*/<BR>Win(int sx,int sy,int ex,int ey)<BR>{<BR> Hide();<BR> /*设置填充的颜色*/<BR> setfillstyle(1,7);<BR> /*绘制一个区域*/<BR> bar(sx,sy,ex,ey);<BR> /*两条亮边框线*/<BR> setcolor(15);<BR> line(sx,sy,sx,ey);<BR> line(sx,sy,ex,sy);<BR> /*两条暗边框线*/<BR> setcolor(0);<BR> line(ex,sy,ex,ey);<BR> line(sx,ey,ex,ey);<BR> Show();<BR>}</P>
<P>Msg(char *m)<BR>{<BR> int l;<BR> setcolor(14);<BR> rectangle(18,438,622,477);<BR> setfillstyle(1,0);<BR> bar(20,440,620,475);<BR> if(MouseOn(&Neeraj))<BR> {<BR> settextstyle(2,0,7);<BR> setcolor(11);<BR> l=textwidth(m)/2;<BR> outtextxy(320-l,440,m);<BR> }<BR> else<BR> {<BR> settextstyle(2,0,8);<BR> setcolor(15);<BR> l=textwidth(m)/2;<BR> outtextxy(320-l,440,m);<BR> outtextxy(321-l,441,m);<BR> }<BR>}</P>
<P>Comments()<BR>{<BR> setfillstyle(1,0);<BR> bar(140,5,500,40);<BR> setcolor(14);<BR> rectangle(140,5,500,40);<BR> settextstyle(2,0,8);<BR> setcolor(15);<BR> outtextxy(260,10,"GUI-MENUS");<BR> setfillstyle(1,8);<BR> bar(140,150,502,402);<BR> setcolor(14);<BR> rectangle(138,148,502,402);<BR> Msg("Welcome !");<BR> settextstyle(2,0,5);<BR> setcolor(10);<BR> outtextxy(158,180,"Hello Friends !");<BR> settextstyle(2,0,4);<BR> outtextxy(260,200,"Here i am with my menu program.");<BR> outtextxy(158,220,"As you can see it's like windows menu,you can");<BR> outtextxy(158,240,"add more menus to it. It's just a concept for");<BR> outtextxy(158,260,"building more menus. I hope you will like my effort,");<BR> outtextxy(158,280,"email me at : <a href="mailto:n21@indiatimes.com" target="_blank" >n21@indiatimes.com</A>");<BR> setcolor(14);<BR> outtextxy(370,300,"- Neeraj Sharma");<BR> line(380,304+textheight("A"),380+textwidth("Neeraj Sharma"),304+textheight("A"));<BR>}</P>
<P>End()<BR>{<BR> int i,j;<BR> setcolor(0);<BR> Hide();<BR> for(j=0;j<40;j++)<BR> {<BR> for(i=0;i<481;i+=20)<BR> line(0,0+i+j,640,j+0+i);<BR> delay(100);<BR> if(kbhit()) if(getch()==27) break;<BR> }<BR> settextstyle(2,0,5);<BR> setcolor(15);<BR> outtextxy(220,180,"Thanks for being with us !");<BR> Show();<BR> delay(3000);<BR>}</P>
<P><BR> </P> [em09]恐怖 不会
页:
[1]
