回覆列表
  • 1 # 大國師魂系列

    #include <conio.h>#include<dos.h>#include <graphics.h>#include<stdio.h>#include<stdlib.h>

    /*定義一些常數*/#define NO 0#define YES 1#define NONE -1#define MAX_MENU 7/*根據情況改變可以在選單顯示的最大項*/

    /*全域性引數*/int X,Y;int Selection;int button,x,y;void *p;size_t area;

    /*選單結構*/struct MENU{ /*左上角*/ int x; int y; /*右下角*/ int x1; int y1; unsigned char Active[MAX_MENU];/* 選單項是否啟用的標誌*/ char *head;/* 選單的名字(可選項)*/};

    int MouseOn(struct MENU *t);void Process();void Xorbar(int sx,int sy,int ex,int ey,int c);void Show();void Hide();void Status();

    struct MENU File={200,110,250,130};struct MENU Edit={295,110,345,130};struct MENU Help={390,110,440,130};struct MENU Close={600,10,620,30};struct MENU Neeraj={380,300,460,315};

    void main(){ int gd = DETECT,gm; /*初始化圖形介面*/ initgraph(&gd, &gm,""); Selection=NONE; /*是否啟用選單項,YES表示啟用;NO表示取消啟用*/ for(gd=0;gd<MAX_MENU;gd++) { File.Active[gd]=YES; Edit.Active[gd]=YES; Help.Active[gd]=YES; Close.Active[gd]=YES; } /*選單項的名字,使用者可以修改以顯示其他的名稱*/ File.head="[ File Menu ]"; Edit.head="[ Edit Menu ]"; Help.head="[ Help Menu ]"; Close.head="[ Exit ]"; /*為介面分配記憶體*/ area=p_w_picpathsize(0,0,150,150); p=malloc(area); /*沒有足夠的記憶體*/ if(p==NULL) { closegraph(); restorecrtmode(); printf("Not Enough Memory !\n try to reduce the area used.\n"); exit(1); } setfillstyle(1,7); bar(0,0,640,480); X=2;Y=2; /*繪製這個介面*/ Process(); End(); /*關閉圖形顯示方式*/ closegraph(); printf("GUI-MENUS By Neeraj Sharma - [email][email protected][/email]\n"); getch();}

    void Process(){ /*檔案選單項的子選單項*/ char *fm[]={"New","Open","Save","Save as","Exit"}; /*編輯選單項的子選單項*/ char *em[]={"Undo","Cut","Copy","Paste","Delete","Select All"}; /*幫助選單項的子選單項*/ char *hm[]={"Help Topics","About Menus"}; /* 如果使用者在這裡改變的了選單項的值,那麼也同樣需要改變在函式 中的PullMenuDown(char *,int)值*/ char KEY; /* 繪製各個選單 */ Win(140,100,500,140); /* 繪製檔案選單項 */ DrawBtn(&File,"File"); /* 繪製編輯選單項 */ DrawBtn(&Edit,"Edit"); /* 繪製幫助選單項*/ DrawBtn(&Help,"Help"); settextstyle(0,0,0); outtextxy(Close.x+7,Close.y+7,"x"); Comments(); Show(); /* 正如下面所示使用者可以啟用和取消啟用一個選單項 下面的"save as"被取消啟用*/ File.Active[3]=NO;/* Save as*/ Edit.Active[0]=NO;/* 取消啟用*/ while(1) { if(kbhit()) { KEY=getch(); /*如果使用者按下"ESC"或者"-"推出程式*/ if(KEY==27|| KEY==45) break; } Status(); /*如果按下滑鼠左鍵*/ if(button==1) { /*使用者點選Close*/ if(MouseOn(&Close)) break; /*使用者點選File*/ if(MouseOn(&File)) { PullMenuDown(fm,5,&File);/* 選單項為5*/ if(Selection==NONE) Msg("No Selection !"); else Msg(fm[Selection]); if(Selection==4) break; } /*使用者點選Edit*/ if(MouseOn(&Edit)) { PullMenuDown(em,6,&Edit);/* 選單項為6*/ if(Selection==NONE) Msg("No Selection !"); else Msg(em[Selection]); } /*使用者點選Help*/ if(MouseOn(&Help)) { PullMenuDown(hm,2,&Help);/* 選單項為2*/ if(Selection==NONE) Msg("No Selection !"); else Msg(hm[Selection]); if(Selection==1) Msg("* Programming By NEERAJ SHARMA *"); } } else { /*滑鼠在那個選單項上,就高亮顯示之*/ /*高亮顯示檔案選單項*/ if(MouseOn(&File)) Hlight(&File); /*高亮顯示編輯選單項*/ if(MouseOn(&Edit)) Hlight(&Edit); /*高亮顯示幫助選單項*/ if(MouseOn(&Help)) Hlight(&Help); /*高亮顯示關閉選單項*/ if(MouseOn(&Close)) Hlight(&Close); if(MouseOn(&Neeraj)) { Msg("Hello, everyone"); while(MouseOn(&Neeraj)) { Status(); if(button!=0) break; if(kbhit()) break; } Msg(""); } } } Msg(":: Bye Bye ::");}

    /* 繪製選單項 */DrawBtn(struct MENU *tm,char *txt){ /*設定繪製的顏色*/ setcolor(0); /*設定文字格式*/ settextstyle(2,0,4); /*輸出txt文字*/ outtextxy(tm->x+15,tm->y+4,txt);}/*高亮顯示函式*/Hlight(struct MENU *t){/* 高亮 */ winp(t,0);/* 顯示選單項名稱 */ Msg(t->head); /*在沒有輸入的情況下,一直高亮*/ while(MouseOn(t)) { Status(); if(button!=0) break; if(kbhit()) break; } /*消除高亮*/ setcolor(7); Hide(); rectangle(t->x,t->y,t->x1,t->y1); Msg(""); /*重繪*/ Show();}

    int MouseOn(struct MENU *t){ int flag; flag=0; if(x>=t->x && x<=t->x1 && y>=t->y && y<=t->y1) flag=1; return flag;}

    void Xorbar(int sx,int sy,int ex,int ey,int c){ int i; setwritemode(1); setcolor(c); Hide(); for(i=sy;i<=ey;i++) line(sx,i,ex,i); Show(); setwritemode(0);}

    Push(struct MENU *tm){ winp(tm,1); while(button==1) { Status(); if(kbhit()) break; }}

    Up(struct MENU *tm){ setcolor(7); Hide(); rectangle(tm->x,tm->y,tm->x1,tm->y1); Show(); while(button==1) { Status(); if(kbhit()) break; }}

    /*彈出下拉選單*/PullMenuDown(char *H[],int n,struct MENU *tm){ int i; i=n*20; /* 儲存背景*/ Push(tm); /* 隱藏滑鼠游標*/ Hide(); /* 把選單項點陣圖讀入記憶體,以供收回彈出項時重新繪製*/ getp_w_picpath(tm->x+1,tm->y1+2,tm->x+100,tm->y1+i+5,p); /* 重新設定這塊區域*/ Win(tm->x+1,tm->y1+2,tm->x+100,tm->y1+i+5); setcolor(0); settextstyle(2,0,4); /*彈出下拉選單項*/ for(i=0;i<n;i++) { if(!tm->Active[i]) { setcolor(15); outtextxy(tm->x+15,tm->y1+9+i*20,H[i]); setcolor(0); outtextxy(tm->x+14,tm->y1+8+i*20,H[i]); } else outtextxy(tm->x+14,tm->y1+8+i*20,H[i]); } /*顯示滑鼠游標*/ Show(); while(button==1) Status(); HandleMenu(n,tm);}

    PullMenuUp(int n,struct MENU *tm){ int i; /*隱藏滑鼠游標*/ Hide(); /* 把讀入內容的原選單項點陣圖重新繪製出來*/ putp_w_picpath(tm->x+1,tm->y1+2,p,COPY_PUT); /*顯示滑鼠游標*/ Show(); Up(tm);}/* 處理選單項命令函式 */HandleMenu(int n,struct MENU *tm){ int i; char Key; setcolor(0); Selection=NONE; i=0; while(1) { Status(); if(button==1) break; for(i=0;i<n;i++) { Status(); if(MouseOn1(tm->x+4,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20)) { Xorbar(tm->x+3,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20,14); while(MouseOn1(tm->x+4,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20)) { Status(); if(button==1) { if(tm->Active[i]==YES) { Selection=i;break; } else Selection=NONE; } if(kbhit()) break; } Xorbar(tm->x+3,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20,14); } } } PullMenuUp(n,tm);}

    MouseOn1(int cx,int cy,int cx1,int cy1){ int flag; flag=0; if(x>=cx && x<=cx1 && y>=cy && y<=cy1) flag=1; return flag;}

    /* Mouse & win routines */

    union REGS i,o;struct SREGS ss;

    /* 顯示滑鼠游標函式 */void Show(){ i.x.ax=1; int86(0x33,&i,&o);}

    /* 隱藏滑鼠游標函式*/void Hide(){ i.x.ax=2; int86(0x33,&i,&o);}

    /* 獲得滑鼠狀態函式 */void Status(){ i.x.ax=3; int86(0x33,&i,&o); button=o.x.bx; x=o.x.cx; y=o.x.dx;}/*繪製選單項的背景和邊框*/winp(struct MENU *t,int state){ Hide(); /*非高亮模式*/ if(state==1) { /*兩條暗邊框線*/ setcolor(0); line(t->x,t->y,t->x,t->y1); line(t->x,t->y,t->x1,t->y); /*兩條亮邊框線*/ setcolor(15); line(t->x1,t->y,t->x1,t->y1); line(t->x,t->y1,t->x1,t->y1); } /*高亮模式*/ else { /*兩條亮邊框線*/ setcolor(15); line(t->x,t->y,t->x,t->y1); line(t->x,t->y,t->x1,t->y); /*兩條暗邊框線*/ setcolor(0); line(t->x1,t->y,t->x1,t->y1); line(t->x,t->y1,t->x1,t->y1); } Show();}/*繪製一個矩形區域*/Win(int sx,int sy,int ex,int ey){ Hide(); /*設定填充的顏色*/ setfillstyle(1,7); /*繪製一個區域*/ bar(sx,sy,ex,ey); /*兩條亮邊框線*/ setcolor(15); line(sx,sy,sx,ey); line(sx,sy,ex,sy); /*兩條暗邊框線*/ setcolor(0); line(ex,sy,ex,ey); line(sx,ey,ex,ey); Show();}

    Msg(char *m){ int l; setcolor(14); rectangle(18,438,622,477); setfillstyle(1,0); bar(20,440,620,475); if(MouseOn(&Neeraj)) { settextstyle(2,0,7); setcolor(11); l=textwidth(m)/2; outtextxy(320-l,440,m); } else { settextstyle(2,0,8); setcolor(15); l=textwidth(m)/2; outtextxy(320-l,440,m); outtextxy(321-l,441,m); }}

    Comments(){ setfillstyle(1,0); bar(140,5,500,40); setcolor(14); rectangle(140,5,500,40); settextstyle(2,0,8); setcolor(15); outtextxy(260,10,"GUI-MENUS"); setfillstyle(1,8); bar(140,150,502,402); setcolor(14); rectangle(138,148,502,402); Msg("Welcome !"); settextstyle(2,0,5); setcolor(10); outtextxy(158,180,"Hello Friends !"); settextstyle(2,0,4); outtextxy(260,200,"Here i am with my menu program."); outtextxy(158,220,"As you can see it"s like windows menu,you can"); outtextxy(158,240,"add more menus to it. It"s just a concept for"); outtextxy(158,260,"building more menus. I hope you will like my effort,"); outtextxy(158,280,"email me at : [email][email protected][/email]"); setcolor(14); outtextxy(370,300,"- Neeraj Sharma"); line(380,304+textheight("A"),380+textwidth("Neeraj Sharma"),304+textheight("A"));}

    End(){ int i,j; setcolor(0); Hide(); for(j=0;j<40;j++) { for(i=0;i<481;i+=20) line(0,0+i+j,640,j+0+i); delay(100); if(kbhit()) if(getch()==27) break; } settextstyle(2,0,5); setcolor(15); outtextxy(220,180,"Thanks for being with us !"); Show(); delay(3000);}

  • 中秋節和大豐收的關聯?
  • 電視劇《我是農民》的故事情節?