⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cwind.cpp

📁 一个在unix下用curses实现的菜单程序
💻 CPP
字号:
#include "CWind.hpp"
#include <curses.h>
#include <string.h>
#include <stdlib.h>
static char FlagState;
CWind::CWind()
{
	if(!FlagState)
	{
		initscr();
		cbreak();
  		noecho();
  		start_color();
		init_color(COLOR_RED,0,0,1000);
		init_pair(COLOR_PAIR(3),9,0);
	}
	FlagState++;
	Color=0;
}
CWind::~CWind()
{
	FlagState--;
	if(!FlagState)
	{
		endwin();
	}
	y=0;
}
int CWind::CreateWindow(char x,char y,char High,char Witch)
{
	if(High>0&&High<5||Witch>0&&Witch<5)
		return -1;
	win=newwin(Witch,High,y,x);
	if(!win)
		return -2;
	box(win, ACS_VLINE, ACS_HLINE);
	OnCreate();
//	refresh();
//	wrefresh(win);
	return 0;
}
int CWind::ClearWindows()
{
	wclear(win);
	wrefresh(win);
	box(win, ACS_VLINE, ACS_HLINE);
	y=0;
	return 0;
}
int CWind::Run()
{
	OnInitialize();
	while(1)
	{
		char ch=getch();
		OnWindow(&ch,0);//键盘消息
	}
	return 0;
}
int CWind::OnWindow(void * lp,int type)
{
	switch(type)
	{
	case 0://键盘消息
		break;
	}
	return 0;
}
int CWind::SetColor(int CurrColor,int BackColor)
{
	Color%=65;
	init_pair(++Color,CurrColor, BackColor); /*建立一个颜色对*/
	attron(COLOR_PAIR(Color)); /*开启字符输出颜色*/
	return Color;
}
int CWind::EndColor(int Color)
{
	return attroff(COLOR_PAIR(Color)); /*关闭颜色显示*/
}

