O как !!!
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено Котик 24 июня 2004 г. 10:10
В ответ на: Бросьте в меня примером реализации меню (на С). Ведь многие делали! отправлено Getm 24 июня 2004 г. 09:52

//
// (с)Котик
//

#include

//
//
//

#define TRUE 1
#define FALSE 0

//
//
//

unsigned char x = 2, y = 2;
unsigned char ExitFlag = FALSE;
unsigned char KeyPressed = 0;

//
//
//

void Item0Action()
{
attron(COLOR_PAIR(1));
mvprintw(20,10,"Item0 action applied");
attroff(COLOR_PAIR(1));
refresh();
}

void Item1Action()
{
attron(COLOR_PAIR(1));
mvprintw(20,10,"Item1 action applied");
attroff(COLOR_PAIR(1));
refresh();
}

void Item2Action()
{
attron(COLOR_PAIR(1));
mvprintw(20,10,"Item2 action applied");
attroff(COLOR_PAIR(1));
refresh();
}

void Item3Action()
{
attron(COLOR_PAIR(1));
mvprintw(20,10,"Item3 action applied");
attroff(COLOR_PAIR(1));
refresh();
}

//
//
//

struct TItem
{
char *ItemCaption;
unsigned char Index;
unsigned char IsSelected;
unsigned char XPos;
unsigned char YPos;
void(*PaintFunction)(struct TItem *Self);
void(*ActionFunction)();
};

typedef struct TItem TMenuItem;

void RepaintItem(struct TItem *Self)
{
unsigned char SelectedColorPair;

if(Self->IsSelected)SelectedColorPair = 2;
else SelectedColorPair = 1;
attron(COLOR_PAIR(SelectedColorPair));
mvprintw(Self->YPos, Self->XPos, Self->ItemCaption);
attroff(COLOR_PAIR(SelectedColorPair));
};

TMenuItem MainMenu[4] = {
{" assa 0 ", 0, TRUE, 10, 10, RepaintItem, Item0Action},
{" assa 1 ", 0, FALSE, 10, 11, RepaintItem, Item1Action},
{" assa 2 ", 0, FALSE, 10, 12, RepaintItem, Item2Action},
{" assa 3 ", 0, FALSE, 10, 13, RepaintItem, Item3Action},
};

unsigned char SelectedItemN = 0;
unsigned char MainMenuItemsN = 4;

void DisplayMainMenu()
{
unsigned char i;

for(i = 0; i < 4; i++)
(*MainMenu[i].PaintFunction)(&MainMenu[i]);
};

void ProcessMainMenu()
{
unsigned char i;

for(i = 0; i < 4; i++)
if(i == SelectedItemN)MainMenu[i].IsSelected = TRUE;
else MainMenu[i].IsSelected = FALSE;
};

//
//
//

int main(void)
{
initscr();
start_color();
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_BLACK, COLOR_GREEN);

noecho();
curs_set(0); // set cursor invisible
box(stdscr, ACS_VLINE, ACS_HLINE);
attron(COLOR_PAIR(1));
mvprintw(0,23," Simple one-level menu sample ");
mvprintw(24,13," Use numpad arrow keys to move '*', 'q' - to quit :) ");
attroff(COLOR_PAIR(2));
DisplayMainMenu();
refresh();

while(1)
{
KeyPressed = getch();
switch(KeyPressed)
{
case('-'):
{
if(SelectedItemN)SelectedItemN--;
break;
};
case('+'):
{
if(SelectedItemN < MainMenuItemsN - 1)SelectedItemN++;
break;
}
case('q'):ExitFlag = TRUE;break;
case(10):
{
(*MainMenu[SelectedItemN].ActionFunction)();
break;
}
default: break;
};
if(ExitFlag)break;
ProcessMainMenu();
DisplayMainMenu();
refresh();
};
curs_set(1); // set cursor visible
clear();
refresh();
endwin();
return 0;
}

Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru