LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 973|回复: 5

在ncurses中获得鼠标点击的问题

[复制链接]
发表于 2003-10-17 12:03:45 | 显示全部楼层 |阅读模式
刚才,我用 ncurses库  做了一个简单的菜单(照别人写的程序写的) :-)

他是用 c=getch();
       if (c == KEY_MOUSE) {.......}

括号里面是鼠标的处理.

在 ncurses.h 定义 KEY_MOUSE  为 0632

可是,返回的c值却很大,不知道为什么?

哪位仁兄,也遇到过类似的问题?

请指教!
 楼主| 发表于 2003-10-18 08:57:59 | 显示全部楼层
谁能指教一下呀!

:help
发表于 2003-10-18 10:20:43 | 显示全部楼层
先调用mousemask,再
c=getch();
if (c == KEY_MOUSE)
再getmouse
应该没什么问题
 楼主| 发表于 2003-10-19 11:50:15 | 显示全部楼层
#include <ncurses.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;

        /* Initialize curses */
        initscr();
        clear();
        noecho();
        cbreak();        //Line buffering disabled. pass on everything

        /* Try to put the window in the middle of screen */
        startx = (80 - WIDTH) / 2;
        starty = (24 - HEIGHT) / 2;
       
        attron(A_REVERSE);
        mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
        refresh();
        attroff(A_REVERSE);

        /* Print the menu for the first time */
        menu_win = newwin(HEIGHT, WIDTH, starty, startx);
        print_menu(menu_win, 1);
        /* Get all the mouse events */
        mousemask(ALL_MOUSE_EVENTS, NULL);
       
        while(1)
        {        c = wgetch(menu_win);
                switch(c)
                {        case KEY_MOUSE:
                        if(getmouse(&event) == OK)
                        {        /* When the user clicks left mouse button */
                                if(event.bstate & BUTTON1_PRESSED)
                                {        report_choice(event.x + 1, event.y + 1, &choice);
                                        if(choice == -1) //Exit chosen
                                                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);
}

/* Report the choice according to mouse position */
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;
                }
}

这是他的源码,运行就是不好使.

c = wgetch(menu_win);  得到的C值很大,不会和KEY_MOUSE相同

请大家试试!
发表于 2003-10-19 17:47:36 | 显示全部楼层
我做了一下试验,发现初始化的时候应该调用keypad(stdscr, TRUE);,而且getmouse得到的event.bstate应该是BUTTON1_CLICHED
打印跟踪信息是最好的调试法,一定要掌握

  1. #include <ncurses.h>

  2. int
  3. main(void)
  4. {
  5.   int c;
  6.   MEVENT event;
  7.   int e = 1;

  8.   initscr();
  9.   clear();
  10.   noecho();
  11.   cbreak();
  12.   keypad(stdscr, TRUE);

  13.   mousemask(ALL_MOUSE_EVENTS, NULL);

  14.   while(e){
  15.     c = wgetch(stdscr);
  16.     mvprintw(0, 0, "c = %o      ", c);
  17.     switch(c){
  18.     case KEY_MOUSE:
  19.       mvprintw(1, 0, "KEY_MOUSE");
  20.       if(getmouse(&event) == OK){
  21.         mvprintw(2, 0, "getmouse");
  22.         mvprintw(3, 0, "event.bstate = %o      ", event.bstate);
  23.         if(event.bstate == BUTTON1_CLICKED){
  24.           mvprintw(event.y, event.x, "(%d, %d)", event.y, event.x);
  25.           refresh();
  26.         }else if(event.bstate == BUTTON2_CLICKED)
  27.           e = 0;
  28.       }
  29.       break;
  30.     default:
  31.       break;
  32.     }
  33.   }
  34.   endwin();
  35.   exit(0);
  36. }
复制代码
 楼主| 发表于 2003-10-19 19:05:46 | 显示全部楼层
多谢 libinary 版主, 你真的很负责任!

没想到他的例程也能有问题!

再次谢谢 libinary,以后不会的问题还要请教你呀! 呵呵......
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表