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

📄 window.c

📁 linux平台下用C开发的一个超市管理系统(结合了ORACLE数据库)
💻 C
字号:
#include"window.h"


void init_color_info()//初始化颜色
{
	initscr();
	if(has_colors() == FALSE)
	{
		endwin();
		exit(1);
	}		
	start_color();
	refresh();
	init_pair(1,COLOR_WHITE,COLOR_BLACK);//白字黑底
	init_pair(2,COLOR_BLACK,COLOR_WHITE);//黑字白底
	init_pair(3,COLOR_BLACK,COLOR_RED);//黑字红底
	init_pair(4,COLOR_WHITE,COLOR_BLUE);//白字蓝底
	init_pair(5,COLOR_MAGENTA,COLOR_WHITE);//紫字白底
	init_pair(6,COLOR_RED,COLOR_GREEN);//红字绿底
	init_pair(7,COLOR_WHITE,COLOR_MAGENTA);//白字紫底
	init_pair(8,COLOR_BLUE,COLOR_WHITE);//黄字绿底
}

void free_win(WIN *new_win)
{	
	delwin(new_win->win);
	free(new_win);
}
void input_title(WIN *new_win)
{
	int len;
	len=strlen(new_win->title);		
	if(len>0)
	{
		mvwprintw(new_win->win,0,(new_win->cols-len)/2,new_win->title);							
	}
}

void show_win(WIN *new_win)
{
	input_title(new_win);
	wrefresh(new_win->win);

}
WIN * make_win(WIN *new_win)
{
	WIN *p=NULL;
	p=(WIN *)malloc(sizeof(WIN));
	if(p==NULL)
	{
		return NULL;
	}
	memset(p,0,sizeof(WIN));
	memcpy(p,new_win,sizeof(WIN));	
	p->win=newwin(p->line,p->cols,p->x,p->y);
	wbkgd(p->win,COLOR_PAIR(p->color));	
	if(p->frame==1)
	{
		box(p->win,0,0);
	}
	p->input_title=input_title;
	p->show_win=show_win;
	p->free_win=free_win;
	return p;
}


void free_text(TEXT *new_textbox)
{
	delwin(new_textbox->win);
	free(new_textbox);
}
void input_text(TEXT *new_textbox)
{
	int len;
	len=strlen(new_textbox->text);		
	if(len>0)
	{
		mvwprintw(new_textbox->win,0,(new_textbox->maxLen-len)/2,new_textbox->text);							
	}
}

void show_text(TEXT *new_textbox)
{
	input_text(new_textbox);
	wrefresh(new_textbox->win);
//	delwin(new_textbox->win);
//	free(new_textbox);
}

TEXT * make_textbox(TEXT *new_textbox)
{
	TEXT *p=NULL;
	p=(TEXT *)malloc(sizeof(TEXT));
	if(p==NULL)
	{
		return NULL;
	}
	memset(p,0,sizeof(TEXT));
	memcpy(p,new_textbox,sizeof(TEXT));	
	p->win=newwin(1,p->maxLen,p->x,p->y);
	wbkgd(p->win,COLOR_PAIR(p->color));		
	p->input_text=input_text;
	p->show_text=show_text;
	p->free_text=free_text;
	return p;
}


void free_button(BUTTON *new_button)
{
	delwin(new_button->win3);
	delwin(new_button->win2);
	delwin(new_button->win1);
	free(new_button);
}
void input_button(BUTTON *new_button)
{
	int len;
	len=strlen(new_button->title);		
	if(len>0)
	{
		mvwprintw(new_button->win3,new_button->wide/2,(new_button->length-len)/2,new_button->title);							
	}

}
void show_button(BUTTON *new_button)
{
	input_button(new_button);
	wrefresh(new_button->win1);
    wrefresh(new_button->win3);
}
void focus_button(BUTTON *new_button)
{
	mvwin(new_button->win3,new_button->x+1,new_button->y+1);
    wrefresh(new_button->win3);
	usleep(90000);
    mvwin(new_button->win3,new_button->x,new_button->y);
    touchwin(new_button->win1);
	wrefresh(new_button->win1);	
	wrefresh(new_button->win3);
	usleep(90000);
}
void print_white(BUTTON *new_button)
{
	
	curs_set(0);
	wattron(new_button->win3,A_REVERSE|A_BOLD|COLOR_PAIR(1));
	input_button(new_button);
	wattroff(new_button->win3,A_REVERSE|A_BOLD|COLOR_PAIR(1));
    touchwin(new_button->win3);
	wrefresh(new_button->win3);

}
void delete_white(BUTTON *new_button)
{
	curs_set(1);
    input_button(new_button);
	touchwin(new_button->win3);
	wrefresh(new_button->win3);
}
BUTTON *make_button(BUTTON *new_button)
{
	BUTTON *p=NULL;
	p=(BUTTON *)malloc(sizeof(BUTTON));
	if(p==NULL)
	{
		return NULL;
	}
	memset(p,0,sizeof(BUTTON));
	memcpy(p,new_button,sizeof(BUTTON));
    p->win1=newwin(p->wide+1,p->length+1,p->x,p->y);
    wbkgd(p->win1,COLOR_PAIR(p->bcolor));
	p->win2=subwin(p->win1,p->wide,p->length,p->x+1,p->y+1);
    wbkgd(p->win2,COLOR_PAIR(1));
    p->win3=newwin(p->wide,p->length,p->x,p->y);
    wbkgd(p->win3,COLOR_PAIR(p->fcolor));
	box(p->win3,0,0);
	p->input_button=input_button;
	p->show_button=show_button;
	p->focus_button=focus_button;
	p->print_white=print_white;
	p->delete_white=delete_white;
    return p;
}

void draw_line(WINDOW *p,int y,int length)//画横线
{
	mvwhline(p,y,1,ACS_HLINE,length-2);
	mvwaddch(p,y,0,ACS_LTEE);
	mvwaddch(p,y,length-1,ACS_RTEE);	
}



int show_msg(char msg[],int kind)
{
	int len,ch,k=1;
	WIN mainatt1={10,30,10,24,4,"消息框",1};
	BUTTON but3={6,3,15,29,"是",4,4};
	BUTTON but4={6,3,15,43,"否",4,4};
	WIN *w2=make_win(&mainatt1);
	len=strlen(msg);
	scr_dump("msg.scr");
	mvwprintw(w2->win,2,(30-len)/2,msg);
	if(kind==0)
	{
		
		mvwprintw(w2->win,4,8,"请按任意键继续");
		w2->show_win(w2);
		noecho();
		cbreak();					
		keypad(w2->win,true );
		wgetch(w2->win);
		free_win(w2);
		scr_restore("msg.scr");
		refresh();		
		return 1;
	}
	if(kind==1)
	{
		w2->show_win(w2);
		BUTTON *b3=make_button(&but3);
		BUTTON *b4=make_button(&but4);
		b3->show_button(b3);
		b4->show_button(b4);
		do
		{
			switch(k)
			{
				case 1:
					noecho();
					cbreak();					
					keypad(b3->win3,true );
					print_white(b3);
					ch=wgetch(b3->win3);
					if(ch==10)
					{
						b3->focus_button(b3);						
						scr_restore("msg.scr");
						refresh();					
						free_win(w2);
						free_button(b3);
						free_button(b4);
						return 0;//确定
					}
					else if(ch==27||ch==261)
					{
						b3->delete_white(b3);
						k=2;						
						continue;
					}
					else
					{  
						continue;
					}
				case 2:
					noecho();
					cbreak();					
					keypad(b4->win3,true );
					print_white(b4);
					ch=wgetch(b4->win3);
					if(ch==10)
					{
						b4->focus_button(b4);						
						scr_restore("msg.scr");
						refresh();
						free_win(w2);
						free_button(b3);
						free_button(b4);
						return 1;//取消
					}
					else if(ch==260)
					{
						b4->delete_white(b4);
						k=1;
						continue;
					}
					else
					{
						k=2;
						continue;
					}
			}
		}while (1);			
	}
	
}

int run_button(BUTTON *b1,BUTTON *b2,int type)//type=0 有向上功能,type=1 无向上功能
{
	int k=1,ch;
	while(1)
	{
		if(k==1)
		{
			noecho();
			cbreak();					
			keypad(b1->win3,true );
			print_white(b1);
			ch=wgetch(b1->win3);
			if(ch==10)
			{
				b1->focus_button(b1);
				b1->delete_white(b1);
//				free_button(b1);
//				free_button(b2);
				return 1;//确定
			}
			if(ch==27||ch==261)
			{
				b1->delete_white(b1);
				k=2;						
				continue;
			}
			if(ch==259)
			{
				if(type==1)
				{
					continue;
				}
				else
				{
					b1->delete_white(b1);
					return 3;//向上
				}
			}
		}
		if(k==2)
		{
			noecho();
			cbreak();					
			keypad(b2->win3,true );
			print_white(b2);
			ch=wgetch(b2->win3);
			if(ch==10)
			{
				b2->focus_button(b2);				
//				free_button(b1);
//				free_button(b2);
				b2->delete_white(b2);
				return 2;//退出
			}
			if(ch==260)
			{
				b2->delete_white(b2);
				k=1;						
				continue;
			}
			if(ch==259)
			{
				if(type==1)
				{
					continue;
				}
				else
				{
					b2->delete_white(b2);
					return 3;//向上
				}
			}
		}
	}
}

⌨️ 快捷键说明

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