int CWind::Print(char x,char y,char *Farmat,...)
{
	int len=strlen(Farmat);
	char *pBuf=(char*)malloc(len+1256);
	if(!pBuf)
		return -2;
	va_list argList;
	va_start(argList,Farmat); 
	vsprintf(pBuf,Farmat,argList);
	va_end(argList);
	if(y>=win->_maxy-1)
		return -1;
	x=win->_begx+x+1;
	y=win->_begy+y+1;
	mvprintw(y,x,"%s",pBuf);
	refresh();
	wrefresh(win);
	free(pBuf);
	return 0;
}
int CWind::Printf(int x,int y,char *Farmat,...)
{
	if(y>=GetMaxY()-1)
		return -1;
	wmove(win,y+1,x+1);
	va_list argList;
	va_start(argList,Farmat); 
	vwprintw(win,Farmat,argList);
	va_end(argList);
}
int CWind::ScrollYOut(char *Farmat,...)
{
//	wmove(win,y+1,1);
	va_list argList;
	va_start(argList,Farmat); 
	vwprintw(win,Farmat,argList);
	va_end(argList);
	y++;
	if(y>=GetMaxY())
	{
//		y=GetMaxY();
		scroll(win);
		refresh();
		wrefresh(win);
		wmove(win,0,0);
		y=0;
	}
	return 0;
}
int CWind::GetMaxX()
{
	return win->_maxx;
}
int CWind::GetMaxY()
{
	return win->_maxy;
}
CMenu::CMenu()
{
}
int CMenu::ShowMenu()
{
	vector<MENUID>::iterator lp;
	char y=0;
	Print(0,y,"%s",lp->MenuName);
	for(lp=Menu.begin();lp!=Menu.end();lp++)
	{
		Printf(0,y,"%s",lp->MenuName);
		y++;
	}
	return 0;
}
int CMenu::CreateMenu(int x,int y,int Witch,int Height)
{
	return CreateWindow(x,y,Witch,Height);
}
int CMenu::CreateItem(int type,char MenuName[20],int ID,CMenu *pSubMenu)
{
	MENUID TempMenu;
	memset(&TempMenu,0,sizeof(MENUID));
	TempMenu.Type=type;
	memcpy(TempMenu.MenuName,MenuName,20);
	TempMenu.item=ID;
	TempMenu.lpMenu=pSubMenu;
	Menu.push_back(TempMenu);
	return 0;
}
int CMenu::UpDateMenu()
{
	ClearWindows();
	ShowMenu();
}
int CMenu::ShowWindow(int x,int y1)
{
	win->_begx=x;
	win->_begy=y1;
	vector<MENUID>::iterator lp;
	lp=Menu.begin();
	UpDateMenu();
	int Color=SetColor(COLOR_RED, COLOR_GREEN);
	Print(0,0,"%s",lp[0].MenuName);
	Print(0,0,"%s",lp[0].MenuName);
	EndColor(Color);
	char y=0;
	while(1)
	{
		int ch[1];
		ch[0]=wgetch(win);
		Print(0,y,"%s",lp[y].MenuName);
		switch(ch[0])
		{
		case 'w':
		case 'W':
		case 65:
			y--;
			if(y<0)
			{
				ClearWindows();
				return 0;
			}
			break;
		case 's':
		case 'S':
		case 66:
			y=(lp+y)!=Menu.end()?y+1:y;
			break;
		case 10:
			{
				if(lp[y].lpMenu)
				{
					lp[y].lpMenu->ShowWindow(win->_begx+5,y>=GetMaxY()-2?GetMaxY()-2:y);
					UpDateMenu();
					int Color=SetColor(COLOR_RED, COLOR_GREEN);
					Print(1,y,"%s%d",lp[y].MenuName,ch[0]);
					EndColor(Color);
				}
				else
				{
					ClearWindows();
					OnWindow(&lp[y].item,1);
					return 0;
				}
			}
			break;
		case 'q':
		case 'Q':
			ClearWindows();
			return 0;
		}
		ScrollY(y);
		Color=SetColor(COLOR_RED, COLOR_GREEN);
		Print(0,y>=GetMaxY()-2?GetMaxY()-2:y,"%s",lp[y].MenuName);
		Print(0,y>=GetMaxY()-2?GetMaxY()-2:y,"%s",lp[y].MenuName);
		EndColor(Color);
	}
}
int CMenu::ScrollY(int y)
{
	if(y-GetMaxY()<1)
		return 0;
	y=y-GetMaxY();
	vector<MENUID>::iterator lp;
	lp=Menu.begin();
	ClearWindows();
	Print(0,0,"%s",lp[y].MenuName);
	for(int i=0;i<GetMaxY();i++)
	{
		if(lp+y==Menu.end())
			return -1;
		Print(0,i,"%s",lp[y].MenuName);
		y++;
	}
	return 0;
}

MainMenu::MainMenu()
{
	CurrXY=0;
	CreateWindow(0,0,3,0);
}
int MainMenu::CreateMenu(char *MenuName,int ID,CMenu *pMenu)
{
	int len=strlen(MenuName);
	len=len>15?15:15;
	MAINMENU TempMenu;
	memset(&TempMenu,0,sizeof(MENUID));
	memcpy(TempMenu.MenuName,MenuName,20);
	TempMenu.MenuName[len]=0;
	TempMenu.item=ID;
	TempMenu.x=CurrXY;
	CurrXY+=len;
	TempMenu.lpMenu=pMenu;
	Menu.push_back(TempMenu);
}
int MainMenu::ShowWindow()
{
	vector<MAINMENU>::iterator lp;
	char y=0;
	Print(0,y,"%s",lp->MenuName);
	for(lp=Menu.begin();lp!=Menu.end();lp++)
	{
		Print(lp->x,0,"%s",lp->MenuName);
		y++;
	}
	while(1)
	{
		for(int i=0;i<y&&i>-1;)
		{
			int ch=wgetch(win);
			switch(ch)
			{
			case 'd':
			case 'D':
				i++;
				break;
			case 'a':
			case 'A':
				i--;
				break;
			case 10:
				lp[i].lpMenu->ShowWindow(lp[i].x,lp[i].y+3);
				break;
			}
			Color=SetColor(COLOR_RED, COLOR_GREEN);
			Print(lp[i].x,0,"%s",lp->MenuName);
			EndColor(Color);
		}
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -