在Unix下用C編寫curses程式的一些常用模組(轉)
在Unix下用C編寫curses程式的一些常用模組(轉)[@more@]總有人問這個問題,下面是整理的模組程式碼,可以作為程式設計的參考demo。幾個部分可以連線起來編譯程可執行程式執行。初始化資源[code:1:b2615b2355]void initial() /* 自定開啟 curses 函式 */ { initscr(); cbreak(); nonl(); noecho(); intrflush(stdscr,FALSE); keypad(stdscr,TRUE); refresh(); } [/code:1:b2615b2355]/* 按鍵等待函式 */ [code:1:b2615b2355]void keycont() { fprintf(stderr, "按鍵繼續..."); getchar(); } [/code:1:b2615b2355]/* 執行可執行程式函式 */ [code:1:b2615b2355]void execprog() { system("clear"); fprintf(stderr, "%s:
", scrpos->item); system(scrpos->prog); keycont(); initial(); touchwin(boxwin); touchwin(curw); keypad(curw, TRUE); wrefresh(boxwin); wrefresh(curw); } [/code:1:b2615b2355]/* 清除視窗函式 */ [code:1:b2615b2355]void clearwin() { wmove(boxwin, 0, 0); wclrtobot(boxwin); wrefresh(boxwin); delwin(curw); delwin(boxwin); } [/code:1:b2615b2355]/* 主函式 */ [code:1:b2615b2355]main() { initial(); getmenuconf(0); /* 取第0號選單引數 */ /* 建立主視窗 */ menuwin=newwin(m_conf.m_lengh, m_conf.m_wight, m_conf.m_bx+1, m_conf.m_by+1); curw=menuwin; lastw[wno]=menuwin; getitem(); /* 取當前選單各項內容 */ domenu(head, 0); endwin(); } [/code:1:b2615b2355]/* 取選單各項引數函式 */ [code:1:b2615b2355]void getitem() { FILE *fp; char buff[0x100]; /* 建邊框視窗 */ boxwin=newwin(m_conf.m_lengh+2,m_conf.m_wight+2,m_conf.m_bx,m_conf.m_by); keypad(curw, TRUE); if (m_conf.bord_flag==1) { box(boxwin, 0,0 ); wrefresh(boxwin); } head=NULL; if ((fp = fopen("./menu.def","r")) == NULL) { fprintf(stderr, "
不能開啟選單定義檔案
"); return; } while( fgets(buff, 0x100, fp)!=NULL) { get_m_item(buff); if (m_item.menu_code != menu_no) continue; new=(struct menu*)malloc(sizeof(struct menu)); if (head == NULL) { last = head; head = new; } else { this->next = new; last = this; } this = new; this->menu_code=m_item.menu_code; this->item_order=m_item.item_order; strcpy(this->item,m_item.item); strcpy(this->prog,m_item.prog); this->submenu_code=m_item.submenu_code; this->next=NULL; this->prev = last; } fclose(fp); } [/code:1:b2615b2355]/* 選單處理函式 */ [code:1:b2615b2355]void domenu(curscrp, curp) struct menu *curscrp; int curp; { int i, x, y; struct menu *mpos; this = head; disponepage(this); curpos = curp; scrpos = curscrp; lastcurpos = lastscrcurpos = 0; revcurpos(); for(;;) { switch (wgetch(curw)) { case ENT: /* 有下一級選單 */ if ((!strcmp(scrpos->prog, "0")) && (scrpos->submenu_code != 0)) { lastbegin = begin->next; getmenuconf(scrpos->submenu_code); menu_no = scrpos->submenu_code; wno++; lastmenucur[wno]=curpos; lastscr[wno] = scrpos; lastw[wno]=curw; workwin=newwin(m_conf.m_lengh,m_conf.m_wight,m_conf.m_bx+1,m_conf.m_by+1); curw=workwin; getitem(); domenu(head, 0); } /* 是內部函式 */ /* 是外部可執行程式 */ else { endwin(); execprog(); } break; case ESC: case 'q': case 'Q': case '0': /* 無上級選單 */ if (m_conf.last_code == -1) { clearwin(); endwin(); exit(0); } /* 有上級選單 */ else { menu_no = m_conf.last_code; clearwin(); getmenuconf(menu_no); getitem(); touchwin(lastw[wno]); curw=lastw[wno]; curpos = lastmenucur[wno]; scrpos = lastscr[wno]; wno--; wrefresh(curw); } break; case 'r': case 'R': case REFRESH: /* 重顯螢幕 */ wrefresh(curscr); break; case KEY_RIGHT: /* 右游標鍵 */ if ( scrpos->next != NULL ) { lastcurpos = curpos; lastscrpos = scrpos; scrpos=scrpos->next; getyx(curw, x, y); if((x==m_conf.m_lengh-1)&&(curpos%m_conf.m_col==m_conf.m_col-1)){ curpos-=(m_conf.m_col-1); lastcurpos = curpos - 1; /* 實現向上卷屏 */ wmove(curw, 0, 0); wdeleteln(curw); dispnextline("R"); } else curpos++; if ((curpos%m_conf.m_col == 0) && (m_conf.m_lengh == 1)) { revcurpos(); break; } else { nomlastpos(); revcurpos(); } } break; case KEY_LEFT: /* 左游標鍵 */ if ( scrpos->prev != NULL ) { lastcurpos = curpos; lastscrpos = scrpos; scrpos=scrpos->prev; getyx(curw, x, y); if ((x==0) && (curpos%m_conf.m_col ==0)) { curpos+=m_conf.m_col-1; lastcurpos = curpos + 1; /* 實現向下卷屏 */ winsertln(curw); dispprevline("L"); } else curpos--; if ((curpos%m_conf.m_col==m_conf.m_col-1)&&(m_conf.m_lengh==1)) { revcurpos(); break; } else { nomlastpos(); revcurpos(); } } break; case KEY_UP: /* 上游標鍵 */ lastcurpos = curpos; lastscrpos = scrpos; mpos = scrpos; for(i=0; i if ( mpos->prev != NULL ) mpos=mpos->prev; else break; } if ( i==m_conf.m_col ) { getyx(curw, x, y); if (x==0) { lastcurpos += m_conf.m_col; /* 實現向下卷屏 */ winsertln(curw); dispprevline("U"); } else { curpos-=m_conf.m_col; } scrpos = mpos; if ( m_conf.m_lengh!=1) nomlastpos(); revcurpos(); } break; case KEY_DOWN: /* 下游標鍵 */ lastcurpos = curpos; lastscrpos = scrpos; mpos = scrpos; for(i=0;i if ( mpos->next != NULL ) mpos=mpos->next; else break; } if ( i==m_conf.m_col ) { getyx(curw, x, y); if (x==m_conf.m_lengh-1) { lastcurpos -= m_conf.m_col; /* 實現向上卷屏 */ wmove(curw, 0, 0); wdeleteln(curw); dispnextline("D"); } else curpos+=m_conf.m_col; scrpos = mpos; if ( m_conf.m_lengh!=1) nomlastpos(); revcurpos(); } break; default: beep(); break; } } } [/code:1:b2615b2355]/* 反顯當前項函式 */ [code:1:b2615b2355]void revcurpos() { wattrset(curw, A_STANDOUT); wmove(curw, curpos/m_conf.m_col, (curpos%m_conf.m_col)*m_conf.m_wight/m_conf.m_col+m_conf.m_col); wprintw(curw, "%s", scrpos->item); wattrset(curw, A_NORMAL); wrefresh(boxwin); } [/code:1:b2615b2355]/* 正常顯示上一項函式 */ [code:1:b2615b2355]void nomlastpos() { wmove(curw, lastcurpos/m_conf.m_col, (lastcurpos%m_conf.m_col) *m_conf.m_wight/m_conf.m_col+m_conf.m_col); wprintw(curw, "%s", lastscrpos->item); } [/code:1:b2615b2355]/* 顯示一頁函式 */ [code:1:b2615b2355]void disponepage(first) struct menu *first; { short col, row; begin=first; /* begin 為本頁首指標 */ for(row=0;row //這兩個地方都不是很清楚,我想這個函式是想列印選單的,所以照這個意思寫for(col=0;col /* m_conf.m_wight/m_col為每一選單項應占列數*/ wmove(curw,row,col*m_conf.m_wight/m_conf.m_col+m_conf.m_col); wprintw(curw, "%s", first->item); wrefresh(curw); last = first; first = first->next; if (first == NULL) { break; } } } } [/code:1:b2615b2355]/* 顯示上一行函式 */ [code:1:b2615b2355]void dispprevline(flag) char flag[2]; /* L-左游標引起 U-上游標引起 */ { struct menu *tmppos; int tmpcurpos; tmpcurpos = curpos; tmppos = scrpos; if ( flag[0] == 'U') { while ( tmpcurpos % m_conf.m_col != 0) { tmppos = tmppos->prev; tmpcurpos--; } tmppos = tmppos->prev; } for (tmpcurpos = m_conf.m_col-1; tmpcurpos >= 0; tmpcurpos--) { wmove(curw, 0, (tmpcurpos%m_conf.m_col) *m_conf.m_wight/m_conf.m_col+m_conf.m_col); wprintw(curw, "%s", tmppos->item); begin = tmppos; /*begin 為本頁首指標*/ last = tmppos; tmppos = tmppos->prev; if (tmppos == NULL) break; } wrefresh(curw); } [/code:1:b2615b2355]/* 顯示下一行函式 */ [code:1:b2615b2355]void dispnextline(flag) char flag[2];/* R-右游標引起 D-下游標引起 */ { struct menu *tmppos; int tmpcurpos; tmpcurpos = curpos; tmppos = scrpos; if ( flag[0] == 'D') { while ( tmpcurpos % m_conf.m_col != m_conf.m_col-1) { tmppos = tmppos->next; tmpcurpos++; } tmppos = tmppos->next; } for (tmpcurpos = 0; tmpcurpos < m_conf.m_col; tmpcurpos++) { wmove(curw, m_conf.m_lengh-1, (tmpcurpos%m_conf.m_col) *m_conf.m_wight/m_conf.m_col+m_conf.m_col); wprintw(curw, "%s", tmppos->item); last=tmppos;/* last 為本頁最後一個結點指標 */ begin=tmppos; tmppos = tmppos->next; if (tmppos == NULL) break; } } [/code:1:b2615b2355]/* 取指定選單引數函式 */ [code:1:b2615b2355]void getmenuconf(menu_code) short menu_code; { FILE *fp; char menu_buff[0x100]; if ((fp = fopen("menu.conf", "r"))==NULL) { fprintf(stderr, "can not open menu config file"); return; } while( fgets(menu_buff, 0x100, fp)!=NULL ) { get_m_conf(menu_buff); if (m_conf.menu_code == menu_code) break; } return ; } [/code:1:b2615b2355]/* 取指定選單引數處理函式 */ [code:1:b2615b2355]void get_m_conf(menu_conf) char *menu_conf; { register i, j, k; char buff[20]; j = k = 0; for (i = 0; i < strlen(menu_conf); i++) { if ( menu_conf[i] == '!' ) { j++; if ( j == 1) { k = i+1; continue; } switch(j) { case 2: memcpy(buff, &menu_conf[k], i-k); buff[i-k]=0; m_conf.menu_code = atoi(buff); k=i+1; break; case 3: memcpy(buff, &menu_conf[k], i-k); buff[i-k]=0; m_conf.last_code = atoi(buff); k=i+1; break; case 4: memcpy(buff, &menu_conf[k], i-k); buff[i-k]=0; m_conf.bord_flag = atoi(buff); k=i+1; break; case 5: memcpy(buff, &menu_conf[k], i-k); buff[i-k]=0; m_conf.m_wight = atoi(buff); k=i+1; break; case 6: memcpy(buff, &menu_conf[k], i-k); buff[i-k]=0; m_conf.m_lengh = atoi(buff); k=i+1; break; case 7: memcpy(buff, &menu_conf[k], i-k); buff[i-k]=0; m_conf.m_col = atoi(buff); k=i+1; break; case 8: memcpy(buff, &menu_conf[k], i-k); buff[i-k]=0; m_conf.m_bx = atoi(buff); k=i+1; break; case 9: memcpy(buff, &menu_conf[k], i-k); buff[i-k]=0; m_conf.m_by = atoi(buff); k=i+1; break; default: break; } } } } [/code:1:b2615b2355]/* 取指定項引數處理函式 */ [code:1:b2615b2355]void get_m_item(menu_item) char *menu_item; { register i, j, k; char buff[80]; j = k = 0; for (i = 0; i < strlen(menu_item); i++) { if ( menu_item[i] == '!' ) { j++; if ( j == 1) { k = i+1; continue; } switch(j) { case 2: memcpy(buff, &menu_item[k], i-k); buff[i-k] = 0; m_item.menu_code = atoi(buff); k=i+1; break; case 3: memcpy(buff, &menu_item[k], i-k); buff[i-k] = 0; m_item.item_order = atoi(buff); k=i+1; break; case 4: memcpy(buff, &menu_item[k], i-k); buff[i-k] = 0; strcpy(m_item.item,buff); k=i+1; break; case 5: memcpy(buff, &menu_item[k], i-k); buff[i-k] = 0; strcpy(m_item.prog,buff); k=i+1; break; case 6: memcpy(buff, &menu_item[k], i-k); buff[i-k] = 0; m_item.submenu_code = atoi(buff); k=i+1; break; default: break; } } } } [/code:1:b2615b2355]資料結構和標頭檔案等初始化資訊[code:1:b2615b2355]#include #define ESC 27 #define ENT 13 #define REFRESH 12 #define MAX_M 10 /* 選單最大層數 */ void initial(),nomlastpos(),revcurpos(),disponepage(),dispprevline(); void dispnextline(),domenu(),getmenuconf(),keycont(); void getitem(), get_m_conf(), get_m_item(),clearwin(),execprog(); /* 標識每一選單項的結構 */ struct menu { short menu_code; /* 所屬選單代號 */ short item_order; /* 項順序號 */ char item[20]; /* 選單項名稱 */ char prog[80]; /* 本項選單執行程式 */ short submenu_code; /* 下一級選單編號 */ struct menu *next; /* 指向上一項的指標 */ struct menu *prev; /* 指向下一項的指標 */ } m_item,*head,*this,*new,*last,*scrpos,*lastscrpos,*begin,*lastbegin,*lastscr[MAX_M]; /* 標識每一選單內容的結構 */ struct menuconf { short menu_code; /* 選單代號 */ short last_code; /* 上一級選單代號 */ short bord_flag; /* 邊框標誌 0--無邊框 1--有邊框 **/ short m_wight; /* 選單顯示寬度 */ short m_lengh; /* 每一行項數 */ short m_col; /* 選單列數 */ short m_bx; /* 選單起始橫座標 */ short m_by; /* 選單起始縱座標 */ } m_conf; WINDOW *menuwin, *boxwin, *curw, *lastw[MAX_M], *workwin; long curpos, lastcurpos, lastscrcurpos, lastmenucur[MAX_M]; short menu_no = 0, wno = 0;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-944576/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 在linux下用vim編寫一個C程式LinuxC程式
- unix下編寫socket程式的一般步驟(轉)
- VC在windows下編寫用於序列通訊的程式 (轉)Windows
- UNIX 螢幕導向程式的發展利器 - curses (之二)(轉)
- 用QT在Windows下編寫dll程式QTWindows
- 用 C++Builder 編寫 Tray 程式 (轉)C++UI
- 簡單的介紹UNIX下的常用編輯工具VI(轉)
- Unix系列shell程式編寫
- 用C語言編寫Linux實用程式的藝術(轉)C語言Linux
- Unix系列shell程式編寫:下篇
- Unix系列shell程式編寫:中篇
- Unix系列shell程式編寫:上篇
- 本文幫你在 Unix 下玩轉 C 語言
- 用C#Builder編寫螢幕保護程式 (轉)UI
- 用匯編編寫DOS下的記憶體駐留程式(5) (轉)記憶體
- 用匯編編寫DOS下的記憶體駐留程式(3) (轉)記憶體
- 用匯編編寫DOS下的記憶體駐留程式(4) (轉)記憶體
- 用Delphi編寫DelTree程式 (轉)
- 用Visual C++編寫電子郵件程式 (轉)C++
- 用C++Builder在WINNT下編制一個Service (轉)C++UI
- unix 2 windows 的C++程式移植的一些常見問題 (轉)WindowsC++
- 用VC++編寫CGI程式 (轉)C++
- 用VB編寫抽獎程式 (轉)
- linux下用C編寫的基於smtp的郵件傳送程式Linux
- 在Unix系統下用shell製作通用介面(轉)
- BOOT0的主要程式碼兼Unix下彙編小節(轉)boot
- 怎樣在ubuntu下寫c++程式UbuntuC++
- 用VB編寫非同步多執行緒下載程式 (轉)非同步執行緒
- 常用UNIX命令(轉)
- 在LINUX下編寫程式並執行Linux
- SCO-UNIX在vmware下的安裝(轉)
- 用Delphi編寫安裝程式(1) (轉)
- 用VB編寫標準CGI程式 (轉)
- 編寫Linux實用程式的藝術(轉)Linux
- 用C語言編寫windows服務程式C語言Windows
- Linux下c mysql程式的編譯命令(轉)LinuxMySql編譯
- IEDA編寫程式碼常用的快捷鍵
- Unix中Vi編輯常用命令總結(轉)