ncurses下无法响应鼠标事件,高手进来看看

软件和网站开发以及相关技术探讨
回复
orcknight
帖子: 7
注册时间: 2009-03-01 15:42

ncurses下无法响应鼠标事件,高手进来看看

#1

帖子 orcknight » 2009-05-03 14:41

#include<curses.h>
#include<string.h>
#define WIDTH 30
#define HEIGHT 10
int startx=0;
int starty=0;
char *choices[]={"Choice 1","Choice 2","Choice 3","Choice 4","Exit"};
int n_choices=sizeof(choices)/sizeof(char *);
void print_menu(WINDOW *menu_win,int highlight);
void report_choice(int mouse_x,int mouse_y,int*p_choice);
int main()
{
int c,choice=0;
WINDOW *menu_win;
MEVENT event;
initscr();
clear();
noecho();
cbreak();
startx=(80-WIDTH)/2;
starty=(24-HEIGHT)/2;
attron(A_REVERSE);
mvprintw(23,1,"click on Exit to quit.");
refresh();
attroff(A_REVERSE);
menu_win=newwin(HEIGHT,WIDTH,starty,startx);
print_menu(menu_win,1);
mousemask(ALL_MOUSE_EVENTS,NULL);
while(1)
{
c=wgetch(menu_win);
switch(c)
{
case KEY_MOUSE:
if(getmouse(&event)==OK)
{
if(event.bstate&BUTTON1_PRESSED)
{
report_choice(event.x+1,event.y+1,&choice);
if(choice==-1)
goto end;
mvprintw(22,1,"choice made is:%d string chosen is \"%10s\"",choice,choices[choice-1]);
refresh();
}
}
print_menu(menu_win,choice);
break;
}
}
end:
endwin();
return 0;
}
void print_menu(WINDOW *menu_win,int highlight)
{
int x,y,i;
x=2;
y=2;
box(menu_win,0,0);
for(i=0;i<n_choices;++i)
{
if(highlight==i+1)
{
wattron(menu_win,A_REVERSE);
mvwprintw(menu_win,y,x,"%s",choices);
wattroff(menu_win,A_REVERSE);
}
else
mvwprintw(menu_win,y,x,"%s",choices);
++y;
}
wrefresh(menu_win);
}
void report_choice(int mouse_x,int mouse_y,int *p_choice)
{
int i,j,choice;
i=startx+2;
j=starty+3;
for(choice=0;choice<n_choices;++choice)
if(mouse_y==j+choice&&mouse_x>=i&&mouse_x<=i+strlen(choices[choice]))
{
if(choice==n_choices-1)
*p_choice=-1;
else
*p_choice=choice+1;
break;
}
}
为什么照着教程上写的却没有得到预期效果呢,运行编译后的程序没有一点反应
orcknight
帖子: 7
注册时间: 2009-03-01 15:42

Re: ncurses下无法响应鼠标事件,高手进来看看

#2

帖子 orcknight » 2009-05-04 17:29

:em06 没人会么阿
似水流年411328
帖子: 1
注册时间: 2016-10-03 16:28
系统: ubuntu

Re: ncurses下无法响应鼠标事件,高手进来看看

#3

帖子 似水流年411328 » 2016-10-03 16:32

:Adore 兄弟,这个问题解决了吗?我也遇到了这个问题
回复